【发布时间】:2020-02-27 17:33:01
【问题描述】:
我想使用 docker-compose 部署 2 个容器,一个用于 pgAdmin,一个用于 Postgres。容器已正确创建,我可以在浏览器上登录 pgAdin,但是当我尝试创建与 Postgres 的连接时,使用 localhost:15432 它不起作用,但如果我使用 172.19.0.2:15432 它可以工作。有没有办法使用 localhost 进行连接或为我的 Postgres 分配一个固定端口?
version: '3.5'
services:
postgres:
container_name: rn_postgres
image: postgres
hostname: postgres
ports:
- "15432:5432"
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: rn2020
POSTGRES_DB: rnmonitor
volumes:
- postgres-data:/var/lib/postgresql/data
restart: unless-stopped
pgadmin:
container_name: rn_pgadmin
image: dpage/pgadmin4
depends_on:
- postgres
ports:
- "5555:80"
volumes:
- pgadmin:/root/.pgadmin
environment:
PGADMIN_DEFAULT_EMAIL: pgadmin4@pgadmin.org
PGADMIN_DEFAULT_PASSWORD: admin
restart: unless-stopped
volumes:
pgadmin:
postgres-data:
【问题讨论】:
-
使用主机名 rn_postgres 连接怎么样?
-
@Rolando Azevedo 当您使用 localhost:15432 从 pg_admin 连接到 PostgreSQL 容器时,它将路由到 pg_admin 网络内的 localhost,而不是指向 PostgreSQL 网络。您可以使用可以使用 $ ifconfig 获得的操作系统的私有 IP 地址连接到您的 PostgreSQL
-
使用主机名 rn_postgres 我有消息“是在主机 "rn_postgres "172.19.0.2" 上运行并在端口 15432 上接受 TCP/IP 的服务器。如果分配给 Postgres 的 IP,则使用私有 IP 是一种解决方案任何时候重启都是一样的。我想使用 Pentaho 容器来执行作业,并在其中使用连接字符串,但我无法从那里找到 Postgres 的 IP。
标签: docker docker-compose containers