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!
Leave a Reply