How does Docker stop containers on Windows?

I’m using Docker (Docker version 1.12.0, build 8eab29e) on Server 2016 TP5, and I cannot figure out how I can gracefully shut down a container. On Linux, “docker stop” will send SIGTERM to the process, and after 10 (by default) seconds, will send SIGKILL. On Windows, however, this must be different.

I have tried SetConsoleCtrlHandler, but my handler was not called.

I have tried calling signal(…) from MSVCRT, but it seems SIGTERM isn’t being received.

How can I detect that Docker wants to stop my application, so I can gracefully shut down?

1 Like

Have you tried to get this working outside of a container? The process running on the Windows Docker container should see the behavior as a normal Windows process (I don’t actually know exactly what that is).

The code is here:

Well, that’s exactly the question. What a “normal Windows process” sees depends entirely on how it is terminated. Just as a Linux process might receive SIGTERM, or SIGKILL, there are several ways to end a Windows process. If it has a message loop, you might send a WM_CLOSE message, or if the process has a stdio handle, you might send CTRL-C to it. You might simulate an OS shutdown, which would result in a different message being received. From my experimentation, it seems that “docker stop” is doing essentially a TerminateProcess, or the Windows equivalent of SIGKILL. This is bad.

The best discussion I found was here, but the real question, then, is “what is behind hcsshim.ShutdownComputeSystem”? Apparently (from zhcsshim.go) it’s calling “ShutdownComputeSystem” from vmcompute.dll, but that doesn’t help much.

That’s calling the Windows containerization APIs. You can open an issue on docker/docker and ping John Howard if you have ideas for improvements.

I believe that the code that ultimately gets called is MS code here. I’ll open an issue and ping John. Thanks for the pointer, I appreciate it.

Hi. Was this question / issue resolved? I’m interested in running my console app in a Docker container (Windows Containers) and it seems (according to console output - docker logs) that application is terminated or stopped somehow (docker stop -t 120) but I need to emulate Ctrl-C / Ctrl-Break for graceful shutdown (while Docker for Windows Containers doesn’t support STOPSIGNAL directive in Dockerfile - it explicitly refuses to build images with STOPSIGNAL and states that this feature is not supported).

As far as I can tell, with the current (release) version, the process is
stopped using TerminateProcess().

has this issue been resolved meanwhile? I’m developing service that needs to perform cleanup tasks on network during shutdown, so would be great to have a way to get notified for container stop event in order to perform graceful shutdown.


Not as far as I can tell. The docker issue is here, and one PR was merged in, but it’s still waiting for “platform changes”. There are rumours that Server 2016 RS2 won’t be released, and if this got pushed to RS3, we could be waiting until fall 2017 for the fix. Sadface.