【发布时间】:2014-09-03 19:44:35
【问题描述】:
我在尝试使用 Docker 部署我的应用程序时遇到了一个问题,即重新启动命名容器会为容器分配不同的 IP。也许解释我在做什么会更好地解释这个问题:
- Postgres 在名为
"postgres"$ PG_ID=$(docker run --name postgres postgres/image)的单独容器中运行 - 我的 webapp 容器链接到 postgres 容器
$ APP_ID=$(docker run --link postgres:postgres webapp/image)
将 postgres 容器映像链接到 webapp 容器会在 webapp 容器中插入一个带有 postgres 容器 IP 的主机文件条目。这允许我使用postgres:5432(我正在使用 Django btw)在我的 webapp 中指向 postgres db。这一切都很好,除非由于某种原因 postgres 崩溃。
在我手动停止 postgres 进程以模拟 postgres 进程崩溃之前,我验证了 postgres 容器的 IP:
$ docker inspect --format "{{.NetworkSettings.IPAddress}}" $PG_ID
172.17.0.73
现在为了模拟崩溃,我停止了 postgres 容器:
$ docker stop $PG_ID
如果我现在使用重新启动 postgres
$ docker start $PG_ID
容器的ip发生变化:
$ docker inspect --format "{{.NetworkSettings.IPAddress}}" $PG_ID
172.17.0.74
因此,webapp 容器中指向 postgres 容器的 IP 不再正确。我虽然通过命名容器 docker 为它分配了一个具有特定配置的名称,以便您可以在容器(网络和卷)之间可靠地链接。如果 IP 发生变化,这似乎无法达到目的。
如果每次 postgres 重新启动时我都必须重新启动我的 webapp 进程,这似乎并不比仅使用单个容器来运行两个进程更好。然后我可以使用 supervisor 或类似的东西来保持它们都运行并使用localhost 来链接进程。
我还是 Docker 新手,是我做错了什么还是 Docker 中的错误?
【问题讨论】:
-
你会发现这个related question & answer很有用。
-
谢谢。将看看ddns。希望不会那么困难。 Docker 看起来很酷,但似乎需要一些学习曲线才能正确使用它......
标签: linux networking docker linux-containers