8.3: Docker Labels: “Tagging” your containers so they appear on the web

DockerTraefik automatically detects your containers using labels. These labels allow you to:

  • Tell Traefik that a container should be exposed to the web.
  • Define routing rules (domain name, entrypoint, etc.)

For this step, we’ll use the Apache / PHP-FPM / MariaDB stack that we’ve already deployed.

The external network

In our configuration, Traefik is deployed in a separate stack. To make your application containers visible to Traefik:

  1. We create an externalproxy-traefik networkDocker (already done in the previous section).
  2. Each application stack must join this external network.

Example for our PHP/Apache stack:

networks:
  proxy-traefik:
    external: true
Cela permet Γ  Traefik de communiquer avec les conteneurs sans que leurs ports internes soient exposΓ©s directement Γ  Internet.

Example labels for a web container

In the Apache or PHP-FPM service:

services:
web:
image: php:8.2-apache
container_name: monsite-web
restart: unless-stopped
networks:
- proxy-traefik
labels:
- "traefik.enable=true" # On active Traefik sur ce conteneur
- "traefik.http.routers.monsite.rule=Host(`monsite.local`)" # Domaine Γ  router
- "traefik.http.routers.monsite.entrypoints=web" # Entrypoint dΓ©fini dans Traefik

Here, Traefik automatically detects the container and knows it must route traffic to monsite.local.
Labels are simple metadata used to β€œtag” the container for Traefik.

Simplified flow diagram

        Internet
           |
        Traefik (stack sΓ©parΓ©)
           |
           |-- proxy-traefik (rΓ©seau externe)
           |
  -------------------------------
  |        |                  |
 Apache   PHP-FPM            MariaDB
 (web)    (backend)          (bdd)
  • proxy-traefikThe web and backend containers are connected to the external network.
  • Traefik can thus route traffic to the correct container using the labels.
  • The containers do not expose their ports directly to the Internet, which secures the stack.

πŸ’‘ Tip:

  • Labels do not modify the container; they are used solely to inform Traefik.
  • You can add as many labels as needed to define more complex rules (HTTPS, redirects, middleware, etc.)