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 wait blocks until a container stops and prints its exit code

Docker
~/tips/tip-docker-wait

docker wait nightly-job

Waits for a container to stop, then prints its exit code, which is handy in scripts.

When a script starts a container that does a job and then exits, I want to know how it ended without parsing docker ps. docker wait does only that.

The command blocks until the containers you name have stopped, then prints one exit code per container.

docker run -d --name nightly-job example/job:1.0
docker wait nightly-job

The second line just sits there while the job runs. When the container stops it prints something like 0 and returns. Because the exit code is also the output, it drops straight into a script:

code=$(docker wait nightly-job)
if [ "$code" -ne 0 ]; then
  docker logs --tail 50 nightly-job
fi

If the container is already stopped, the command returns immediately with its exit code, so there is no race when the job finishes before you get to the wait line. You can give it several names and get one code for each.

It waits for as long as the container runs, so for a job that might hang, put a limit around it, for example timeout 600 docker wait nightly-job, or your script sits there forever.

Further reading: docker container wait

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