[英]Access Docker postgres container from another container
I am trying to make a portable solution to having my application container connect to a postgres container.我正在尝试制作一个便携式解决方案,让我的应用程序容器连接到 postgres 容器。 By 'portable' I mean that I can give the user two
docker run
commands, one for each container, and they will always work together. “便携”的意思是我可以给用户两个
docker run
命令,每个容器一个,它们将始终协同工作。
I have a postgres docker container running on my local PC, and I run it like this,我在本地 PC 上运行了一个 postgres docker 容器,我像这样运行它,
docker run -p 5432:5432 -v $(pwd)/datadir:/var/lib/postgresql/data -e POSTGRES_PASSWORD=qwerty -d postgres:11
and I am able to access it from a python flask app, using the address 127.0.0.1:5432
.我可以使用地址
127.0.0.1:5432
从 python flask 应用程序访问它。
I put the python app in a docker container as well, and I am having trouble connecting to the postgres container.我也将 python 应用程序放在 docker 容器中,但在连接到 postgres 容器时遇到了问题。
Address 127.0.0.1:5432
does not work.地址
127.0.0.1:5432
不起作用。
Address 172.17.0.2:5432
DOES work (172.17.0.2 is the address of the docker container running postgres).地址
172.17.0.2:5432
确实有效(172.17.0.2 是运行 postgres 的 docker 容器的地址)。 However I consider this not portable because I can't guarantee what the postgres container IP will be.但是我认为这不可移植,因为我不能保证 postgres 容器 IP 是什么。
I am aware of the --add-host
flag, but it is also asking for the host-ip, which I want to be the localhost (127.0.0.1).我知道
--add-host
标志,但它也要求主机 IP,我想成为 localhost (127.0.0.1)。 Despite several hits on --add-host
I wasn't able to get that to work so that the final docker run commands can be the same on any computer they are run on.尽管对
--add-host
了多次点击,但我无法--add-host
工作,因此最终的 docker run 命令在运行它们的任何计算机上都可以相同。
I also tried this: docker container port accessed from another container我也试过这个: docker container port accessed from another container
My situation is that the postgres and myApp will be containers running on the same computer.我的情况是 postgres 和 myApp 将是在同一台计算机上运行的容器。 I would prefer a non-Docker compose solution.
我更喜欢非 Docker 组合解决方案。
The comment from Truong had me try that approach (again) and I got it working.来自Truong的评论让我(再次)尝试了这种方法,并且成功了。 Here are my steps in case it helps out another.
这是我的步骤,以防它帮助另一个。 The crux of the problem was needing one container to address another container in a way that was static (didn't change).
问题的关键是需要一个容器以静态(没有改变)的方式来处理另一个容器。 Using user defined network was the answer, because you can name a container, and thus reference that container IP by that name.
答案是使用用户定义的网络,因为您可以命名一个容器,从而通过该名称引用该容器 IP。
My steps,我的脚步,
docker network create mynet
docker run --net mynet --name mydb -v $(pwd)/datadir:/var/lib/postgresql/data -e POSTGRES_PASSWORD=qwerty -d postgres:11
Now the IP address of the postgres database is mydb
, and all the ports of this container are exposed to any other container running in this network.现在 postgres 数据库的 IP 地址是
mydb
,并且这个容器的所有端口都暴露给在这个网络中运行的任何其他容器。
Now add the front end app,现在添加前端应用程序,
docker run --net mynet -ti -p 80:80 -v mydockerhubaccount/myapp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.