Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to stop my Docker container with Ctrl-C

I'm using WSL2 on Windows 10 using an Ubuntu image, and Docker for Desktop Windows (2.2.2.0) with the WSL integration.

I have a Rust TCP server. When I run it with cargo run (or the binary after cargo install), it does the right thing, and I can send Ctrl-C to it to terminate. I don't do any explicit signal handling in the code.

I turned it into a Docker image. Here's the Dockerfile.

FROM rust:1.40 as builder
COPY . .
RUN cargo install --path . --root .

FROM debian:buster-slim
COPY --from=builder ./bin/myserver ./myserver
EXPOSE 8080
ENTRYPOINT ["./myserver"]

I then do:

docker build -t myserver .
docker run -it --rm -p 8080:8080 myserver

Attempting to Ctrl-C the process shows the ^C character in the terminal, but the signal doesn't seem to reach the process. I have to use docker kill. I've read other posts like this and this. It suggests that a combination of -it and using the array parameter version of ENTRYPOINT or CMD should allow the signal to reach it, however these don't seem to be helping me.

To see if it was something to do with my setup (Docker for Desktop, WSL, etc.) or my Dockerfile, I followed the README for docker-http-https-echo, and I'm able to Ctrl-C the process. Inspecting the Dockerfile doesn't show that it's doing anything different than me, but clearly I'm missing something.

like image 810
defect Avatar asked Mar 18 '20 04:03

defect


People also ask

How do you stop a docker container that won't stop?

docker rm -f The final option for stopping a running container is to use the --force or -f flag in conjunction with the docker rm command. Typically, docker rm is used to remove an already stopped container, but the use of the -f flag will cause it to first issue a SIGKILL.

How do I stop docker Ctrl-C?

In both cases, the docker container acts as if it simply ignores Ctrl-C. Starting with docker 0.6. 5 , you can add -t to the docker run command, which will attach a pseudo-TTY . Then you can type Control-C to detach from the container without terminating it.

How do I stop a docker container from running?

Note that pressing `Ctrl+C` when the terminal is attached to a container output causes the container to shut down. Use `Ctrl+PQ` in order to detach the terminal from container output.

How do I kill a docker container?

Kill All Containers Using Docker Compose If you do, killing multiple containers takes one command: docker-compose down. You could also run docker-compose without detached mode. If so, you'll just use ^C to kill all containers. And there you have it—all containers killed!


1 Answers

The reason for your problem is that the kernel treats a process with PID 1 specially, and does not, by default, kill the process when receiving the SIGTERM or SIGINT signals.

You have two options:

  1. Add --init flag to docker run command. By that a special process with PID 1 will be created, which will be a parent for your process and will proxy all signals and properly reap your processes.
  2. Add explicit signal handling to your app, which is good if you want to do graceful shutdown.

The good practice is to combine both methods.

like image 135
Nikscorp Avatar answered Sep 18 '22 15:09

Nikscorp