Deploying Traefik with Docker: A Tutorial

Traefik is the best and simplest reverse proxy I have ever used, compared to Caddy, NGINX, and others. Besides that, Traefik already comes with automatic SSL like Caddy, but goes further — it’s integrated with Docker and Kubernetes.

First, you need to create a dedicated network — in my example I create my own network named pratama-network, which you can create with the command below.

docker network create pratama-network

Next, create a directory named traefik on your server, then create a compose.yaml file with the code below.

After this, you still need to create a few files:

  • traefik.toml -> Traefik’s main configuration
  • dynamic-conf.toml -> HTTP, TCP and UDP routing configuration
  • acme.json -> SSL content generated by Traefik

Here I use the TOML format for the Traefik configuration. Normally people use the YAML format, but I personally prefer TOML. You can use YAML directly if you prefer it, but in this tutorial we’ll stick with the TOML format.

Let’s create the files first: traefik.toml and dynamic-conf.toml will be stored in the conf directory, while acme.json will sit at the same level as compose.yml

Here are the commands in short:

mkdir conf
touch conf/traefik.toml
touch conf/dynamic-conf.toml
touch acme.json
chmod 600 acme.json

In order, the layout looks like this:

-- | /home/<user>/docker/traefik
   | -- compose.yml
   | -- acme.json
   | -- conf
        | -- traefik.toml
        | -- dynamic-conf.toml

Based on the structure above, I added the docker directory to keep things tidy, but you’re free to decide where the traefik directory lives — and it doesn’t even have to be called traefik, it’s just for tutorial purposes. Now, let’s fill in traefik.toml, which looks roughly like this

Then fill in the dynamic-conf.toml file as below — both files above will be explained in detail later.

The code is all done — you just need to replace the traefik.supanadit.com part with your own domain, then run it with the command below.

docker compose up -d

In short, the Traefik deployed on your server is now accessible. With the configuration above, for example, it’s a small slice of my server — and in this case it’s at the following link.

http://traefik.supanadit.com

Wait…. however, you won’t be able to access it — I protect it with Authelia as Middleware, so not everyone can see the list of apps on the dashboard.

😁Curious how I use Authelia and integrate it with Traefik? I’ll write more articles about Traefik another time, and I’ll create a dedicated page about Traefik.

FAQ

How can other Docker apps be accessed?

There are two ways to do this:

  1. Using labels
  2. Writing routing configuration in dynamic-conf.toml

The details will be covered in a different article, and I’ll share the link here.

What about acme.json?

You can just leave it, because Traefik will fill that file automatically, provided that:

  1. Your website is configured with lets encrypt in the traefik.toml section
  2. Then in the dynamic-conf.toml section, enable the property I marked with Uncomment this for production use, and comment out the part I marked with Comment this for production use. Except the service — never comment that part out, whether in production or not.
What about testing locally with Docker Engine / Desktop on Windows / Mac / Linux

You can follow the step-by-step above without changing anything — meaning websecure stays disabled, and in traefik.toml just comment out the lines below.

[certificatesResolvers.lets-encrypt.acme]
  email = "xxx@xxx.com"
  storage = "acme.json"
  [certificatesResolvers.lets-encrypt.acme.tlsChallenge]

Then you can configure /etc/host on Linux / Mac and C:\Windows\System32\drivers\etc\hosts on Windows with the configuration below.

127.0.0.1 traefik.supanadit.com

The domain is up to you, as long as it matches what’s in traefik.toml

If you have questions or something is unclear, write it in the comments below and I’ll update this article.

Leave a comment

Your email address will not be published. Required fields are marked *