Join our Discord Server
Ajeet Raina Ajeet Singh Raina is a former Docker Captain, Community Leader and Arm Ambassador. He is a founder of Collabnix blogging site and has authored more than 570+ blogs on Docker, Kubernetes and Cloud-Native Technology. He runs a community Slack of 8900+ members and discord server close to 2200+ members. You can follow him on Twitter(@ajeetsraina).

How To Use Traefik v2 as a Reverse Proxy for Docker

3 min read

Traefik is a powerful and flexible tool for managing traffic and routing requests in modern cloud-native environments. It has become a popular choice for many organisations that are embracing microservices and containerisation.

Traefik is designed to work seamlessly with container orchestration systems like Docker and Kubernetes, and it has built-in support for multiple service discovery providers such as Docker, Consul, and Kubernetes. This means that traefik can automatically discover new services and route traffic to them without requiring any manual configuration.

In addition to its core reverse proxy and load balancing functionality, Traefik also includes features such as SSL termination, health checks, circuit breakers, and a web dashboard for monitoring and managing your infrastructure. It is also designed to be highly configurable and extensible, with a powerful plugin system that allows you to add custom functionality as needed.

For more information, check out proxy servers to know about the private proxies.

How does Traefik works?

Traefik is a reverse proxy that sits between client requests and the backend services that handle those requests. It works by inspecting incoming requests and using a set of rules to determine how to route the request to the appropriate backend service.

When a request comes in, Traefik first inspects the request and extracts information such as the URL, request headers, and other metadata. It then uses this information to determine which backend service should handle the request. Traefik’s routing rules can be based on various factors such as the hostname, path, or header values in the incoming request.

Once Traefik has determined the appropriate backend service, it forwards the request to that service and then passes the response back to the client. Traefik can also perform additional operations such as SSL termination, load balancing, and health checks on the backend services.

One of the key advantages of Traefik is its ability to automatically discover and route traffic to backend services without requiring manual configuration. This is achieved through Traefik’s support for service discovery providers such as Docker, Kubernetes, and Consul. When new services are added or removed, Traefik can automatically detect these changes and adjust its routing accordingly.

Overall, Traefik is a powerful and flexible tool that provides a wide range of features for managing traffic and routing requests in modern cloud-native environments. Its ability to automatically discover and route traffic to backend services makes it an attractive choice for organizations that are embracing microservices and containerization.

Getting Started

To set up Traefik on Docker Desktop, follow these steps:

Step 1. Install Docker Desktop on your machine

If you haven’t already, download and install Docker Desktop from the Docker website.

Step 2. Create a Docker network

You need to create a Docker network that Traefik can use to communicate with your other Docker containers. Open a terminal or command prompt and run the following command:

docker network create traefik

Step 3. Create a configuration file

You need to create a configuration file for Traefik that specifies how it should route traffic. Create a new file named traefik.yml and add the following configuration:

api:
  dashboard: true

entryPoints:
  http:
    address: ":80"

providers:
  docker:
    endpoint: "unix:///var/run/docker.sock"
    exposedByDefault: false

certificatesResolvers:
  letsencrypt:
    acme:
      email: <your-email>
      storage: /letsencrypt/acme.json
      httpChallenge:
        entryPoint: http

In this configuration file, we are setting up Traefik to listen on port 80 and to route traffic to Docker containers. We’re also setting up Let’s Encrypt to automatically issue SSL certificates for our domains.

Step 4. Run Traefik

You can now run Traefik using the following command:

docker run -d -p 80:80 -p 8080:8080 \
  -v /var/run/docker.sock:/var/run/docker.sock \
  -v $PWD/traefik.yml:/traefik.yml \
  -v $PWD/letsencrypt:/letsencrypt \
  --network traefik \
  --name traefik \
  traefik:v2.5

This command will start Traefik in a Docker container, mount the configuration file and Let’s Encrypt storage, and connect it to the traefik network.

Once Traefik is up and running, you should be able to access the Traefik dashboard by navigating to http://localhost:8080/dashboard/.

Step 5. Configure your applications

You can now configure your Docker applications to use Traefik as a reverse proxy. To do this, you need to add some labels to your application’s Docker Compose file:

labels:
  - "traefik.enable=true"
  - "traefik.http.routers.myapp.rule=Host(`myapp.local`)"
  - "traefik.http.services.myapp.loadbalancer.server.port=80"

In this example, we’re telling Traefik to route traffic to a container with the hostname myapp.local on port 80.

That’s it! With Traefik set up, you can easily add SSL certificates and route traffic to your Docker containers.

Here’s an example docker-compose.yml file that uses Traefik as a reverse proxy:

version: '3'

services:
  traefik:
    image: traefik:v2.5
    command:
      - "--api.insecure=true"
      - "--providers.docker=true"
      - "--providers.docker.exposedbydefault=false"
      - "--entrypoints.web.address=:80"
    ports:
      - "80:80"
      - "8080:8080"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro

  whoami:
    image: containous/whoami
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.whoami.rule=Host(`whoami.local`)"
      - "traefik.http.services.whoami.loadbalancer.server.port=80"

In this example, we’re defining two services: traefik and whoami. The traefik service is the Traefik reverse proxy itself, and the whoami service is a simple HTTP server that will be routed by Traefik.

We’ve defined the traefik service with the necessary configuration to enable Docker provider and set the entrypoint to port 80. We’ve also mapped the necessary ports and volumes.

For the whoami service, we’ve added some Traefik-specific labels that instruct Traefik to route traffic to the whoami service using the hostname whoami.local.

With this configuration, if you run docker-compose up, you should be able to access the whoami service by navigating to http://whoami.local in your web browser. Traefik will automatically route the traffic to the whoami container. You can also access the Traefik dashboard at http://localhost:8080/dashboard/.

Image2
Image3

References:

Have Queries? Join https://launchpass.com/collabnix

Ajeet Raina Ajeet Singh Raina is a former Docker Captain, Community Leader and Arm Ambassador. He is a founder of Collabnix blogging site and has authored more than 570+ blogs on Docker, Kubernetes and Cloud-Native Technology. He runs a community Slack of 8900+ members and discord server close to 2200+ members. You can follow him on Twitter(@ajeetsraina).
Join our Discord Server
Index