【发布时间】:2020-04-27 09:43:37
【问题描述】:
我有 2 个项目。第一个包含这个docker-compose.yml:
# pg/docker-compose.yml
version: "3"
services:
postgres:
image: postgres
environment:
POSTGRES_USER: pguser
POSTGRES_DB: pg
ports:
- "5432:5432"
volumes:
- "postgres-data:/var/lib/postgresql/data"
networks:
- some_net
volumes:
postgres-data:
networks:
some_net:
driver: bridge
第二个项目有这个 docker compose 文件:
# pg_client/docker-compose.yml
version: "3"
services:
postgres_client:
image: tmaier/postgresql-client
command: postgres://pguser@localhost:5432/pg
networks:
- pg_some_net
networks:
pg_some_net:
external: true
tmaier/postgresql-client 映像非常简单,它安装了 PostgresSQL 并在构建时运行命令 psql DOCKER_COMMAND。就我而言,这是psql postgres://pguser@localhost:5432/pg。
我可以从命令行很好地连接到我的 Postgres 数据库:
$ psql postgres://pgmuser@localhost:5432/pg
psql (12.1)
Type "help" for help.
pg=#
但是当postgres_client 容器尝试连接时(在运行docker-compose up 之后),它说失败了
postgres_client_1 | psql: could not connect to server: Connection refused
postgres_client_1 | Is the server running on host "localhost" (127.0.0.1) and accepting
postgres_client_1 | TCP/IP connections on port 5432?
postgres_client_1 | could not connect to server: Address not available
postgres_client_1 | Is the server running on host "localhost" (::1) and accepting
postgres_client_1 | TCP/IP connections on port 5432?
在编写我的 Postgres 连接 URI 时,我需要考虑 docker-compose 有什么特别之处吗?
非常感谢任何帮助。
【问题讨论】:
-
见stackoverflow.com/questions/34393779/…。可能会帮助你。每个 docker compose 都会建立一个隔离的网络。
-
@AshharHasan Ahhh 感谢您发现这一点。听起来我需要确保两个 docker-compose 文件位于同一个网络中
-
@AshharHasan 不幸的是,将容器添加到同一个网络似乎无法解决此问题。
标签: postgresql docker docker-compose