WoJ
WoJ

Reputation: 30007

How to make traefik automatically assign to one frontend docker backends from the same image?

I am starting to convert all my systemd-nspawn containers to a docker and traefik solution and I managed to have a frontend created automatically once a docker container (the backend) is started.

I would like to have several containers started from the same docker image (each with an auto-created name) to be attached to a frontend which name I would set. Is such a setup possible?

In other words, today once I run

# docker run myimage
# docker run myimage
# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
5b4af3466ba6        wazaa               "/bin/sh -c 'python …"   About an hour ago   Up About an hour    5000/tcp            cranky_montalcini
59b1be83bf98        wazaa               "/bin/sh -c 'python …"   About an hour ago   Up About an hour    5000/tcp            inspiring_goldwasser

I get two containers (cranky-montalcini and inspiring-goldwasser per the screenshot below and docker ps output above) which have two frontends assigned. I, on the other hand, would like to have them assigned to a single myfrontend.example.com which would somehow recognize that containers spawned from myimage are for him.

enter image description here

Upvotes: 1

Views: 140

Answers (1)

larsks
larsks

Reputation: 311740

I'm not entirely clear what you're asking, so my apologies if this answer doesn't quite hit the mark. You should only have one "frontend" (which is your traefik container). Traefik maps new containers to backends based on their name. If you want two containers with different names to be considered as part of the same backend (so that requests directed to that backend will round-robin between them), you can set the traefik.backend label. For example, this would make the following two containers members of the foo backend:

docker run --label traefik.backend=foo some_image
docker run --label traefik.backend=foo another_image

This assumes, of course, that you have also configured an appropriate traefik.frontend.rule that would direct traffic to this particular backend.

See https://docs.traefik.io/configuration/backends/docker/ for additional information.

Upvotes: 1

Related Questions