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 run --init gives your container a proper PID 1

Docker
~/tips/tip-docker-run-init

docker run --init --rm -it my-image

Adds a small init process that forwards signals and cleans up zombie processes.

Some containers take ten seconds to stop and then get killed anyway. A common cause is that the app runs as PID 1 inside the container and was never written to be an init process. It doesn’t pass signals on and doesn’t clean up child processes that exit. Docker has a flag for exactly that.

docker run --init --rm -it my-image

The Docker reference describes --init in one line: run an init inside the container that forwards signals and reaps processes. So docker stop reaches your app, and zombie processes from shell scripts or child workers get cleaned up instead of piling up.

Example: a container that starts a shell script, which starts a Python worker. Without an init, the script is PID 1 and may never pass SIGTERM on to Python. With --init the tiny init process sits in front and does that work. In Compose, the service attribute is called init, so you don’t need to change the image.

I’d only add it where you see the symptom: slow stops, or defunct processes in docker top. Plenty of images already ship their own init process, so read the image docs first; a second one in front of that buys you nothing.

Caveat: --init fixes signal handling for the process tree. If the app itself ignores SIGTERM, it still gets killed after the stop timeout.

Further reading: Docker docs: docker container run, Docker docs: Compose services reference

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