Server Not Shutting Down Gracefully

Learn the ways to shut down the server gracefully.

Compose intermittently aborts with Ctrl+C

When you start your application with Compose in the default, attached mode-in other words, without the -d option, Compose connects to each container’s stdout, tailing the output.

When you press Ctrl+C, Compose is supposed to instruct the containers to terminate by sending the main process the SIGTERM signal. The process should exit gracefully and then the container should terminate. When this happens correctly, the Compose output on pressing Ctrl+C is:

Killing myapp_web_1 ... done
Gracefully stopping... (press Ctrl+C again to force)

However, instead of the containers shutting down gracefully, we get the following error and the termination fails, leaving the containers still running:

^CERROR: Aborting.

Not good. Unfortunately, this seems to be a long-standing, known issue.

Why does the error occur?

It seems to be caused by a problem in PyInstaller, an open source tool for creating executables from Python scripts, which Compose relies on.

How to solve it

The issue is an irritation rather than a showstopper. We can manually shut down the containers by issuing a docker-compose stop (or kill) command. However, although it seems to be an issue with a third-party dependency, it cannot help but undermine our feeling of confidence in Compose itself, which is a shame.

So far any 100% workable workaround to prevent it is not found. If you find yourself affected, avoid starting your application in attached mode, and instead, always use detached mode with the -d option. To date, many haven’t experienced the issue with that.

Get hands-on with 1200+ tech skills courses.