traefik-in-docker-compose
traefik-in-docker-compose Mayur Chavhan

How to launch Traefik reverse proxy using docker-compose?

Mayur Chavhan DevOps

There is a growing demand for centralised, multi-domain, and secure application hosting in light of the proliferation of reverse proxy services. This is partly due to the accessibility of numerous open source projects that encourage experimentation.

Therefore, Traefik is introduced as a reverse proxy, and although there is much discussion about how easy it is to use, most users will find the documentation to be difficult to understand. Even though I am one of them, I have found a method to overcome my situation, and I hope to teach others to do the same.

I'm using Traefik as a reverse proxy to publish and secure services that are running in Docker containers because that's where most of the popular open source projects are housed. In this blog post, I'll show you how to use docker-compose to deploy Traefik 2 across many hosts on a local machine.


If you're using Docker and want to utilize Traefik as a reverse proxy, you may use docker-compose to set up the Traefik container and any additional containers you'd like to run behind the proxy.

Here is an example docker-compose.yml file that sets up Traefik as a reverse proxy:

 
version: '3'

services:
  traefik:
    image: traefik:latest
    command: --api --docker
    ports:
      - "80:80"
      - "8080:8080"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - ./traefik.toml:/etc/traefik/traefik.toml
      - ./acme.json:/acme.json
    networks:
      - web

networks:
  web:
    driver: bridge
 
This docker-compose.yml file does the following:
  • Defines a single service, named traefik, that runs the latest version of the Traefik Docker image.
  • Exposes the Traefik dashboard on port 8080 and the default HTTP port (80) to the host machine.
  • Mounts the Docker socket file as a volume, so that Traefik can listen for container events and update its configuration accordingly.
  • Mounts a configuration file (traefik.toml) and an ACME JSON file (acme.json), which are used to configure Traefik's behavior and store SSL certificates, respectively.
  • Creates a custom network named web and assigns the traefik service to it.

To start Traefik and any other containers behind the proxy, run the following command:

 
$ docker-compose up -d

This will start the traefik service in the background (detached mode) and create any other necessary containers, as specified in the docker-compose.yml file.

Note: The specific configuration options used in the traefik.toml file will depend on your specific needs and setup. You can find more information about how to configure Traefik in the Traefik documentation.