traefik use label data of service container in compose yaml to route
example
https://doc.traefik.io/traefik/user-guides/docker-compose/basic-example/
traefik通过docker engine访问其它容器的label信息,来做路由。
version: "3.3"
services:
traefik:
image: "traefik:v3.2"
container_name: "traefik"
command:
#- "--log.level=DEBUG"
- "--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: "traefik/whoami"
container_name: "simple-service"
labels:
- "traefik.enable=true"
- "traefik.http.routers.whoami.rule=Host(`whoami.localhost`)"
- "traefik.http.routers.whoami.entrypoints=web"
Fourth, you allow Traefik to gather configuration from Docker:
traefik: command: # Enabling Docker provider - "--providers.docker=true" # Do not expose containers unless explicitly told so - "--providers.docker.exposedbydefault=false" volumes: - "/var/run/docker.sock:/var/run/docker.sock:ro" whoami: labels: # Explicitly tell Traefik to expose this container - "traefik.enable=true" # The domain the service will respond to - "traefik.http.routers.whoami.rule=Host(`whoami.localhost`)" # Allow request only from the predefined entry point named "web" - "traefik.http.routers.whoami.entrypoints=web"
Routing Configuration with Labels
https://doc.traefik.io/traefik/providers/docker/#routing-configuration-with-labels
By default, Traefik watches for container level labels on a standalone Docker Engine.
When using Docker Compose, labels are specified by the directive
labels
from the "services" objects.
Docker object labels
https://docs.docker.com/engine/manage-resources/labels/
Labels are a mechanism for applying metadata to Docker objects, including:
- Images
- Containers
- Local daemons
- Volumes
- Networks
- Swarm nodes
- Swarm services
You can use labels to organize your images, record licensing information, annotate relationships between containers, volumes, and networks, or in any way that makes sense for your business or application.
Label keys and values
A label is a key-value pair, stored as a string. You can specify multiple labels for an object, but each key must be unique within an object. If the same key is given multiple values, the most-recently-written value overwrites all previous values.
Key format recommendations
A label key is the left-hand side of the key-value pair. Keys are alphanumeric strings which may contain periods (
.
), underscores (_
), slashes (/
), and hyphens (-
). Most Docker users use images created by other organizations, and the following guidelines help to prevent inadvertent duplication of labels across objects, especially if you plan to use labels as a mechanism for automation.
Authors of third-party tools should prefix each label key with the reverse DNS notation of a domain they own, such as
com.example.some-label
.Don't use a domain in your label key without the domain owner's permission.
The
com.docker.*
,io.docker.*
, andorg.dockerproject.*
namespaces are reserved by Docker for internal use.Label keys should begin and end with a lower-case letter and should only contain lower-case alphanumeric characters, the period character (
.
), and the hyphen character (-
). Consecutive periods or hyphens aren't allowed.The period character (
.
) separates namespace "fields". Label keys without namespaces are reserved for CLI use, allowing users of the CLI to interactively label Docker objects using shorter typing-friendly strings.These guidelines aren't currently enforced and additional guidelines may apply to specific use cases.
Value guidelines
Label values can contain any data type that can be represented as a string, including (but not limited to) JSON, XML, CSV, or YAML. The only requirement is that the value be serialized to a string first, using a mechanism specific to the type of structure. For instance, to serialize JSON into a string, you might use the
JSON.stringify()
JavaScript method.Since Docker doesn't deserialize the value, you can't treat a JSON or XML document as a nested structure when querying or filtering by label value unless you build this functionality into third-party tooling.
Manage labels on objects
Each type of object with support for labels has mechanisms for adding and managing them and using them as they relate to that type of object. These links provide a good place to start learning about how you can use labels in your Docker deployments.
Labels on images, containers, local daemons, volumes, and networks are static for the lifetime of the object. To change these labels you must recreate the object. Labels on Swarm nodes and services can be updated dynamically.
Images and containers
Local Docker daemons
Volumes
Networks
Swarm nodes
Swarm services
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
2017-01-05 由LazyMan联想到的