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: add init: true when a container leaves zombie processes behind

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

docker run --init myimage

Runs a small init process first so signals are forwarded and finished child processes are cleaned up.

Most containers run their main program as PID 1. Linux treats PID 1 differently from other processes, and many programs were never written for it.

PID 1 is expected to forward signals and to clean up child processes that have exited. If your app spawns children and does neither, you get zombie processes and slow, ugly shutdowns. Docker can put a tiny init program in front of yours to do that job.

docker run --init myimage

In a compose file it is one line on the service:

services:
  worker:
    image: myimage
    init: true

The init binary is docker-init, which is based on tini, and it forwards signals and reaps the children. I would turn it on for anything that runs scripts, shells out a lot or starts browsers.

You can see the effect with docker top or ps inside the container: PID 1 is then the init, and your app is its child. It costs almost nothing, so I would not agonise over it.

The caveat: if the image already has its own init in the entrypoint, you do not need it twice. It also does not fix an app that ignores SIGTERM on its own, since it only makes sure PID 1 behaves.

Further reading: docker container run reference, Compose services reference: init, On this site: put optional services behind a Compose profile, On this site: docker compose watch syncs your code into a running container

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