Reputation: 17210
I have a program run in a docker container with detached mode.
So how to send a signal such as SIGINT to this program?
Upvotes: 54
Views: 46444
Reputation: 1788
When working with compose
this was useful for me:
docker compose kill -s <SIGNAL> <compose service name>
This will send specified signal to container's root process.
For example, I had a problem with reloading gunicorn WSGI server process inside the container. docker compose kill -s HUP <my service name>
solved the issue.
Upvotes: 1
Reputation: 38237
You can use docker kill --signal="<signal>" <container name or id>
to send any signal to the root process of a given container.
See https://docs.docker.com/engine/reference/commandline/kill/#send-a-custom-signal--to-a-container
Upvotes: 110
Reputation: 463
I managed to send a signal I want to a process(program) in docker container by:
Upvotes: 5
Reputation: 388
docker kill
used to send signal to main container process i.e process with PID 1.$ docker kill --signal="SIGTERM" container-id/name
Dockerfile
. (Update it as per the application)FROM centos:6.7
# Install/Deploye the service below.
# Copy the shell script.
COPY entrypoint.sh /home
EXPOSE 8080
ENTRYPOINT ["/home/entrypoint.sh"]
Below is the entrypoint.sh
. (Update it it as per the application). Suppose we wants to restart a init.d
service.
#start the service
/etc/init.d/<servicename> start
pid="$!"
# SIGUSR1- Single handler
my_handler() {
/etc/init.d/<servicename> restart
}
# Trap and handle the user defind singnal.
trap 'my_handler' SIGUSR1
# wait forever(Alive container.)
while true
do
tail -f /dev/null & wait ${!}
done
$docker kill --signal="SIGUSR1" container-id/name
Upvotes: 10
Reputation: 8791
You can use nsenter to get into your container space and send your signal.
PID=$(docker inspect --format {{.State.Pid}} <container_name_or_ID>)
nsenter --target $PID --mount --uts --ipc --net --pid kill -SIGINT <PID of your program inside your container>
More info : http://jpetazzo.github.io/2014/06/23/docker-ssh-considered-evil/
Upvotes: 14