简体   繁体   English

如何在 Docker 容器中运行 Nginx 并将其设置为每 6 小时重新加载一次配置?

[英]How to run Nginx within a Docker container and set it up to reload its configuration every 6h?

I'm runnign NGINX in a Docker container, and I'm trying to find a way to start it up using docker run in a way that would order my NGINX to reload its configuration every 6h.我在 Docker 容器中运行 NGINX,我试图找到一种使用 docker docker run启动它的方法,这种方式会命令我的 NGINX 每 6 小时重新加载一次配置。 I need this to be done, because I also use Certbot in another container, and I'd like the SSL certificate renewal process to be automated and applied, and for that I neen NGINX to reload its configuration on schedule.我需要这样做,因为我也在另一个容器中使用 Certbot,并且我希望 SSL 证书更新过程能够自动化和应用,为此我需要 NGINX 按计划重新加载其配置。

At this point I use the folowing docker run sequence (more or less):在这一点上,我使用以下 docker docker run序列(或多或少):

docker run --restart=always -d -p 80:80 -p 443:443 \\ --name=nginx_RevPr nginx:latest \\ /bin/bash -c 'while :; do sleep 6h & wait ${!}; nginx -s reload; done & nginx -g "daemon off;"'

The container created this way is running and working fine.以这种方式创建的容器正在运行并且工作正常。 The only problem I have is that I actually don't know if NGINX is really realoading its configuration.我唯一的问题是我实际上不知道 NGINX 是否真的在重新加载其配置。 docker logs doesn't show anything. docker logs没有显示任何内容。

I'd like to approach this problem in a twofold way:我想以双重方式解决这个问题:

  1. How can I output to terminal / docker logs ( echo ?) the succcessful nginx -s reload modifying my /bin/bash -c 'while :; do sleep 6h & wait ${!}; nginx -s reload; done & nginx -g "daemon off;"'如何输出到终端 / docker 日志( echo ?)成功的nginx -s reload修改我的/bin/bash -c 'while :; do sleep 6h & wait ${!}; nginx -s reload; done & nginx -g "daemon off;"' /bin/bash -c 'while :; do sleep 6h & wait ${!}; nginx -s reload; done & nginx -g "daemon off;"' /bin/bash -c 'while :; do sleep 6h & wait ${!}; nginx -s reload; done & nginx -g "daemon off;"' ? /bin/bash -c 'while :; do sleep 6h & wait ${!}; nginx -s reload; done & nginx -g "daemon off;"' ?
  2. In case I miss something (I'm a beginner for both Docker and NGINX): do you see a way to achieve the results I'd like to get in a more reasonable way?万一我遗漏了什么(我是 Docker 和 NGINX 的初学者):你有没有办法以更合理的方式实现我想要的结果?

Please don't suggest Docker Compose.请不要推荐 Docker Compose。

Thank you for your attention.感谢您的关注。

OK, I got it working.好的,我让它工作了。 I changed the CMD args line to:我将 CMD args 行更改为:

/bin/sh -c 'while :; do sleep 6h & wait ${!}; nginx -s reload && echo NGINX config reload for Certbot - OK; done & nginx -g "daemon off;"'

My main mistake was to enter the CMD using /bin/bash , where I should do /bin/sh .我的主要错误是使用/bin/bash输入 CMD,我应该在其中执行/bin/sh And the following nginx -s reload && echo NGINX config reload for Certbot - OK solved the problem with notyfying docker logs about config reload.以下nginx -s reload && echo NGINX config reload for Certbot - OK解决了通知 docker docker logs有关配置重新加载的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM