我认为这个问题应该很容易解决,但我正在拉我的头发。我有以下 docker-compose:
version: "3"
services:
pg-db:
image: postgres:11
environment:
POSTGRES_PASSWORD: "postgres"
POSTGRES_USER: "postgres"
POSTGRES_DB: "postgres"
ports:
- 5432:5432
#network_mode: host
然后我运行
docker-compose up
然后启动容器pg-db_1_78e7ec4a95e6 | 2020-02-21 13:53:53.928 UTC [1] LOG: database system is ready to accept connections
我可以用 docker exec 连接到它
docker exec -it docker_pg-db-compose_1_78e7ec4a95e6 psql -h pg-db -U postgres postgres
但是我无法通过“裸” psql 连接到它:
psql postgresql://postgres:postgres@localhost:5432/postgres
psql: could not connect to server: Connection refused
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
psql postgresql://postgres:postgres@pg-dg:5432/postgres
psql: could not translate host name "pg-db" to address: Name or service not known
我试过
network_mode
但它没有帮助。
最佳答案
启动 docker-compose 后,您需要通过以下方式获取 docker 容器 ip:docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id
那么你可以做postgresql://postgres:postgres@<container idp>:5432/postgres
你应该能够连接到它
关于postgresql - 如何从外部主机连接到使用 docker-compose 创建的 postgres,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60340228/