Traefik + Docker is a great set up for developing microservice architectures

Docker + Traefik

Create a microservice architecture and handle routing

Eric Fossas
Aug 19, 2018 · 5 min read

Docker is a container management tool and Traefik is a reverse proxy for managing the routing between containers. With both tools, you can create a microservice architecture.

This quick tutorial will focus on developing with Docker and Traefik. Thus, it won’t go over Docker swarm mode very much (which is for running Docker on multiple machines).

I’ll be using this repo for the tutorial: https://github.com/efossas/CRUD. It’s for creating a CRUD application. It’s super simple to set up. Run the commands below and set up DNS to route crud.docker.localhost & traefik.docker.localhost to localhost (127.0.0.1). The easy way to do that is to edit /etc/hosts

git clone https://github.com/efossas/CRUD 
cd CRUD
docker-compose up -d

Btw, it helps to have a strong understanding of how Unix shells work to really understand Docker.

Containers as far as the eye can see!

What This Tutorial Covers

  1. Docker basics

What You Need For This Tutorial

Docker


Docker (Basics)

Docker is a set of tools for managing containers. A container is essentially a sandboxed user level operating system with a single running process.

Run the following command and you will be put inside a container with the Alpine operating system. It’s running process is the ASH unix shell, so you can run typical commands like ls, cd, pwd, etc. To exit the container, run exit.

docker run -it alpine

Btw, there are many linux based operating system Docker containers, like Ubuntu and Centos, but Alpine is very common as it has the smallest size.

The following are basic Docker commands everyone should know:

  • docker run -itd — name NAME IMAGE

Building A Docker Image

A Docker image is like a template. When you run an image, it becomes a container. To build a Docker image, we first create a Dockerfile with instructions. The following is a simple Dockerfile definition:

Now to actually build the file, we run the following command:

# without the -f flag, Docker assumes the file is called 'Dockerfile' 
# the . at the end just means to look for the file in current directory
docker build --no-cache -f Dockerfile -t efossas/crud:latest .

The no-cache flag requires some explaining. For each instruction in the Dockerfile, Docker creates a new commit, like the way git does a commit. When you rebuild an image, Docker will use the results of previously run instructions instead of running them again. Using the no-cache ensures that all instructions are run again.

The following are some commands for viewing, removing, pulling, and pushing images:

  • docker image ls

For the push command to work, you must have a registry set up. You can run your own using the official registry image https://hub.docker.com/_/registry/. You can also use DockerHub to host your images. Finally, there are many other SaaS providers that offer registry services, like GitLab.

Finally, Docker assumes you’re using DockerHub as your registry. To use a different one, you must run the following command:

docker login URI

Docker Terminology

  1. Image — A template for a container.

Docker Compose

Compose is a tool that allows you to declare Docker stacks. A Docker stack is a group of related services.

It’s best explained by just showing you a complete docker-compose.yml file with comments explaining what everything does.

Now to start your microservice application, you run the following command:

# without the -f flag, Docker assumes the file is called 'docker-compose.yml' 
# the -d flag is for detached mode, in that Docker will run as a background process
docker-compose -f docker-compose.yml up -d

Finally, some extra configuration can be put in a hidden .env file.


Traefik

Traefik is a reverse proxy. It’s useful for routing connections to different services based on different domains and paths. For example, you can have http://DOMAIN/first/pathroute to one container & http://DOMAIN/second/pathroute to another container. Traefik also has load balancers built in, though I don’t use them in this tutorial as it’s focused on development more than production.

There are 3 important things to understand about Traefik.

  1. Entry Point is a port that Traefik accepts connections to. Entry points are defined in traefik.toml. Then, each service must declare which entry point(s) it wants to accept connections from.

A list of configuration and rules can be found here: https://docs.traefik.io/configuration/backends/docker/#on-containers

So, after spinning up our Docker stack, our application networking looks like the image below:

Here is the traefik.toml configuration.

Btw, feel free to tell Traefik it should support yml files for configuration here: https://github.com/containous/traefik/issues/2999


Done!

Well, that’s zero to hundred in about as short as I could make it for Docker and Traefik.

Eric Fossas

Written by

Twitter: @ericfossas | Github: efossas

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade