▲Wirig Diagram Hot
▲ ▲ ▼

Traefik Docker Compose Labels Example

By

Traefik is a popular reverse proxy and load balancer that makes publishing your applications to the web incredibly easy. When using Docker Compose, Traefik can be integrated seamlessly to provide automatic HTTP and websocket routing. Labels play a crucial role in this integration, helping Traefik discover and configure your services. Let's delve into examples of Traefik Docker Compose labels to illustrate how to leverage this powerful feature.

Traefik uses labels to determine which services to proxy and how to configure them. These labels, when added to your Docker Compose file, enable Traefik to dynamically update its configuration based on the services defined. This results in a robust, scalable, and maintainable infrastructure.

Traefik Docker Compose Labels: Basic Setup and Discovery

Before exploring specific label examples, let's establish a basic Traefik setup within a Docker Compose file. To enable Traefik's auto-discovery, add the following labels to your Traefik service:

the docker compose diagram is shown in blue and white, with icons on it
the docker compose diagram is shown in blue and white, with icons on it
Docker Compose Cheat Sheet: Essential YAML Concepts
Docker Compose Cheat Sheet: Essential YAML Concepts
the docker containers info sheet
the docker containers info sheet

`traefik.enable: "true"` ensures that Traefik considers the service for configuration. `traefik.http.routes.some-route.entrypoints: "http"` and `traefik.http.services.some-service.loadbalancer.server.port: "80"` define the entrypoint and target port for the service.

Label for enabling Traefik

The `traefik.enable` label determines whether a service should be exposed by Traefik. Setting it to `"true"` enables Traefik to discover and configure the service.

an image of a computer screen with the text example docker compose
an image of a computer screen with the text example docker compose
a blue box with a ship drawn on it
a blue box with a ship drawn on it
a table with two different types of words in each language and the same type of text below it
a table with two different types of words in each language and the same type of text below it

Example: `traefik.enable: "true"`

Labels for defining entrypoints and target ports

Entrypoints define how Traefik exposes your services. The `traefik.http.routes some-route.entrypoints: "http"` label configures an HTTP entrypoint for your service. The `traefik.http.services some-service.loadbalancer.server.port: "80"` label specifies the target port that Traefik should use to communicate with your service.

Example: ```yaml traefik.http.routes: some-route: entrypoints: "http" traefik.http.services: some-service: loadbalancer: server: port: "80" ```

30 DOCKER COMMANDS EVERY DEVELOPER SHOULD KNOW
30 DOCKER COMMANDS EVERY DEVELOPER SHOULD KNOW
Docker cheat sheet
Docker cheat sheet
the docker logo with a whale on it's back and an image of a ship
the docker logo with a whale on it's back and an image of a ship
Docker Cheat Sheet - BEST Version - Complete and Concise
Docker Cheat Sheet - BEST Version - Complete and Concise
Docker-Swarm-Logo
Docker-Swarm-Logo
Docker Cheat Sheet for Beginners | Essential Docker Command
Docker Cheat Sheet for Beginners | Essential Docker Command

Traefik Docker Compose Labels: Routing and Middleware

Traefik labels allow you to configure routing rules and middleware for your services, providing fine-grained control over request handling and flow.

Let's explore some examples of Traefik labels for routing and middleware:

Labels for defining paths and hosts

To configure routing rules, use the `traefik.http.routes` label to define the path and `traefik.http.routers` label to specify the host and entrypoints. This allows you to route incoming requests to specific services based on the requested path and host.

Docker
Docker
Cómo crear imágenes Dockerfile personalizadas paso a paso
Cómo crear imágenes Dockerfile personalizadas paso a paso
Docker Ecosystem Explained: Containers, Images, Volumes & More
Docker Ecosystem Explained: Containers, Images, Volumes & More

Example: ```yaml traefik.http.routes: direct: path: "/api" traefik.http.routers: api-route: rule: "Host(`api.example.com`) && PathPrefix(`/api`)" service: "api-service" entrypoints: ["http"] ```

Labels for configuring middleware

Middleware allows you to intercept and modify incoming requests, enabling functionality like authentication, rate limiting, and request modification. To use middleware, define it with the `traefik.http.middlewares` label and associate it with your router using the `traefik.http.routers.some-router.middlewares` label.

Example: ```yaml traefik.http.middlewares: example-auth: basicauth: users: - "user:pass" traefik.http.routers: api-route: middlewares: ["example-auth"] ```

Final paragraph: Understanding and leveraging Traefik's Docker Compose labels opens up a world of possibilities for configuring and managing your applications' networking. By harnessing the power of labels like `traefik.enable`, routing rules, and middleware, you can create robust, scalable, and secure services that seamlessly integrate with Traefik. So, dive in, experiment, and unlock the full potential of Traefik in your Docker Compose projects.

💬 Comments ↗ Share ★ Save
AD