Restart a Docker service when config changes
How would you restart a service, say for example ‘nginx‘ when a config file changes? For example I’ve got Puppet creating some nginx cfg files and place them on a volume which is mounted to my nginx container. At the moment I am using docker-gen, but are there any other methods?
One Solution collect form web for “Restart a Docker service when config changes”
Docker containers are meant to be ephemeral. Also, Docker containers “containerize” whatever process you are running by making that process PID 1 inside your container. That means there is no traditional init system. In fact, no init system at all. And as you know, when the process inside your container exits, the container dies. So if we approach the problem from the standpoint of implementing ephemeral containers, you don’t restart your service. You create a new container using your modified configuration. And as mentioned in the comments by thaJeztah, you can
docker restart nginx your container to refresh the configuration.
Now, there are a couple of ways to hammer this square peg into a round hole. You are better than that… However, You’ve already noticed that
docker-gen will get you nearly there. Likewise, if you take a dive into how the
jwilder/nginx-proxy image works, you’ll get a better idea of how docker-gen works in practice. But you’ve probably already seen that, since you’re already using
The other option is to shoehorn in something like
supervisord. There is plenty of information about doing that online. Tons of people have done this in the past. And so for other people that may not understand why that solves the problem, supervisord becomes your container’s PID 1, and allows you to restart the child nginx processes “like normal”, but without killing your container.