Installing Traefik with Docker

Installing Traefik with Docker

In this tutorial we will simply launch a traefik docker image with a whoami docker image over a docker network. The official Traefik docs can be found at: https://doc.traefik.io/traefik/getting-started/quick-start/

The only pre-requisites are that you already have docker and docker compose installed. See my tutorial here for installing docker: https://phoenixignited.tech/install-docker-on-ubuntu/ and here for installing docker compose: https://phoenixignited.tech/install-docker-compose-on-ubuntu/

Running Traefik

We will first off create a compose.yaml file for Traefik: 

							
							
					mkdir traefik && cd traefik && nano compose.yaml				
			

Inside the compose.yaml file we will enter:

							
							
					version: '3'

services:
  reverse-proxy:
    image: traefik:v3.0
    command:
      - "--api.insecure=true"
      - "--providers.docker=true"
      - "--entrypoints.web.address=:80"
      - "--entrypoints.websecure.address=:443"
    ports:
      - "80:80"
      - "443:443"
      - "8080:8080"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    networks:
      - traefik

networks:
    traefik:
      external: true
				
			

Next we need to create a docker network called traefik

							
							
					docker network create traefik				
			

We can start our Traefik docker image with:

							
							
					docker-compose up -d reverse-proxy				
			

Now that our docker traefik image is running we need to run the whoami instance

Running Whoami

We will begin with creating the whoami compose.yaml file.

							
							
					cd .. && mkdir whoami && cd whoami && nano compose.yaml				
			

We will enter the following into the compose.yaml file:

							
							
					version: '3'

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

networks:
  traefik:
    external: true
				
			

Ensuring Traefik & Whoami Are Up and Running

We can check and see that Traefik is running and that Traefik is recognizing Whoami by going to http://<your_host_ip>:8080, this will display the traefik dashboard as seen below:

Now we can go to our http services and find our whoami service, we should see something like: 

We can also run in the terminal: 

							
							
					curl -H Host:whoami.docker.localhost http://127.0.0.1
				
			

You should get something similar to:

We can see that whoami and traefik are running successfully, and that traefik sees whoami and is acting as a reverse proxy, forwarding traffic to the whoami server, which was our goal. That’s a wrap! I hope you found it useful and stay tuned for more!

Walter is a tech entrepreneur and in his free time loves fitness, art, writing, and exploring Montana's outdoors.