brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: docker compose exec with index picks one container of a scaled service

Docker
~/tips/tip-docker-compose-exec-index

docker compose exec --index 2 worker sh

Open a shell in the second or third copy of a scaled Compose service.

After docker compose up --scale worker=3 there are three worker containers, and docker compose exec worker only ever lands in the first. The --index option chooses which one.

docker compose ps worker
docker compose exec --index 2 worker sh

The containers are numbered from 1, and the number is the suffix you see in their names, such as myproject-worker-2. So the first command shows which copies exist, and the second opens a shell in copy number 2.

I find it most useful when one copy behaves differently: it logs errors the others do not, or it is stuck on a job. Combine it with docker compose logs and the container name from ps to find the odd one out first.

The same --index option exists on docker compose cp, docker compose logs and docker compose port.

The numbers are not a stable identity. Scaling down removes copies and scaling up adds new ones, so look at docker compose ps each time rather than assuming that index 2 is still the container you were looking at yesterday.

Further reading: docker compose exec

More Docker tips ONBUILD RUN /usr/local/bin/python-build --dir /app/src SHELL ["/bin/bash", "-c"] docker commit <container> myname/testimage:version3 docker import /path/to/exampleimage.tgz docker compose events All Docker tips (192) All topics

Link