Use traefik in local and automatic discovery for docker projects.
First of all create a network that will be used by docker projects.
just type for example :
docker network create traefik
just type
docker compose up -d
then traefik web interface should be available at traefik.docker.localhost
in your docker compose file : add these labels to exposed service (like caddy, apache, mailer ...) and the network :
Replace {SERVICE-NAME} {SERVICE-URL} and {INTERNAL_PORT_OF_SERVICE} with the correct values.
services:
webserver:
image: my-server-image
labels:
- traefik.enable=true
- traefik.http.routers.{SERVICE-NAME}.rule=Host(`{SERVICE-URL}.docker.localhost`)
- traefik.http.services.{SERVICE-NAME}.loadbalancer.server.port={INTERNAL_PORT_OF_SERVICE}
networks:
- traefik
you can have a service being accessible by multiple name :
services:
webserver:
image: my-server-image
labels:
- traefik.enable=true
- traefik.http.routers.{SERVICE-NAME}.rule=Host(`{SERVICE-URL}.docker.localhost`, `{SERVICE-URL-2}.docker.localhost`)
- traefik.http.services.{SERVICE-NAME}.loadbalancer.server.port={INTERNAL_PORT_OF_SERVICE}
networks:
- traefik
then and add the network you created in the docker compose file :
networks:
traefik:
external: true
Don't forget to update the vhost of your projects to use the {SERVICE-URL} you mentioned in your compose files.
Your project will be accessible via the {SERVICE-URL}.
If you need to install another web server like caddy for example, just make it work on another port (for example 81 instead of 80 and 441 instead of 443) then in your traefik-dynamic.yml update :
http:
routers:
test-router:
rule: Host(`other-service.localhost`) # Définir la règle de routage pour l'adresse other-service.localhost
service: caddy-service # Rediriger vers le service Caddy
entryPoints:
- web # Traefik utilise l'entrée "web" qui écoute sur le port 80 (configuré dans traefik.yml)
services:
caddy-service:
loadBalancer:
servers:
- url: "http://localhost:81" # Caddy écoute sur localhost:81
and you need to update /etc/hosts as you would usualy do without traefik.