Why Docker and Traefik
Recently I decided to redeploy my home-server. It was still running Ubuntu 12.04 and the configuration drifted ever since it was setup. Time for a revamp. I opted to use Docker and Traefik because I wanted a setup that was easy to update and easy to maintain.
All the applications I use are available on Docker Hub. Keeping all configuration in a single GIT repo using docker-compose makes it easy to maintain.
I also decided to introduce a reverse proxy, this allows all my applications to be available by entering a domain name (no more remembering ports!). Docker and Traefik combined make that easy. On top of that it’s also very easy to add SSL due to Traefik’s the Let’s Encrypt integration.
In this blog post we will be building this using Docker and Traefik:
What distribution to choose
For my home-server I opted to use Alpine Linux because it’s lightweight and an excellent docker host. For this blog post I’ll be using Amazon Linux so it’s easy for you to follow along. However, you can use any other distro that you are happy with.
Docker host and firewall rules
We will start by spinning up a new host OS, next we will setup firewall rules. Depending on what you’re using instructions might slightly differ. The instructions below are for AWS.
Start a new host-os, I’m using Amazon Linux:
We should open the following ports in the firewall:
- 22 (to SSH into the machine, you should trim this down to your own IP).
- 80 (for HTTP traffic, required for requesting the SSL Certificate from Let’s Encrypt.
- 443 (for HTTPS)
Creating the DNS record set
We want our Docker apps to be available over the internet and we are going to setup HTTPS. Hence, we need to own a DNS domain. In order for Traefik to request certificates for the domain, we need to set up DNS first. We can either create A-Records, or use a CNAME. I will use the following two CNAME records.
- *.dockerdemo.yourdomain This will be used by containers (e.g. app1.dockerdemo.javydekoning.com)
- dockerdemo.yourdomain This will be used by Traefik (This is NOT required, we could also use traefik.dockerdemo.javydekoning.com)
From the instance/VM we just created, copy either the public IP (to be used in an A-Record), or the Public DNS name (to be used in a CNAME record). I will be using the public DNS name, therefore I’ll create a CNAME record.
Instructions will differ depending on your DNS provider. My zone is hosted in Route53, so we can create a record by clicking “Create Record Set” in the AWS console. We use a wildcard (*) record because we plan to create multiple sub-domains for our containers.
Configuring the Docker host
The OS only needs to run Docker and Docker-Compose all the other applications will run in containers. Let’s set that up. We’ll install docker, docker-compose and make sure the docker daemon is started on system boot.
1#Install dependencies 2sudo su 3yum update -y 4yum install -y python-pip docker 5pip install docker-compose 6 7#Start docker and configure to start on init. 8service docker start 9chkconfig docker on 10 11#create files and directories 12mkdir /dockerdemo/traefik -p 13touch /dockerdemo/docker-compose.yml \ 14 /dockerdemo/traefik/traefik.toml \ 15 /dockerdemo/traefik/acme.json 16chmod 600 /dockerdemo/traefik/acme.json
Next, we will create the required files and folders. That will look like this:
Create and set permissions:
1#create files and directories 2mkdir /dockerdemo/traefik -p 3touch /dockerdemo/docker-compose.yml \ 4 /dockerdemo/traefik/traefik.toml \ 5 /dockerdemo/traefik/acme.json 6chmod 600 acme.json
Creating a Traefik container
First we setup our Traefik container. This container will act as a reverse proxy and redirect the traffic to the correct container. For this we will have to set up two files:
- /dockerdemo/docker-compose.yml This will tell docker what containers to run
- /dockerdemo/traefik/traefik.toml This will hold the Traefik config
We will start with the traefik config file. We want to configure the Traefik webinterface
[web] and re-direct all http traffic to https to encrypt all our traffic. Finally, when we create a new docker container we don’t want to reconfigure Traefik, hence we tell Traefik to watch Docker.
traefik.toml in the editor of your choice and edit as follows:
1#Comment out when done. 2logLevel = "DEBUG" 3 4defaultEntryPoints = ["http","https"] 5 6[web] 7 #Run Traefik info page at 8080. 8 address = ":8080" 9 10[entryPoints] 11 [entryPoints.http] 12 #redirect ALL http traffic to https 443 13 address = ":80" 14 [entryPoints.http.redirect] 15 entryPoint = "https" 16 [entryPoints.https] 17 address = ":443" 18 [entryPoints.https.tls] 19 20#Let's encrypt setup 21[acme] 22 email = "firstname.lastname@example.org" 23 storage = "acme.json" 24 entryPoint = "https" 25 #When new host is created, request certificate. 26 onHostRule = true 27 [acme.httpChallenge] 28 entryPoint = "http" 29 30#Watch Docker, when new containers are created with label create mapping. 31[docker] 32 endpoint = "unix:///var/run/docker.sock" 33 domain = "dockerdemo.javydekoning.com" 34 watch = true
Next we will setup our
docker-compose.yml file. We will start with just the Traefik container.
1version: '3' 2services: 3 traefik: 4 image: traefik 5 container_name: traefik 6 ports: 7 - "80:80" 8 - "443:443" 9 volumes: 10 - "/var/run/docker.sock:/var/run/docker.sock" 11 - "./traefik/traefik.toml:/traefik.toml" 12 - "./traefik/acme.json:/acme.json" 13 labels: 14 - "traefik.port=8080" 15 - "traefik.frontend.rule=Host:dockerdemo.javydekoning.com"
Some key pointers:
- docker.sock we map the sock file from the host container, so Traefik can monitor changes in the docker environment.
- ./traefik/* maps the configuration file and certificate store from our host to our Traefik container.
- traefik.port tells traefik to which backend port traffic needs to be redirected.
- traefik.frontend.rule tells traefik which ‘Host header’ should be redirected to this container.
Make sure you are in the directory of your
docker-compose.yml and run
1/usr/local/bin/docker-compose up -d
Now go to the IP of your container host to see if it works. You should see:
Don’t worry about the 404. This is what we expect! Remember, we told Traefik ONLY to redirect traffic to our container if the http host header is ‘dockerdemo.yourdomain’. Let’s test that as well, you should be redirected to HTTPS and see:
If you are presented with an invalid certificate, troubleshoot using:
1docker logs traefik
Adding two more containers
Finally we will introduce two more containers.
- Portainer lightweight management UI for your docker environment.
- jwilder/whoami simple HTTP docker service that prints it’s container ID.
For the whoami container we will also add a form of authentication. You can skip this if you don’t want that, or you can use the hash I generated (for testing only).
We will generate a secure Bcrypt hash. On your local machine install the apache2 utils and run:
1htpasswd -nbB -C 13 username YouShouldNotUseThisPassword!
This should return:
You need to duplicate the ‘$’ in this string when adding this to your docker-compose.yml file.
Let’s complete our docker-compose.yml file:
1version: '3' 2services: 3 traefik: 4 image: traefik 5 container_name: traefik 6 ports: 7 - "80:80" 8 - "443:443" 9 volumes: 10 - "/var/run/docker.sock:/var/run/docker.sock" 11 - "./traefik/traefik.toml:/traefik.toml" 12 - "./traefik/acme.json:/acme.json" 13 labels: 14 - "traefik.port=8080" 15 - "traefik.frontend.rule=Host:dockerdemo.javydekoning.com" 16 17 portainer: 18 container_name: portainer 19 image: portainer/portainer:latest 20 volumes: 21 - "/var/run/docker.sock:/var/run/docker.sock" 22 - ./portainer:/data 23 labels: 24 - "traefik.port=9000" 25 - "traefik.frontend.rule=Host:portainer.dockerdemo.javydekoning.com" 26 27 whoami: 28 container_name: whoami 29 image: jwilder/whoami 30 labels: 31 - "traefik.port=8000" 32 - "traefik.frontend.rule=Host:app1.dockerdemo.javydekoning.com" 33 - "traefik.frontend.auth.basic=username:$$2y$$13$$dSXnrizi74xsfKH740Axt..tIiCL16GGpQn4hpX6Wtph15XGVS08u"
There is one newly added label:
- traefik.frontend.auth.basic tells traffic to use basic authentication to authenticate a user before passing traffic on to the container.
Final Docker and Traefik test!
Make sure you are in the directory of your
docker-compose.yml file again and run
1/usr/local/bin/docker-compose up -d
This should bring up the two remaining containers. Now the first thing you should do is go to portainer.yourdomain and SET the initial credentials.
Next go to app1.yourdomain, and you should be asked for the credentials that you’ve created with the ‘htpasswd’ tool. If you’ve used my exact example that would be
- user: username
- pass: YouShouldNotUseThisPassword!
Now you should see this:
Congrats, you’ve configured Docker and Traefik (reverse-proxy), Docker-Compose, ssl and authentication!