Traefik (pronounced traffic) is a modern HTTP reverse proxy and load balancer that makes deploying microservices easy. Traefik integrates with your existing infrastructure components (Docker, Swarm mode, Kubernetes, Consul, Etcd, Rancher v2, Amazon ECS, …) and configures itself automatically and dynamically. Pointing Traefik at your orchestrator should be the only configuration step you need.
Instructions
Pre-requisites
Registered domain name and control over DNS records
acme.json requires a permission value of 600. Make sure to set this using:
chmod 600 acme.json
Docker Network
Create our docker network. Our applications will communicate over this network.
docker network create proxy
.env
Info
Generating a password is only necessary if using the dashboard. In production use this is generally not recommended. If using the dashboard don’t expose it to the internet or look into a proper authentication service e.g. Zitadel, Authelia, Authentik, or Keycloak. However, it is useful for testing if our staging certificate works when setting up for the first time.
Generate a password using the following command:
echo $(htpasswd -nB user) | sed -e s/\\$/\\$\\$/g
Change the example password with your generated password:
.env
DDN=example.com # change to your domain nameTRAEFIK_DASHBOARD_CREDENTIALS=user:$$2y$$05$$lSaEi.G.aIygyXRdiFpt7OqmUMW9QUG5I1N.j0bXoXxIjxQmoGOWu # swap with generated password
It sometimes doesn’t want to use the cleaned out file. If this happens you just have to recreate the file.
cd datarm acme.jsontouch acme.jsonchmod 600 acme.json
Restart the stack
docker compose restart
Exposing other workloads using traefik
Add following labels to your docker-compose file:
docker-compose-example.yml
... labels: # traefik - traefik.enable=true - traefik.http.services.example.loadbalancer.server.port=80 # this depends on what port the service is running on - traefik.http.routers.example.entrypoints=https - traefik.http.routers.example.tls=true - traefik.http.routers.example.rule=Host(`example.${DDN}`)...
Tip
Don’t forget to change the router names to whatever the service is you are running. Also change the url to whatever you want.
Option 1
docker-compose-example.yml
... labels: # traefik - traefik.enable=true - traefik.http.services.nginx.loadbalancer.server.port=80 # this depends on what port the service is running on - traefik.http.routers.nginx.entrypoints=https - traefik.http.routers.nginx.tls=true - traefik.http.routers.nginx.rule=Host(`nginx.${DDN}`)...