Docker: how to send a signal from one running container to another?

I use docker and I would like to know: is it possible to send a signal from a running container to another running container?

More precisely, I would like to send SIGUSR1 to a python program.

I already made a bash script to get the pid of my python process and send a signal:

send_signal.sh

#!/bin/bash py_pid=$(pidof -s python my_programme.py kill -SIGUSR1 $py_pid

Before that, I ran send_signal.sh from the Docker host as follows:

docker exec docker_with_python bash send_signal.sh

Or just like that:

docker kill --signal="SIGUSR1 docker_with_python

But now I would like to send a signal to the current container to another. So, how can I execute this command from another running container. Or is there another way to send a signal?

Thanks in advance

+4
source share
3 answers

This is the code I used. This may help someone else:

echo -e "POST /containers/<docker_id>/kill?signal=SIGUSR1 HTTP/1.0\r\n" |nc -U /tmp/docker.sock

docker-compose.yml :

exemple1 hostname: exemple_1 volumes: - /var/run/docker.sock:/tmp/docker.sock

+5

, Docker - , . ., , .

+3

You can do this in python by overriding the socket HTTPConnection

import socket
from http.client import HTTPConnection

f = '/var/run/docker.sock' # Or wherever you've mounted it to in the container
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.connect(f)

conn = HTTPConnection('notused')
conn.sock = s
conn.request('POST', '/containers/<docker_id>/kill?signal=SIGHUP')
resp = conn.getresponse()

print(resp.status)
print(resp.headers)
print(resp.read())

The advantage is that you can check the status of success (a 204). And if the status indicates an error, the response body will have an error message.

As indicated in the accepted answer, you will need to install the docker socket if you are doing this from a container: -v /var/run/docker.sock:/tmp/docker.sockand change the code to point to the right socket.

0
source

All Articles