Service AutoDiscovery Using Traefik For Docker Containers

Deepesh Tripathi
Dec 6, 2019 · 3 min read

In a containerized environment it is essential to expose your service containers to the outer world so that end users can access your service containers.

simple expose of a container service can be done in Dockerfile using expose directive and with command line as well.

A more scaled and production use case could be accessing your container app using name-based/host routing or path based routing. this functionality can be easily implemented using Traefik edge routing.

Basically an edge router works a doorway to your platform which is able to check or intercept every request which comes to your platform. edge router like traefik listens on a particular port and receives traffic requests for your container platform in form of the path, the host, or headers and redirect them to the service which can handle that request based on rules defined during configuration.

traefik is easy configurable edge routing for your ingress routing needs which auto-discovers new services in your platform and adds route for them without any configuration. vice versa is also true. whereas traditional edge routing/reverse proxy apps need a configuration file that contains routes to your services. traefik just keep itself in contact to docker API and listens to any spin up or spin down of containers and accordingly add/deletes route for them.

connectors which fetch service information via cluster API are called providers and provide configuration to traefik in real-time.

Below is the working example of how to do the setup of traefik edge routing for docker containers in a cluster.

Illustration of edge routing with Traefik

to test this piece of code you should have docker and docker-compose installed in your machine.

Launching Traefik with docker provider

create a docker-compose.yml file in a directory, where you can use reverse-proxy service using traefik official image.

version: '3'  
services:
reverse-proxy:
# The official v2.0 Traefik docker image
image: traefik:v2.0
# Enables the web UI and tells Traefik to listen to docker command: --api.insecure=true --providers.docker
ports:
# The HTTP port
- "80:80"
# The Web UI (enabled by --api.insecure=true)
- "8080:8080"
volumes:
# So that Traefik can listen to the Docker events
- /var/run/docker.sock:/var/run/docker.sock

start your revery-proxy traefik service with below command

docker-compose up -d reverse-proxy

Accessing traefik console

http://localhost:8080
<localhost can be replaced with host ip>

a simple routing service is enabled for you which can work as reverse proxy now.

let’s see how traefik auto-discovers on the launch of new services. to do so just add below line of code at the end of your docker-compose.yml

# ...   
whoami:
# A container that exposes an API to show its IP address
image: containous/whoami
labels:
-"traefik.http.routers.whoami.rule=Host(`whoami.docker.localhost`)"

Start the whoami service with the following command:

docker-compose up -d whoami

When Traefik detects new services, it creates the corresponding routes so you can call them

auto-discovery of launched whoami service by traefik provider in docker cluster

Here you go!! working reverse-proxy with auto-discovery using traefik, do let me know if you like this write-up and what topics you would like to see in the future from me.

Deepesh Tripathi

Written by

A Tech Geek | Meditator | Writer | Vedic Scholar | Speaker who loves to write about technology and humanity for personal and professional transformation.

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