【问题标题】:Restarting Containers When Using Docker and Nginx proxy_pass使用 Docker 和 Nginx proxy_pass 时重启容器
【发布时间】:2015-05-03 16:04:02
【问题描述】:

我有一个 nginx docker 容器和一个 webapp 容器成功运行并相互通信。

nginx 容器监听 80 端口,并使用 proxy_pass 将流量引导到 webapp 容器的 IP。

upstream app_humansio { server humansio:8080 max_fails=3 fail_timeout=30s; }

“humansio”是 docker 在 /etc/hosts 文件中设置的,因为我已经使用 --link humansio:humansio 启动了 nginx。 webapp 容器 (humansio) 总是暴露 8080。

问题是,当我重新加载 webapp 容器时,到 nginx 容器的链接中断,我也需要重新启动它。有什么方法可以让我在 webapp 容器重新加载时不需要重新启动 nginx 容器?

--

我尝试通过使用公共端口(两者都为 8001)手动连接它们,但由于它们实际上保留了该端口,因此第二个容器也无法使用它。

谢谢!

【问题讨论】:

  • 您使用的是什么版本的 Docker?重启你的webapp容器后nginx容器/etc/hosts的内容会更新吗?

标签: nginx docker proxypass


【解决方案1】:

出于这个原因,我更喜欢直接在主机上运行代理(haproxy 的 nginx)。

但一个选项是“通过大使容器链接”https://docs.docker.com/articles/ambassador_pattern_linking/

https://www.digitalocean.com/community/tutorials/how-to-use-the-ambassador-pattern-to-dynamically-configure-services-on-coreos

【讨论】:

    【解决方案2】:

    如果您不想在必须重新启动代理容器之一时重新启动代理容器(例如 fig),您可以查看自动更新代理配置方法:http://jasonwilder.com/blog/2014/03/25/automated-nginx-reverse-proxy-for-docker/

    【讨论】:

      【解决方案3】:

      如果你使用一些现代版本的 docker,nginx 容器中到你的 web 服务的链接可能会更新(你可以用 docker exec -ti nginx bash - 然后 cat /etc/hosts 检查它) - 问题是 nginx 不使用/etc/hosts 每次 - 它都会缓存 ip,当它发生变化时 - 他会迷路。 'docker kill -s HUP nginx' 让 nginx 在不重启的情况下重新加载其配置也有帮助。

      我也有同样的问题。我曾经使用 systemd 单元文件启动我的服务——当你让一个服务(nginx)依赖于另一个(webapp)然后重新启动 webapp 时——systemd 足够聪明,也可以重新启动 nginx。现在我正在尝试使用 docker-compose 并重新启动 webapp 容器让 nginx 感到困惑。

      【讨论】:

        猜你喜欢
        • 2020-01-28
        • 2017-01-22
        • 2023-02-26
        • 2018-07-08
        • 2022-01-22
        • 1970-01-01
        • 2015-03-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多