从另一个容器访问Docker postgres容器 [英] Access Docker postgres container from another container

查看:198
本文介绍了从另一个容器访问Docker postgres容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试提供一种便携式解决方案,以使我的应用程序容器连接到postgres容器。 便携式是指我可以给用户两个 docker run 命令,每个容器一个,它们将始终协同工作。

I am trying to make a portable solution to having my application container connect to a postgres container. By 'portable' I mean that I can give the user two docker run commands, one for each container, and they will always work together.

我的本​​地PC上运行着一个postgres docker容器,我这样运行它,

I have a postgres docker container running on my local PC, and I run it like this,

docker run -p 5432:5432 -v $(pwd)/datadir:/var/lib/postgresql/data -e POSTGRES_PASSWORD=qwerty -d postgres:11

,我可以使用地址 127.0.0.1:5432 从python flask应用程序访问它。

and I am able to access it from a python flask app, using the address 127.0.0.1:5432.

我也将python应用程序也放置在docker容器中,但是我无法连接到postgres容器。

I put the python app in a docker container as well, and I am having trouble connecting to the postgres container.

地址 127.0.0.1:5432 不起作用。

地址 172.17.0.2: 5432 工作(172.17.0.2是运行postgres的Docker容器的地址)。但是我认为这不是可移植的,因为我不能保证postgres容器的IP是什么。

Address 172.17.0.2:5432 DOES work (172.17.0.2 is the address of the docker container running postgres). However I consider this not portable because I can't guarantee what the postgres container IP will be.

我知道-add- host 标志,但它也要求提供host-ip,我想成为localhost(127.0.0.1)。尽管-add-host 遇到了一些问题,但我还是无法使它正常工作,因此最终docker run命令在运行它们的任何计算机上都可以相同

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). 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.

我也尝试过这样做:从另一个容器访问的docker容器端口

我的情况是postgres和myApp将是在同一台计算机上运行的容器。我希望使用非Docker的组合解决方案。

My situation is that the postgres and myApp will be containers running on the same computer. I would prefer a non-Docker compose solution.

推荐答案

Truong 让我(再次)尝试了这种方法,并且使它起作用。这是我的步骤,以防其他问题。问题的症结在于需要一个容器以静态方式(不变)来寻址另一个容器。答案是使用用户定义的网络,因为您可以命名一个容器,并以此名称引用该容器IP。

The comment from Truong had me try that approach (again) and I got it working. 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.

我的步骤,

docker network create mynet
docker run --net mynet --name mydb -v $(pwd)/datadir:/var/lib/postgresql/data -e POSTGRES_PASSWORD=qwerty -d postgres:11

现在postgres数据库的IP地址为 mydb ,并且此容器的所有端口都暴露给该网络中运行的任何其他容器。

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.

现在添加前端应用程序,

Now add the front end app,

docker run --net mynet -ti -p 80:80 -v mydockerhubaccount/myapp

这篇关于从另一个容器访问Docker postgres容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆