GitProphet
GitProphet

Reputation: 1038

bash script command "trap" doesn't work in docker container

I'm trying to gracefully shutdown a service that I run in a docker container (on a raspberry pi 3 b running raspbian lite), since just issuing 'docker stop' breaks the service (a lock file does not get cleared)

My internet research has shown people working with the bash command 'trap' to execute a command when the script receives a signal to terminate.

I have tried this with the following:

docker run --rm -it --name trap resin/rpi-raspbian:jessie /bin/bash -c " trap 'echo shutting down ; exit' INT; echo waiting; read"

When I run above command, 'waiting' is being printed to the screen and when i hit CTRL+C, the message 'shutting down' appears. - Exactly as I want it!

The only problem is, that I can't seem to get that signal sent to the bash script from outside the docker container. I've tried (in a parallel ssh session):

docker kill --signal="INT" trap
docker kill --signal="SIGINT" trap

Furthermore, I tried to use the default signal that is being sent by 'docker stop', but that resulted in the console waiting ~10s (default docker stop timeout) and then killing the docker container.

docker stop trap

Neither methods got the message 'shutting down' to print onto the screen.

I'd really appreciate any help.

Edit 1: It doesn't work with SIGTERM either (when I either of the following commands and then issue a 'docker stop' I still don't get the message 'shutting down').

docker run --rm -it --name trap resin/rpi-raspbian:jessie /bin/bash -c " trap 'echo shutting down ; exit' TERM; echo waiting; read"
docker run --rm -it --name trap resin/rpi-raspbian:jessie /bin/bash -c " trap 'echo shutting down ; exit' SIGTERM; echo waiting; read"

Workaround: The image seems to be the problem. The entrypoint '/usr/bin/entry.sh' traps the signal. I've tried it with another image and it worked:

$ docker run -it --name trap digitallyseamless/archlinux-armv6h /bin/bash -c " trap 'echo shutting down ; exit 0' SIGTERM; echo waiting; read"

Stop the container using (from another terminal)

$ docker stop trap

Then view the container output

$ docker logs trap
waiting
shutting down

As I have no particular need to use a specific image (it really is just a means to get me to my goal), I'll just swap out images.

PS: Huge thanks to @tkausl

Upvotes: 3

Views: 3948

Answers (2)

tkausl
tkausl

Reputation: 14269

As I mentioned in the comments, docker stop sends a SIGTERM. According to your edit, this doesn't work for you, however, I just tried it with exactly the same command (changed the image to ubuntu though) and it works. Are you probably detaching from the running box? In this case, you obviously don't see the message, remove the --rm flag and take a look at the docker logs.

How I did it:

In one terminal:

docker run --rm -it --name trap ubuntu /bin/bash -c " trap 'echo shutting down ; exit' SIGTERM; echo waiting; read"

In a second terminal:

$ docker stop f6
f6

What I see in the first terminal after stopping:

$ docker run --rm -it --name trap ubuntu /bin/bash -c " trap 'echo shutting down ; exit' SIGTERM; echo waiting; read"
waiting
shutting down    
$

Upvotes: 1

Titouan Freville
Titouan Freville

Reputation: 477

Docker stop and docker kill are sending a signal and not the trap command. You have to make a docker exec container_name trap

Upvotes: 0

Related Questions