Traefik is a powerful and modern reverse proxy that integrates easily with Docker and other orchestrators. It automatically manages services, handles dynamic routing, and supports features like HTTPS and load balancing.
docker compose --help
If it's not installed, follow the guide here.mkdir traefik-setup
cd traefik-setup
traefik.toml
Configuration FileInside the traefik-setup
directory, create a new directory for the Traefik configuration files:
mkdir config
Create the traefik.toml
file inside the config
directory:
touch config/traefik.toml
Open traefik.toml
in a text editor and add the following content:
[entryPoints]
[entryPoints.web]
address = ":80"
[entryPoints.websecure]
address = ":443"
[api]
dashboard = true
insecure = true
[providers]
[providers.docker]
endpoint = "unix:///var/run/docker.sock"
exposedByDefault = false
Here's what the configuration does:
:80
) and HTTPS (:443
).docker-compose.yml
FileIn the traefik-setup
directory, create a file named docker-compose.yml
:
touch docker-compose.yml
Open docker-compose.yml
in a text editor and add the following configuration:
version: '3'
services:
traefik:
image: traefik:v2.9
container_name: traefik
restart: unless-stopped
ports:
- "80:80" # HTTP
- "443:443" # HTTPS
- "8080:8080" # Traefik Dashboard
volumes:
- /var/run/docker.sock:/var/run/docker.sock # Give Traefik access to Docker
- ./config/traefik.toml:/etc/traefik/traefik.toml # Traefik configuration file
networks:
- traefik-public
networks:
traefik-public:
external: true
Explanation:
traefik
: This is the Traefik service that uses the official Traefik Docker image.80
: Exposes HTTP traffic.443
: Exposes HTTPS traffic.8080
: Exposes the Traefik dashboard./var/run/docker.sock
: Grants Traefik access to the Docker socket for service discovery../config/traefik.toml
: Links the local Traefik configuration file to the container.web
. This allows other services to connect to Traefik using Docker networking.web
:docker network create traefik-public
Make sure you're in the traefik-setup
directory where the docker-compose.yml
file is located.
Run the following command to start Traefik using Docker Compose:
docker-compose up -d
up
: Starts the containers defined in your docker-compose.yml
.-d
: Runs the containers in detached mode (in the background).Docker Compose will download the Traefik image (if it’s not already present) and start the Traefik container.
http://localhost:8080
Now, let’s deploy a simple web service (such as an Nginx container) and expose it through Traefik.
Create a directory for the sample service:
mkdir nginx
cd nginx
Create a docker-compose.yml
file for the Nginx service:
touch docker-compose.yml
Add the following content to docker-compose.yml
:
version: '3'
services:
nginx:
image: nginx:latest
container_name: nginx
labels:
- "traefik.enable=true"
- "traefik.http.routers.nginx.rule=Host(`nginx.srvX.lab.npnog.org.np`)"
- "traefik.http.services.nginx.loadbalancer.server.port=80"
networks:
- web
restart: unless-stopped
networks:
web:
external: true
nginx
: This is the Nginx container."traefik.enable=true"
: Enables Traefik routing for this service."traefik.http.routers.nginx.rule=Host('nginx.local')"
: Defines the routing rule (based on the hostname nginx.srvX.lab.npnog.org.np
)."traefik.http.services.nginx.loadbalancer.server.port=80"
: Specifies the internal port that Nginx is using.web
network, shared with Traefik.In the nginx
directory, start the Nginx service with Docker Compose:
docker-compose up -d
Open your browser and navigate to:
http://nginx.srvX.lab.npnog.org.np
You should see the default Nginx welcome page, confirming that Traefik is successfully routing traffic to your Nginx container.
To enable HTTPS with Let's Encrypt, update the traefik.toml
configuration to include Let's Encrypt settings.
Edit config/traefik.toml
and add the following:
[certificatesResolvers]
[certificatesResolvers.le.acme]
email = "your-email@example.com"
storage = "/etc/traefik/acme.json"
[certificatesResolvers.myresolver.acme.tlsChallenge]
This tells Traefik to use Let's Encrypt and store certificates in /etc/traefik/acme.json
.
Create an empty acme.json
file and set the correct permissions:
touch config/acme.json
chmod 600 config/acme.json
Update your Nginx service’s Docker Compose labels to enable HTTPS:
labels:
- "traefik.enable=true"
- "traefik.http.routers.nginx.rule=Host(`nginx.srvX.lab.npnog.org.np`)"
- "traefik.http.services.nginx.loadbalancer.server.port=80"
- "traefik.http.routers.nginx.entrypoints=web"
- "traefik.http.routers.nginx-secure.rule=Host(`nginx.srvX.lab.npnoh.org.np`)"
- "traefik.http.routers.nginx-secure.entrypoints=websecure"
- "traefik.http.routers.nginx-secure.tls.certresolver=le"
Restart Traefik and the Nginx service to apply the changes:
docker-compose restart
Visit https://nginx.srvX.lab.npnog.org.np
to see the Nginx page served over HTTPS.
To stop the Traefik and Nginx containers, run:
docker compose down
This will stop and remove the containers, but your configuration and volumes will remain.
That’s it! You have successfully set up Traefik using Docker and Docker Compose, routed traffic to an Nginx container, and optionally enabled HTTPS with Let's Encrypt.