简体   繁体   English

docker 构建错误:psql:无法连接到服务器:连接被拒绝

[英]docker build error: psql: could not connect to server: Connection refused

My goal is to create an docker image(postgres) with its default data.我的目标是使用其默认数据创建一个 docker 镜像(postgres)。

So my plan is to write a dockerfile, use base image postgres:11 , then copy and run db0.sql inside it.所以我的计划是写一个 dockerfile,使用基础镜像postgres:11 ,然后在里面复制并运行db0.sql

SQL Script: db0.sql SQL 脚本: db0.sql

CREATE TABLE "quiz"."example" (
  "name" varchar(255),
  "age" int4,
  "phone" varchar(255),
  "blood_group" varchar(255),
  "height" int4
);

INSERT INTO "quiz"."example" VALUES ('Tony', NULL, NULL, 'O', 180);
ALTER TABLE "quiz"."example" ADD CONSTRAINT "name" UNIQUE ("name");

dockerfile: db0.dockerfile dockerfile: db0.dockerfile

FROM postgres:11
COPY db0.sql /srv/
RUN /etc/init.d/postgresql start
RUN su postgres
EXPOSE 5432
RUN psql -U postgres -p 5432 -h localhost -f /srv/db0.sql

But when I run docker build -t db0 -f db0.dockerfile .但是当我运行docker build -t db0 -f db0.dockerfile . , it shows me the following error: ,它显示了以下错误:

Sending build context to Docker daemon  4.096kB
Step 1/6 : FROM postgres:11
 ---> 55ff21ffc6d1
Step 2/6 : COPY db0.sql /srv/
 ---> Using cache
 ---> 5623b274bfef
Step 3/6 : RUN /etc/init.d/postgresql start
 ---> Using cache
 ---> 838cd16f9545
Step 4/6 : RUN su postgres
 ---> Using cache
 ---> 2c65f67991f0
Step 5/6 : EXPOSE 5432
 ---> Using cache
 ---> a7921ebdf180
Step 6/6 : RUN psql -U postgres -p 5432 -h localhost -f /srv/db0.sql
 ---> Running in ddaced269112
psql: could not connect to server: Connection refused
        Is the server running on host "localhost" (127.0.0.1) and accepting
        TCP/IP connections on port 5432?
could not connect to server: Network is unreachable
        Is the server running on host "localhost" (::1) and accepting
        TCP/IP connections on port 5432?
The command '/bin/sh -c psql -U postgres -p 5432 -h localhost -f /srv/db0.sql' returned a non-zero code: 2

I'm not good at docker, but as I know, the error seems to tell me that我不擅长 docker,但据我所知,错误似乎告诉我

When I try to run psql command, but at this time, the postgres service is not ready for use?当我尝试运行psql命令时,但此时 postgres 服务还没有准备好使用?


For now, my final goal is just like do the following command in terminal:现在,我的最终目标就像在终端中执行以下命令一样:

  1. docker run -d -p 5432:5432 -v /srv/docker_data/db0:/var/lib/postgresql/data/ -e POSTGRES_PASSWORD=postgres --name=db0 --restart=always postgres:11
  2. docker cp db0.sql db0:/srv/
  3. docker exec -it db0 bash
  4. su postgres
  5. psql -U postgres -p 5432 -h localhost -f /srv/db0.sql

After doing these, I can connect into it with a python_team Database, a quiz schema in it with an example table, and a record in it.完成这些后,我可以使用 python_team 数据库、其中包含示例表的测验模式和其中的记录连接到它。

How to solve that?如何解决?

I found the solution, follow this我找到了解决方案,请按照此操作

dockerfile: db0.dockerfile dockerfile: db0.dockerfile

FROM postgres:11

ENV POSTGRES_PASSWORD postgres
COPY db0.sql /docker-entrypoint-initdb.d/

official postgres image will run .sql scripts found inside /docker-entrypoint-initdb.d/官方 postgres 映像将运行在/docker-entrypoint-initdb.d/找到的.sql脚本

and then run docker run -d -p 5432:5432 -v /srv/docker_data/db0:/var/lib/postgresql/data/ -e POSTGRES_PASSWORD=postgres --name=db0 db0然后运行docker run -d -p 5432:5432 -v /srv/docker_data/db0:/var/lib/postgresql/data/ -e POSTGRES_PASSWORD=postgres --name=db0 db0

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 psql:无法连接到服务器:Docker在Windows中拒绝连接到psql - psql: could not connect to server: Connection refused in Docker to a psql in Windows 在/docker-entrypoint-initdb.d/db_init.sh中运行psql命令时出错(psql:无法连接到服务器:连接被拒绝) - Error when running psql command in /docker-entrypoint-initdb.d/db_init.sh (psql: could not connect to server: Connection refused) psql:无法连接到服务器:连接被拒绝 - psql: could not connect to server: Connection refused 如何修复:在 gitlab-ci 的 docker 容器中使用 Postgres 时,“Psql:无法连接到服务器:连接被拒绝” - How to fix: 'Psql: could not connect to server: Connection refused' when using Postgres in a docker container in gitlab-ci 使用休眠模式:psql:无法连接到服务器:连接被拒绝 - using hibernate: psql: could not connect to server: Connection refused “psql:无法连接到服务器:连接被拒绝”连接到远程数据库时出错 - "psql: could not connect to server: Connection refused" Error when connecting to remote database PostgreSQL:无法连接到服务器 - 连接被拒绝错误 - PostgreSQL: could not connect to server - Connection refused error PG ::错误:无法连接到服务器:连接被拒绝 - PG::Error: could not connect to server: Connection refused psql:无法连接到服务器:连接被拒绝服务器是否在主机“ localhost”(:: 1)上运行并在端口5432上接受TCP / IP连接? - psql: could not connect to server: Connection refused Is the server running on host “localhost” (::1) and accepting TCP/IP connections on port 5432? Docker - PostgreSQL 无法连接到服务器:连接被拒绝 127.0.0.1:5432 - Docker - PostgreSQL could not connect to server: Connection refused 127.0.0.1:5432
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM