Traefik
Docker Compose
Firstly start be creating a docker-compose.yaml
file, which will be used to hold the configuration for Traefik
.
Traefik Docker Compose
Below is an example docker compose file that has been used to host a variety of services within docker containers.
Components
Note: all the configuration above, is done using “Labels” which are part of the documentation provided by Traefik on their official website. The labels are an alternative to using the traefik.yaml
file
- Ports : Exposes HTTP (80), HTTPS (443)
- Volumes: Configuration storage / acccess should it be required without having to access the container directly.
- Docker Socket for container management
--certificatesresolvers.myresolver.acme.storage
for SSL certificate storage- Network custom networks that are used throughout the rest of the docker containers deployed behind traefik.
- environment specific configuration that is defined to access relevant services, required by Traefik in order to function.
- whoami is a “test” container that displays a variety of information, used for troubleshooting.
Creating the Networks
Before deploying, networks need to be created using the Docker CLI:
docker network create frontend
docker network create backend
Traefik Configuration (traefik.yaml)
Important Notes
- ACME Configuration - Primarily for HTTPS, Traefik will communicate with Lets Encrypt to obtain, issue and renew certificates as needed.
- email - Used for any important communications from Lets Encrypt notifications.
Preparing acme.json
Create an empty acme.json
file with restricted permissions to securely store SSL certificates. Based on the above configuration this needs to be created in the ./config/traefik
folder.
Steps above are crucial to make sure that SSL certificates are kept securely and confidential.
Deploy Traefik
With configuration completed (acme.json
), and the docker networks created, deploy Traefik using Docker Compose:
docker compose up -d
Securing Services with HTTPS: Example
Deploying traefik’s whoami container, using traefik labels to enable HTTPs.
Docker Compose for Traefik Whoami container
Highlights
- Labels: Configure routing rules, entry points, certificate resolver (SSL / TLS), Loadbalancer (Access via Traefik), DNS Challenge (endpoint / service has a valid dns record, controlled through Cloudflare)
- Networks: Ensures that Traefik’s Whoami container has the correct network access, which allows Traefik to provide the proper routing.
- Metrics: Metrics have now been added as of 15/12/2024, which are scraped as part of the Grafana configuration.
Deployment Steps
- Prepare Configuration Files: Create an empty
acme.json
with restrictive permissions (chmod 600 acme.json
) and update theLabels
on the above docker compose example as required. - Deploy Traefik: Use
docker compose up -d
in the directoy containing the above traefik docker compose filedocker-compose.yaml
3.Verify Traefik Operation: Check if the Dashboard can be accessed. Configuration has been removed from the above example, so it will need to be added according to the official documentation supplied by Traefik themselves. 4.Deploy Services: Repeat the Deployment Steps for additional services required, ensuring that the appropriate labels from Traefik are included.