Use traefik in local and automatic discovery for docker projects.
Create a network that will be used by docker projects.
docker network create traefik
docker compose up -d
then traefik web interface will be available at traefik.docker.localhost
in your docker compose file: 2 steps are needed
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:
- default // you need to add this in order to keep the default network for your stack
- 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`) || Host(`{SERVICE-URL-2}.docker.localhost`)
- traefik.http.services.{SERVICE-NAME}.loadbalancer.server.port={INTERNAL_PORT_OF_SERVICE}
networks:
- default // you need to add this in order to keep the default network for your stack
- traefik
then and add the network you created in the docker compose file :
networks:
default: // you need to add this in order to keep the default network for your stack
traefik:
external: true
Remember 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}
Create certificate
- mkcert -cert-file certs/local-cert.pem -key-file certs/local-key.pem "docker.localhost" ".docker.localhost" "domain.local" ".domain.local"
- Reminder: X.509 wildcards only go one level deep, so this won't match a.b.docker.localhost
In your project update labels
labels:
- traefik.enable=true // should already be existing il you followed previous steps
- traefik.http.routers.{SERVICE-NAME}.rule=Host(`project.docker.localhost`) // should already be existing il you followed previous steps
- traefik.http.services.{SERVICE-NAME}.loadbalancer.server.port=80 // should already be existing il you followed previous steps
// TLS related lines
- traefik.http.routers.{SERVICE-NAME}-tls.rule=Host(`project.docker.localhost`) // add these lines on services you want to be https exposed
- traefik.http.routers.{SERVICE-NAME}-tls.tls=true // add these lines on services you want to be https exposed
- traefik.http.routers.{SERVICE-NAME}-tls.entrypoints=webSecure // add these lines on services you want to be https exposed
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`) # Define the router rule "other-service.localhost", the service will be available at this address
service: caddy-service # Redirect to the caddy-service
entryPoints:
- web # will use the web entrypoint configured in traefik.yml.
services:
caddy-service:
loadBalancer:
servers:
- url: "http://localhost:81" # Caddy listening on localhost:81
and you need to update /etc/hosts as you would usually do without traefik.