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: stop_grace_period gives a slow container more than 10 seconds to shut down

Docker
~/tips/tip-docker-compose-stop-grace-period

services:

Lets a container finish its shutdown work before Docker kills it after the default 10 seconds.

When Docker stops a container it sends SIGTERM, waits, and then sends SIGKILL. That wait is 10 seconds by default. For a web app that’s plenty. For a database flushing to disk or an app finishing a big write, 10 seconds can be too short, and SIGKILL doesn’t negotiate. I’d much rather give those containers a longer goodbye.

In Compose you set it per service with stop_grace_period, written as a duration:

services:
  db:
    image: postgres:17
    stop_grace_period: 1m

Now docker compose stop and docker compose down give db a full minute after SIGTERM before Docker kills it. If the database is done after three seconds, the container stops after three seconds; the grace period is a maximum, not a delay. For one-off stops you can override it on the command line:

docker compose stop -t 120 db
docker stop -t 120 db

Without Compose, docker run --stop-timeout 60 sets the same thing at creation time. On Unraid that flag can go in the Extra Parameters of a container template. If an app expects a different signal than SIGTERM, stop_signal next to it changes which one Docker sends first.

The caveat: a longer grace period only helps when the app actually reacts to the signal. If PID 1 in the container is a shell script that doesn’t pass SIGTERM on, the app never hears it, and you just wait the full minute before the same SIGKILL. In that case I’d fix the signal handling first (init: true helps with some images) and only then raise the timeout. Also keep in mind that every stack shutdown, including a host reboot, can now take as long as your slowest grace period.

Further reading: Compose specification: stop_grace_period, docker container stop 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