简体   繁体   English

无法连接 docker 容器中的 postgres

[英]Can't connect on postgres in docker container

I'm using Docker on Windows to use in local a postgres database.我在 Windows 上使用 Docker 在本地 postgres 数据库中使用。 I made two docker container: one for the database called pg and another called pgadmin with pgadmin4.我制作了两个 docker 容器:一个用于名为 pg 的数据库,另一个用于 pgadmin 和 pgadmin4。 After inspected pg, I connected pgadmin4 in 172.17.0.3:5432 and it works.检查 pg 后,我将 pgadmin4 连接到 172.17.0.3:5432 并且它可以工作。 Now time to code but...现在是时候编码了,但是......

psycopg2.OperationalError: could not connect to server: Connection timed out (0x0000274C/10060)
        Is the server running on host "172.17.0.3" and accepting
        TCP/IP connections on port 5432?

I read a lot of questions like that, but i can't figure it out.我读了很多这样的问题,但我无法弄清楚。 Doesn't seem, to me, like a problem generated in pg_hba.conf for the denied connection, because in this case pgadmin should not work, right?对我来说,这似乎不像在 pg_hba.conf 中为被拒绝的连接生成的问题,因为在这种情况下 pgadmin 不应该工作,对吧? (But if is a problem with pg_hba.conf where I can find it? In the docker installation folder I did't find it) (但是如果pg_hba.conf有问题我在哪里可以找到它?在docker安装文件夹中我没有找到它)

I put my python code here but don't seems the problem:我把我的 python 代码放在这里,但似乎没有问题:

conn = psycopg2.connect(
    database = 'example',
    user = 'username',
    password = 'secretpassword',
    host = '172.17.0.3', # default port is 5432 so it's not necessary
)

Upgrade edit: I found the configurations In postgresql.conf the listen_addresses = '*', that is correct.升级编辑:我在 postgresql.conf 中找到了 listen_addresses = '*' 的配置,这是正确的。 In pg_hba.conf:在 pg_hba.conf 中:

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     trust
host    replication     all             127.0.0.1/32            trust
host    replication     all             ::1/128                 trust

You could use docker run and the --expose parameter to expose your ports, on each container.您可以使用docker run--expose参数在每个容器上公开您的端口。

Another command you can run is docker network connect which allows you to connect a container to a network, so if two containers are on the same network, they should be able to communicate with each other.您可以运行的另一个命令是docker network connect ,它允许您将容器连接到网络,因此如果两个容器位于同一网络上,它们应该能够相互通信。

The usage is docker network connect [OPTIONS] NETWORK CONTAINER用法是docker network connect [OPTIONS] NETWORK CONTAINER

One advantage of using the docker network create command is to be able to create an --ip-range so to specify ip addresses to be used on the network.使用docker network create命令的一个优点是能够创建 --ip --ip-range以便指定要在网络上使用的 ip 地址。

Afterwards you can assign a container to have a specific ip address, docker network connect .之后,您可以分配一个容器以具有特定的 ip 地址, docker network connect If the container was not on the network for example, the ip address would not be reassigned to another container on the network.例如,如果容器不在网络上,则不会将 ip 地址重新分配给网络上的另一个容器。

More information in the docker documentation, https://docs.docker.com/engine/reference/commandline/network_connect/ docker 文档中的更多信息, https://docs.docker.com/engine/reference/commandline/network_connect/

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

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