Sometimes it’s useful to send a signal from one container to another. In a previous post, I showed how to run confd and nginx in separate containers. In that example, the confd container used the docker client to send a HUP to nginx.
To do this, the confd container had the full docker installation script run on it. That works, but adds a lot of needless bulk. You can also run into problems if the docker client you install is newer than the server you’re pointing it at.
But there’s an easier way – we can just make docker API calls using HTTP through its unix domain socket.
Step 1 – share /var/run/docker.sock from the host into the container
The socket we need is in /var/run/docker.sock
, and we can share that when we launch the container with docker run -v /var/run/docker.sock:/var/run/docker.sock ...
Step 2 – send HTTP through the socket
Here’s a handy gist by Everton Ribeiro which shows various ways of doing this. Also note that the latest release of curl (7.40) has support for using a unix domain socket too.
I used netcat – here’s a simple example which should produce a result….
echo -e "GET /images/json HTTP/1.0\r\n" | nc -U /var/run/docker.sock
Check out the docker API documentation for more calls you can make. For example, let’s see how we can send a signal to another container.
echo -e "POST /containers/nginx/kill?signal=HUP HTTP/1.0\r\n" | \ nc -U /var/run/docker.sock
Brilliant! We can communicate with docker and we didn’t need to install anything else to do it!