45

I want to add/remove servers in my nginx running inside a docker container

I use ADD command in Dockerfile to add my nginx.conf to /etc/nginx dir.

# Copy a configuration file from the current directory
ADD nginx.conf /etc/nginx/

then in my running nginx container that have a conf like this

# List of application servers
upstream app_servers {
    server 172.17.0.91:9000;
    server 172.17.0.92:9000;
    server 172.17.0.93:9000;
}

how do restart my nginx to take effect of the edited nginx.conf?

thank you in advance!

Hokutosei
  • 2,114
  • 5
  • 24
  • 42
  • 1
    since you are modifying your Dockerfile, you need to rebuild the docker image and thus run a new container from that image. There is no way that the nginx process which is in an obsolete container can be restarted to read a new configuration file it does not have. – Thomasleveil Oct 10 '14 at 08:01
  • I guess i really did not understand docker very well. I thought, external scripts, when updated, I'll just do docker restart or something else to take effect. its kinda inconvenient rebuilding the image again. because i had other scripts that modify the nginx config. I guess, i'll quit using nginx docker. but hey, I use docker heavily to other apps. thanks for the answer. – Hokutosei Oct 10 '14 at 13:16
  • In conjunction with Dean Armada's great answer below, I'd suggest you look into mounting a volume with the conf.d to persist it either from the docker host, a shared drive, or even just an s3 bucket. Then it's easy to edit, and a soft reload gets it going. Also, if unsure, test it prior to reload to avoid down time! – Evan Morrison Nov 05 '18 at 03:44

6 Answers6

120

restarting the container is not advisable when you initialize Docker Swarm because it may remove the nginx service. So if you need an alternative aside docker restart; You can go inside the container and just run nginx -s reload

For example, in docker env, if you have the container named nginx

docker exec <nginx_container_id> nginx -s reload
Dean Christian Armada
  • 6,724
  • 9
  • 67
  • 116
13

to reload the NGINX configuration run the command

docker kill -s HUP container_name

https://blog.docker.com/2015/04/tips-for-deploying-nginx-official-image-with-docker/

smallfish
  • 155
  • 1
  • 3
  • FYI, this may work for some, but it didn't work for me. It just simply killed the container. I had to do `docker start ` afterwards to bring it back up – jlee Jan 11 '17 at 01:45
  • @jlee Set a [restart policy](https://docs.docker.com/engine/reference/run/#restart-policies---restart). – Greg Schmit Nov 12 '19 at 02:50
7

Simple way to Reload Nginx in Docker container

docker exec -it {container_name} nginx -s reload

Using PowerShell enter image description here

Thirsty Six
  • 399
  • 1
  • 6
  • 13
5

If you want to restart the NGINX process, restart the container by running the command:

docker restart <container name>

https://blog.docker.com/2015/04/tips-for-deploying-nginx-official-image-with-docker/

WCO
  • 480
  • 5
  • 8
  • Not sure, but sometimes when these containers are associated with cloudfront, the cloudfront registry creates some issues, and the container itself disappears. – Anand Vaidya Apr 22 '18 at 17:22
1

If you want to do this from within a container, you will need service/configuration discovery. Tools like coreOS/etcd and Apache Zookeeper are made to facilitate this process. Fair warning: for simple applications, these can be a bit difficult to manage.

There is also docker-gen, which is somewhat easier to get going. There is even a specific pre-made script for exactly the scenario you are describing.

naneau
  • 489
  • 2
  • 4
  • thanks nanaeu, ill give it a shot now. ill post back, what are my results. if this works, i might change use this in production. as of now, we just have containers, nginx on bare, and varnish. testing nginx containers on test machines of course. thanks again for the suggestion! – Hokutosei Oct 10 '14 at 17:15
0

All answers are correct, but just to clarify that there are a number of different ways of achieving the same result. For example

  1. docker kill -s SIGHUP container_name
  2. docker run nginx_image nginx -s reload
  3. docker exec -t container_name nginx -s reload

Docker-Compose works pretty much the same:

docker-compose kill -s SIGHUP container_name
Skeptic
  • 1,254
  • 14
  • 18