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

This site runs on Cloudways.
It's fast isn't it?
Cloudways offers high-quality, fast and affordable hosting.

Learn more

Need a hand? Post your project and hire me and other top notch developers on Codeable.

Hire me on Codeable

Leave a Reply Cancel reply

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

Ready to take action?

I'm looking forward to discuss your projects and goals!
Feel free to reach out using the contact details below.

Marinus Klasen

[email protected]
twitter.com/marinusklasen
linkedin.com/in/marinusklasen

  • GitHub
  • LinkedIn
  • Twitter

Have you read?

  • Storing private data with SSH on WPEngine sitesDecember 15, 2022
  • Get the HTML content of a block edit page or post in WordPressNovember 30, 2022
  • Rename Coupon code text to Discount code in WoocommerceNovember 3, 2022
  • Background-size cover in mPDFOctober 22, 2022
  • WordPress.com SSH & duplicating a WordPress.com website locallyOctober 11, 2022

Copyright © 2023 · Marinus Klasen | Webdesign by Team Rood