내 목표는 기본 데이터로 도커 이미지 (postgres)를 만드는 것입니다.
그래서 내 계획은 dockerfile을 작성하고 base image postgres:11
를 사용한 다음 복사하여 실행 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
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
하지만 실행 docker build -t db0 -f db0.dockerfile .
하면 다음 오류가 표시됩니다.
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
나는 도커를 잘하지 못하지만 내가 아는 것처럼 오류는
psql
명령 을 실행하려고하는데 현재 postgres 서비스를 사용할 준비가되지 않았습니까?
현재 최종 목표는 터미널에서 다음 명령을 수행하는 것과 같습니다.
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
docker cp db0.sql db0:/srv/
docker exec -it db0 bash
su postgres
psql -U postgres -p 5432 -h localhost -f /srv/db0.sql
이 작업을 수행 한 후 python_team 데이터베이스, 예제 테이블이있는 퀴즈 스키마 및 레코드를 사용하여 연결할 수 있습니다.
그것을 해결하는 방법?
나는 다음 해결책을 발견 이
dockerfile : db0.dockerfile
FROM postgres:11
ENV POSTGRES_PASSWORD postgres
COPY db0.sql /docker-entrypoint-initdb.d/
공식 postgres 이미지는 내부에있는 .sql 스크립트 를 실행합니다./docker-entrypoint-initdb.d/
그리고 실행 docker run -d -p 5432:5432 -v /srv/docker_data/db0:/var/lib/postgresql/data/ -e POSTGRES_PASSWORD=postgres --name=db0 db0
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다