restart nginx container when upstream servers is u

2020-05-20 07:03发布

问题:

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!

回答1:

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/



回答2:

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


回答3:

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/



回答4:

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.



标签: nginx docker