【发布时间】:2017-10-09 21:32:01
【问题描述】:
我正在使用 Docker 运行 PostgreSQL 服务。出于某种原因,PostgreSQL 想要绑定到 IPV6——尽管我没有在任何地方指定(至少据我所知)。
因此,我无法连接到 PG。相关详情如下:
Dockerfile
FROM postgres:9.6
RUN apt-get update \
&& apt-get -y install apt-utils \
&& apt-get -y install python3 \
&& apt-get -y install postgresql-plpython3-9.6
COPY sql /docker-entrypoint-initdb.d/
EXPOSE 5432
# Add VOLUMEs to allow backup of config, logs and databases
VOLUME ["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql"]
PostgreSQL 日志文件内容
LOG: received fast shutdown request
LOG: aborting any active transactions
waiting for server to shut down....LOG: autovacuum launcher shutting down
LOG: shutting down
LOG: database system is shut down
done
server stopped
PostgreSQL init process complete; ready for start up.
LOG: could not bind IPv6 socket: Cannot assign requested address
HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
LOG: database system was shut down at 2017-10-09 21:22:22 UTC
LOG: MultiXact member wraparound protections are now enabled
LOG: database system is ready to accept connections
LOG: autovacuum launcher started
我使用以下命令运行容器:docker run --name my_db_service_cntnr image_tag
当我运行以下命令时:docker container port my_db_service_cntnr,我没有得到任何返回:
me@yourbox:~/path/to/pgdb$ docker container port my_db_service_cntnr
me@yourbox:~/path/to/pgdb$
我知道 PostgreSQL 正在容器中运行:
me@yourbox:~/path/to/pgdb$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
824ffe17c5b9 df:pg "docker-entrypoint..." 16 hours ago Up 5 minutes 5432/tcp my_db_service_cntnr
me@yourbox:/path/to/pgdb$ docker container inspect my_db_service_cntnr | grep Address
"LinkLocalIPv6Address": "",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"GlobalIPv6Address": "",
"IPAddress": "172.17.0.2",
"MacAddress": "02:42:ac:11:00:02",
"IPAddress": "172.17.0.2",
"GlobalIPv6Address": "",
"MacAddress": "02:42:ac:11:00:02"
然而,当我尝试连接到 PostgreSQL(使用默认端口 5432)时,它无法连接到数据库:
尝试通过 psql 连接
me@yourbox:~/path/to/pgdb$ psql -h 172.17.0.2 -U postgres -p 5432
psql: could not connect to server: Connection refused
Is the server running on host "172.17.0.2" and accepting
TCP/IP connections on port 5432?
me@yourbox:~/path/to/pgdb$
我的机器上似乎没有监听端口 5432,尽管我指定了 PG 映像 EXPOSE 端口 5432:
me@yourbox:~/path/to/pgdb$ sudo lsof -i -P | grep -i "listen"
lighttpd 1477 www-data 4u IPv4 22342 0t0 TCP *:80 (LISTEN)
dnsmasq 1645 nobody 5u IPv4 26954 0t0 TCP CEBERUS:53 (LISTEN)
master 2182 root 12u IPv4 28720 0t0 TCP localhost:25 (LISTEN)
master 2182 root 13u IPv6 28721 0t0 TCP ip6-localhost:25 (LISTEN)
rhythmbox 3149 me 17u IPv4 33925 0t0 TCP *:3689 (LISTEN)
rhythmbox 3149 me 18u IPv6 33926 0t0 TCP *:3689 (LISTEN)
cupsd 8432 root 10u IPv6 87004 0t0 TCP ip6-localhost:631 (LISTEN)
cupsd 8432 root 11u IPv4 87005 0t0 TCP localhost:631 (LISTEN)
是什么导致了这个错误,我该如何解决?
【问题讨论】:
-
从日志看来 postgres 已启动 - 您是否尝试连接到它?..
-
你在postgres.conf中把
listen_address改成172.17.0.2了吗?.. -
@VaoTsun 我不确定我为什么要这样做。我没有遇到任何表明这一点的文件。你有任何参考来支持你的建议吗?我问,因为我已经在 Docker 中成功地将 PostgreSQL 作为服务运行,而没有遇到这个问题(或者必须按照你的建议修改 listen_address)。
-
除非在运行容器时使用
-p,否则端口不会发布到主机地址。至于连接问题,你在 Docker for Mac/Windows 上吗?使用docker run -p 5432时可以连接psql -h localhost吗? -
@HomunculusReticulli 为了在套接字之外的网络 iface 上监听,您必须将 listen_address 更改为 & 或 IP... i
标签: postgresql docker