szazs89 / docker-gen

docker-gen supporting services on different nodes

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

docker-gen with dynamic container names support running on multiple nodes

This is an enhancement to the docker-gen image that adds a script that can send a SIGHUP signal to a container running on a different node. either running inside the swarm or standalone. (This is a modification of helderco's docker-gen enhancement.)

The problem

Helder Correia's enhancement works through the use of the label added to the container to be restarted (e.g. running the nginx-proxy).

However, the container can be accessed on the same node or docker host, only. That is, this method does not work if the containers of the services behind the nginx-proxy are running on different docker hosts attached together with an overlay net of a swarm.

The solution

So, the docker-gen service is deployed in the swarm running one replica (container) on each node of the swarm.

The docker-gen.wrapper script

Generating separated default.conf by each replica of docker-gen

The controled /etc/nginx-proxy/conf.d folder is accessed through NFS. However, the controlling docker-gen exe generates different default.conf files depending upon the node which the container is running on. This distinction is made by adding the IP as an extension to the filename (e.g. default.conf.192.168.1.2). These files are updated when the script is invoked as given by the -notify parameter. (Without this modification, the default.conf file would contain only the services of the node where the last modification was detected through the docker socket.)

Furthermore, the command parameters of the compose file (used for deploying docker-gen) are the same on each node, therefore, a docker-gen.wrapper script is needed before the original docker-gen executable. This script determines the IP of the current node and modifies the passed variables with it (default.conf -> default.conf.$IP).

Exposing docker.sock on TCP port 2375

The wrapper script also determines whether the container of nginx-proxy is running on the current node. In this case it exposes the /tmp/docker.sock to the 2375 TCP port of the container which the docker daemon can be accessed through in order to restart the container of the nginx-proxy

fifo=/tmp/nginx-gen.fifo
mkfifo $fifo && cat $fifo | nc -U /tmp/docker.sock |nc -kl 0.0.0.0 2375 > $fifo &

Nice, isn't it? It is from man netcat ;-)

So, when a modification in the running containers is noticed on a node, docker-gen exe updates /etc/nginx-proxy/conf.d/default.conf.$IP using /etc/docker-gen/templates/nginx.tmpl template file and notifies the docker-merge-sighup script.

The docker-merge-sighup script

Merging the updated default.conf.$IP files

This script merges default.conf.* into default.conf. Since, there is The first n lines of each default.conf.$IP is common (header part). Its end is marked by the line containing the VIRTUAL HOST CONFIGS string in the template file. This is followed by the virtual host specific configuration of the detected services.

Thus, practically it is sufficient to copy the first file to default.conf and the lines of the others from the VIRTUAL HOST CONFIGS line to append to it.

Sending SIGHUP to the node where nginx-proxy is running

Finally, the script finds out the IP addresses of the nodes participating in the swarm (i.e. every node has to be a manager in order to be able to get such information).

After that it scans the port 2375 of the obtained IP addresses using netcat and determines the Id of the container which runs the nginx-proxy and sends the SIGHUP.

Installation

The local image can be built from the Dockerfile. It is recommended to use a local registry service.

docker build -t 127.0.0.1:5000/docker-gen:merge

A compose file (nginx-stack.yaml) can be generated by running the make_compose_file.sh script in the example folder. After that the nginx-proxy and the docker-gen services can be deployed into a stack:

docker stack deploy -c nginx-stack.yaml nginx-stack

The nginx-proxy can also be run in a standalone container (outside of the swarm). In this case the parameter of the docker-merge-sighup script must match on container_name (as for Swarm Mode, on the name of the service).

References:

Further similar projects:

About

docker-gen supporting services on different nodes

License:MIT License


Languages

Language:Shell 96.5%Language:Dockerfile 3.5%