Prerequisites

If you want to follow along, you need to have the following:

  • Docker. Version 20.10.13 is used in this tutorial.
  • Docker-Compose. This tutorial uses version 1.29.2.
  • You can use any other Linux-based operating system that allows you to run Docker and Docker-Compose freely. However, Fedora 35 host Docker was used in this article.

Installing and configuring Traefik with Docker-Compose

The best way to see all enabled configuration options for a Traefik setup, while taking full advantage of native integration with Docker, is to use a Compose file. For this tutorial, we will use the file in the screenshot below. It is divided into two parts. The first part will focus on installing, running, and configuring Traefik statically. The second part will configure a backend Apache HTTPD service for Traefik via labels.

Running the Compose file

Now that you have a working understanding of the Compose file, it is time to run it in your Docker environment.

Copy and paste the full file below into a new text file.

services:
traefik:
image: “traefik:latest”
ports:
“80:80”
“8080:8080”
command:
“–api.insecure=true”
“–providers.docker=true”
“–entrypoints.web.address=:80”
volumes:
“/var/run/docker.sock:/var/run/docker.sock”
apache:
image: “httpd:latest”
labels:
“traefik.http.routers.apache.rule=Host(`webserver.localhost`)”
“traefik.http.routers.apache.service=apache-serv”
“traefik.http.services.apache-serv.loadbalancer.server.port=80”

Save it as compose.yml.

Execute docker-compose, as shown below, in a terminal window to bring up the services in the background (-d):

# docker compose -f traefik/docker-compose.yml up -d

You should have feedback in your terminal, as follows, when the containers are up and running.

Bringing up the containers

Open a browser window to http://webserver.localhost to confirm that you can reach the backend service successfully.

Accessing the backend service in a browser

Also, bring up the dashboard at http://localhost:8080 to view stats on your Traefik setup.

Conclusion

Installing and configuring Traefik can be a complex undertaking because you need to have a little container administration knowledge. This is one of the downsides of using Traefik. However, if Docker is your container environment, using Docker-Compose allows you to have all your configuration options in one location. In a few lines, it is straightforward to set up and customize components such as entryPoints, routers, and services. Once you climb the initial learning curve, you can enjoy all the benefits of Traefik, such as minimal configuration, automatic SSL termination, and automatic service discovery.

Leave a Reply