Traefik basics with Docker-Compose

In this post you will configure Traefik to route requests to your backend. You will use Jenkins as an example of how to do a simple URL redirection.


Traefik is a modern HTTP reverse proxy and load balancer made to deploy microservices with ease. Traefik supports multiple backends and specifically Docker which we are going to use.

Traefik & Docker-Compose are a match made in heaven. While you can configure rules via a configuration file, for this post you will define everything in your Docker-Compose file.

Also worth noting, though we won’t use it here, is Let’s Encrypt support built into Traefik.

Some explanations & code:

	image: traefik
	command: --web --web.address=:8686 --docker --docker.domain=dev.local --logLevel=DEBUG \
         	--entryPoints='Name:http Address::80' --defaultEntryPoints='http' --accessLogsFile='log/access.log'
	container_name: stam_traefik
  	- traefik
  	- 80:80
  	- 8686:8686
  	- /var/run/docker.sock:/var/run/docker.sock
  	- /dev/null:/traefik.toml
  • –web enable the web interface (Can be readonly via –web.readonly, authentication is also possible)
  • –web.address is the port the interface will be exposed with
  • –docker specify the docker backend
  • –docker.domain will be appended to containers
  • – watch the docker backend for changes (add new container/remove deleted ones)
  • –logLevel you will be using debug so you can review everything that Traefik does when it starts up

This is the first part of our compose file, the second will be Jenkins. Notice that we can setup Traefik and run it and later spin-up whatever containers we wish and Traefik will pick it up. In order to do that you need to specify new rules using labels in our Docker-Compose file. That way you can stay dynamic.

So far we have established the foundation for Dynamic routing let’s run a service and use Trearik to route its traffic.

Jenkins via Traefik

Running a Jenkins container and Accessing it via “”. You will need to use **JENKINS_OPTS=”–prefix=/jenkins” **to insure that when Jenkins itself redirects you it will use the correct path.

For this post you will use a custom Jenkins container that has a few pre-installed plugins, ‘admin’ user and Docker installed.

plugins.txt contains the plugins to install, they will be downloaded during the ‘docker build’

default-user.groovy contains the bit of code for creating the user

FROM jenkins/jenkins:lts


USER root
RUN apt-get update && apt-get install -y \
	apt-transport-https \
RUN curl -fsSL | apt-key add -
RUN echo "deb \
$(cat /etc/os-release | grep -E '^VERSION=' | awk '{print $2}' \
| tr -d '()\"') stable" >> /etc/apt/sources.list
RUN apt-get update && apt-get install -y \
RUN usermod -aG docker jenkins

USER jenkins
COPY default-user.groovy /usr/share/jenkins/ref/init.groovy.d/
COPY plugins.txt /usr/share/jenkins/ref/plugins.txt
RUN /usr/local/bin/ < /usr/share/jenkins/ref/plugins.txt

If using docker natively a simple Docker build process:

docker build -t stam_jenkins . -f ./Dockerfile_jenkins

Now that you have the image (verify with ‘docker images’) you can add Jenkins to your compose file, or alternatively instead of using the image: In compose you can use build ./Docker_jenkins which will build it with docker-compose build but let’s skip to the simple use case:

	image: stam_jenkins
	container_name: stam_jenkins
  	- traefik
  	- jenkins-data:/var/jenkins_home
  	- /var/run/docker.sock:/var/run/docker.sock
  	- "traefik.port=8080"
  	- "traefik.jenkins.frontend.rule=Host: dev.local; PathPrefix: /jenkins"
  	- "traefik.enable=true"
  	- JENKINS_OPTS=--prefix=/jenkins
  	- JAVA_OPTS=-Dhudson.footerURL=http://dev.local -Djenkins.install.runSetupWizard=false
  	- traefik

Run ‘docker-compose up -d’ to bring up the servers.

To easily access the servers add the following to your /etc/hosts file:

dev.local ![Screenshot from 2017-10-10 14-23-50.png](/media/Screenshot from 2017-10-10 14-23-50.png) Traefik will route your requests based on the rules we defined earlier using ‘lables’ in Docker-Compose. In this example you have exposed the Traefik dashboard on port 8686 and you access it via dev.local:8686 and you have exposed Jenkins on port 8080 and you access it via dev.local/jenkins. ![Screenshot from 2017-10-10 14-23-21.png](/media/Screenshot from 2017-10-10 14-23-21.png)

Thank you for your interest!

We will contact you as soon as possible.

Want to Know More?

Oops, something went wrong
Please try again or contact us by email at
Thank you for your interest!

We will contact you as soon as possible.

Let's talk

Oops, something went wrong
Please try again or contact us by email at