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.
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.
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.
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.
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!
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:
--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.The good practice is to combine both methods.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With