Nicolas Talichet
Nicolas Talichet

Reputation: 311

Communication between two containers on the same host

The idea is simple, I need to send a signal from a container to another one to restart nginx.

Connect to the nginx container from the first one in ssh is a good solution?

Do you have other recommended ways for this?

Upvotes: 1

Views: 398

Answers (1)

Farhad Farahi
Farhad Farahi

Reputation: 39247

I don't recommend installing ssh, Docker containers are not virtual machines, And should respect microservices architecture to benefit from many advantages it provides.

In order to send signal from one container to another, You can use docker API.

First you need to share /var/run/docker.sock between required containers.

docker run -d --name control -v /var/run/docker.sock:/var/run/docker.sock <Control Container>

to send signal to a container named nginx you can do the following:

echo -e "POST /containers/nginx/kill?signal=HUP HTTP/1.0\r\n" | \
nc -U /var/run/docker.sock

Another option is using a custom image, with a custom script, that checks nginx config files and if the hash is changed sends reload signal. This way, each time you change config, nginx will automatically reload, or You can reload manually using comments. these kind of scripts are common among kubernetes users. Following is an example:

nginx "$@"
oldcksum=`cksum /etc/nginx/conf.d/default.conf`

inotifywait -e modify,move,create,delete -mr --timefmt '%d/%m/%y %H:%M' --format '%T' \
/etc/nginx/conf.d/ | while read date time; do

    newcksum=`cksum /etc/nginx/conf.d/default.conf`
    if [ "$newcksum" != "$oldcksum" ]; then
        echo "At ${time} on ${date}, config file update detected."
        oldcksum=$newcksum
        nginx -s reload
    fi

done

Don't forget to install inotifywait package.

Upvotes: 4

Related Questions