Marinus Klasen

Marinus Klasen

  • Development
  • Consultancy
  • Developer Resources
  • Blog
  • Contact

October 6, 2022

Make a service available on a different port in docker-compose with Traefik

Marinus Klasen

This is one of these things that I broke my head around a couple of times. And every time I need to do this, it takes me a fair amount of trial and error before solving it. Let’s get that over once and for all.

(seriously, i’ll probably Google for this issue in about 6 months and find my own blog article)

Alright, so in this specific situation, I want to make my mailhog service available with a custom domain, that way, I can just type ‘mail.c7’ in the address bar and see e-mails of all my docker containers coming in. Great for testing purposes!

There’s a couple of rules we need to set for this, but let’s first have a look a the full docker-compose file:

version: "3"

services:
  catchall:
    container_name: catchall
    image: mailhog/mailhog:latest
    user: "1000:1000"
    logging:
      driver: 'none'  # disable saving logs
    expose:
      - 8025
      - 1025
    labels:
      - "traefik.enable=true"
      - "traefik.docker.network=traefik_proxy"
      - "traefik.http.routers.mail.rule=Host(`mail.c7`)"
      - "traefik.http.routers.mail.tls=true"
      - "traefik.http.services.mail.loadbalancer.server.port=8025"
    restart: always

networks:
  default:
    external:
      name: traefik_proxy

You can probably guess which rules are of importance here, it’s these ones:

      - "traefik.enable=true"
      - "traefik.docker.network=traefik_proxy"
      - "traefik.http.routers.mail.rule=Host(`mail.c7`)"
      - "traefik.http.routers.mail.tls=true"
      - "traefik.http.services.mail.loadbalancer.server.port=8025"

But, most importantly, it’s the loadbalancer one. So, Mailhog makes the web interface available via 8025. By setting the loadbalancer rule and defining port 8025, we make the service available for http/https traffic.

Mind that it’s important to use the same name for the routers and services, we’re using mail in the example above.

The Traefik docker-compose file

The blog wouldn’t be complete with an example of the main Traefik docker-compose file, you’ll find it below:

version: "3.3"

services:

  traefik:
    image: "traefik:v2.8"
    container_name: "traefik"
    restart: always
    command:
      #- "--log.level=DEBUG"
      - "--api.insecure=true"
      - "--providers.docker=true"
      - "--providers.docker.exposedbydefault=false"
      - '--providers.docker.watch=true'
      - "--providers.file.directory=/etc/traefik"
      - "--entrypoints.websecure.address=:443"
      - "--entrypoints.web.address=:80"
      - "--entrypoints.web.http.redirections.entryPoint.to=websecure"
      - "--entrypoints.web.http.redirections.entryPoint.scheme=https"
      - "--entrypoints.web.http.redirections.entrypoint.permanent=true"
    ports:
      - "80:80"
      - "443:443"
      - "8080:8080"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
      - "./certs:/etc/certs:ro"
      - "./config/:/etc/traefik/"
    networks:
      default:
        ipv4_address: 172.24.0.19

networks:
  default:
    external:
      name: traefik_proxy

Alright, hope that makes sense!

Happy developing!

About Marinus Klasen

Marinus has been working in software/web development for more than a decade. Since 2020 his attention shifted on sharing knowledge and developing products and tools for sharing knowledge.

Marinus Klasen on Twitter

Looking for a WordPress expert?
Hire top notch developers on Codeable!

Consult an expert

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Get in touch

Contact me using the details below

Marinus Klasen

Wilhelmina van Pruisenweg 35, 2595 AN Den Haag

marinus@mklasen.com
twitter.com/marinusklasen
linkedin.com/in/marinusklasen

  • GitHub
  • LinkedIn
  • Twitter

Have you read?

  • Elementor Widget UsageMarch 27, 2025
  • Programmatically showing popups with ElementorMarch 25, 2025
  • The Elementor sanitize_settings errorSeptember 12, 2024
  • Announcing.. Wooping Shop Health!June 13, 2024
  • Swiper setup when using wp-scriptsMarch 14, 2024

Copyright © 2025 ยท Marinus Klasen | Webdesign by Team Rood