Quick Start

A Use Case Using Docker

quickstart-diagram

Launch Traefik With the Docker Provider

Create a docker-compose.yml file where you will define a reverse-proxy service that uses the official Traefik image:

  1. version: '3'
  2. services:
  3. reverse-proxy:
  4. # The official v3 Traefik docker image
  5. image: traefik:v3.1
  6. # Enables the web UI and tells Traefik to listen to docker
  7. command: --api.insecure=true --providers.docker
  8. ports:
  9. # The HTTP port
  10. - "80:80"
  11. # The Web UI (enabled by --api.insecure=true)
  12. - "8080:8080"
  13. volumes:
  14. # So that Traefik can listen to the Docker events
  15. - /var/run/docker.sock:/var/run/docker.sock

That’s it. Now you can launch Traefik!

Start your reverse-proxy with the following command:

  1. docker-compose up -d reverse-proxy

You can open a browser and go to http://localhost:8080/api/rawdata to see Traefik’s API rawdata (you’ll go back there once you have launched a service in step 2).

Traefik Detects New Services and Creates the Route for You

Now that you have a Traefik instance up and running, you will deploy new services.

Edit your docker-compose.yml file and add the following at the end of your file.

  1. version: '3'
  2. services:
  3. ...
  4. whoami:
  5. # A container that exposes an API to show its IP address
  6. image: traefik/whoami
  7. labels:
  8. - "traefik.http.routers.whoami.rule=Host(`whoami.docker.localhost`)"

The above defines whoami: a web service that outputs information about the machine it is deployed on (its IP address, host, and others).

Start the whoami service with the following command:

  1. docker-compose up -d whoami

Browse http://localhost:8080/api/rawdata and see that Traefik has automatically detected the new container and updated its own configuration.

When Traefik detects new services, it creates the corresponding routes, so you can call them … let’s see! (Here, you’re using curl)

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

Shows the following output:

  1. Hostname: a656c8ddca6c
  2. IP: 172.27.0.3
  3. #...

More Instances? Traefik Load Balances Them

Run more instances of your whoami service with the following command:

  1. docker-compose up -d --scale whoami=2

Browse to http://localhost:8080/api/rawdata and see that Traefik has automatically detected the new instance of the container.

Finally, see that Traefik load-balances between the two instances of your service by running the following command twice:

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

The output will show alternatively one of the following:

  1. Hostname: a656c8ddca6c
  2. IP: 172.27.0.3
  3. #...
  1. Hostname: s458f154e1f1
  2. IP: 172.27.0.4
  3. # ...

Where to Go Next?

Now that you have a basic understanding of how Traefik can automatically create the routes to your services and load balance them, it is time to dive into the user guides and the documentation and let Traefik work for you!


Using Traefik OSS in Production? Consider Adding Advanced Capabilities.

Add API Gateway or API Management capabilities seamlessly to your existing Traefik deployments. No rip and replace. No learning curve.