Docker 容器 Postgres 连接错误
Docker container Postgres connection error
我一直在尝试 docker postgres 容器。到目前为止,我还无法连接到容器内的数据库。
我在下面重现问题的步骤。
docker文件开始
FROM postgres
ENV POSTGRES_DB db
ENV POSTGRES_USER postgres
ENV POSTGRES_PASSWORD postgres
COPY db_schema.sql /docker-entrypoint-initdb.d/
我像这样构建了 docker 文件
$ docker build -t db_con .
并创建了一个容器
$ docker run -it -p 5432:5432 --name test_db db_con /bin/bash
运行 容器视图如下
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
82347f1114c4 db "docker-entrypoint..." 3 hours ago Up 2 sec 0.0.0.0:5432->5432/tcp test_db
我检查了容器中的地址信息..
$ docker inspect test_db
--extract start--
"Networks": {
"bridge": {
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
}
}
--extract end--
现在,我已经在容器内尝试过,但没有成功。
我已尝试以下所有命令,但出现以下错误。
root@82347f1114c4:/# psql -U postgres -h 0.0.0.0 -p 5432 -d db
root@82347f1114c4:/# psql -U postgres -h 172.17.0.1 -p 5432 -d db
root@82347f1114c4:/# psql -U postgres -h 172.17.0.2 -p 5432 -d db
**response for all the above**
psql: could not connect to server: Connection refused
Is the server running on host "0.0.0.0" and accepting
TCP/IP connections on port 5432?
如果有人能指出正确的方向,我将很高兴。我在这里碰壁了,非常感谢任何帮助。
您似乎将默认的 postgres cmd 覆盖为 /bin/bash
。
为什么把/bin/bash
放在命令的末尾?
docker run -it -p 5432:5432 --name test_db db_con /bin/bash
尝试执行
docker run -it -p 5432:5432 --name test_db db_con
此外,只有在恢复数据库转储后,postgres 才可用。
您需要在 pg_hba.conf 中添加新规则:
nano /etc/postgresql/9.3/main/pg_hba.conf
添加:
host all all [Docker Server IP]/16 md5
接下来,您需要取消注释 postgres.conf 中的以下行:
listen_addresses = '*' # what IP address(es) to listen on;
现在重新启动您的 postgres 服务,然后重试。
我一直在尝试 docker postgres 容器。到目前为止,我还无法连接到容器内的数据库。
我在下面重现问题的步骤。
docker文件开始
FROM postgres
ENV POSTGRES_DB db
ENV POSTGRES_USER postgres
ENV POSTGRES_PASSWORD postgres
COPY db_schema.sql /docker-entrypoint-initdb.d/
我像这样构建了 docker 文件
$ docker build -t db_con .
并创建了一个容器
$ docker run -it -p 5432:5432 --name test_db db_con /bin/bash
运行 容器视图如下
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
82347f1114c4 db "docker-entrypoint..." 3 hours ago Up 2 sec 0.0.0.0:5432->5432/tcp test_db
我检查了容器中的地址信息..
$ docker inspect test_db
--extract start--
"Networks": {
"bridge": {
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
}
}
--extract end--
现在,我已经在容器内尝试过,但没有成功。 我已尝试以下所有命令,但出现以下错误。
root@82347f1114c4:/# psql -U postgres -h 0.0.0.0 -p 5432 -d db
root@82347f1114c4:/# psql -U postgres -h 172.17.0.1 -p 5432 -d db
root@82347f1114c4:/# psql -U postgres -h 172.17.0.2 -p 5432 -d db
**response for all the above**
psql: could not connect to server: Connection refused
Is the server running on host "0.0.0.0" and accepting
TCP/IP connections on port 5432?
如果有人能指出正确的方向,我将很高兴。我在这里碰壁了,非常感谢任何帮助。
您似乎将默认的 postgres cmd 覆盖为 /bin/bash
。
为什么把/bin/bash
放在命令的末尾?
docker run -it -p 5432:5432 --name test_db db_con /bin/bash
尝试执行
docker run -it -p 5432:5432 --name test_db db_con
此外,只有在恢复数据库转储后,postgres 才可用。
您需要在 pg_hba.conf 中添加新规则:
nano /etc/postgresql/9.3/main/pg_hba.conf
添加:
host all all [Docker Server IP]/16 md5
接下来,您需要取消注释 postgres.conf 中的以下行:
listen_addresses = '*' # what IP address(es) to listen on;
现在重新启动您的 postgres 服务,然后重试。