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