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.)