我通过pgadmins4
从数据库中进行了备份,并存储到了我的dockerfile位置。
现在,我通过我的docker文件创建一个自定义docker映像:
FROM mdillon/postgis:11-alpine
LABEL MAINTAINER groot
ENV LANG en_US.utf8
ENV DBNAME pglocations
ENV USERNAME postgres
COPY init.sh /docker-entrypoint-initdb.d/
COPY ./pglocations.sql .
我建立了一个图像,当我想创建容器时,出现了这个错误:
server started
/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init.sh
CREATE DATABASE
2019-08-27 06:07:31.790 UTC [43] ERROR: index "user_device_id" does not exist
2019-08-27 06:07:31.790 UTC [43] STATEMENT: DROP INDEX public.user_device_id;
2019-08-27 06:07:31.790 UTC [43] ERROR: index "uid" does not exist
2019-08-27 06:07:31.790 UTC [43] STATEMENT: DROP INDEX public.uid;
2019-08-27 06:07:31.790 UTC [43] ERROR: index "timestamp" does not exist
2019-08-27 06:07:31.790 UTC [43] STATEMENT: DROP INDEX public."timestamp";
2019-08-27 06:07:31.791 UTC [43] ERROR: index "temp_mileage_location_user_id_idx" does not exist
2019-08-27 06:07:31.791 UTC [43] STATEMENT: DROP INDEX public.temp_mileage_location_user_id_idx;
2019-08-27 06:07:31.791 UTC [43] ERROR: index "temp_mileage_location_shape_idx" does not exist
2019-08-27 06:07:31.791 UTC [43] STATEMENT: DROP INDEX public.temp_mileage_location_shape_idx;
2019-08-27 06:07:31.791 UTC [43] ERROR: index "temp_mileage_location_device_id_idx" does not exist
2019-08-27 06:07:31.791 UTC [43] STATEMENT: DROP INDEX public.temp_mileage_location_device_id_idx;
...
在数据库中,我添加了 postgis 和 uuid.ossp 扩展名和2个功能。
通过Pgadmin4,我可以还原该数据库。
这是我的init.sh
#!/bin/sh
set -e
psql -v ON_ERROR_STOP=1 --dbname template1 --username postgres <<-EOSQL
CREATE DATABASE "$DBNAME"
WITH OWNER = postgres
ENCODING = 'UTF8'
LC_COLLATE = 'English_United States.1252'
TABLESPACE = pg_default
CONNECTION LIMIT = -1
TEMPLATE template0;
EOSQL
#start postgres database
pg_restore -d "$DBNAME" pglocations.sql -c -U "$USERNAME"
这是我的容器挂壁处:
docker inspect -f'{{json .Mounts}}'pg-docker
[{"Type":"volume","Name":"a0334d571a61c0a17a23ca4be6a191143f39657967c52b3326abf8c011f54861","Source":"/var/lib/docker/volumes/a0334d571a61c0a17a23ca4be6a191143f39657967c52b3326abf8c011f54861/_data","Destination":"/var/lib/postgresql/data","Driver":"local","Mode":"","RW":true,"Propagation":""}]
答案 0 :(得分:0)
首先遵循这些Steps
秒:您不应在/var/lib/postgresql/data
阶段复制Build
中的任何文件-将docker文件更改为:
FROM mdillon/postgis:11-alpine
LABEL MAINTAINER groot
ENV LANG en_US.utf8
ENV DBNAME pglocations
ENV USERNAME postgres
COPY init.sh /docker-entrypoint-initdb.d/
然后构建新图像后启动container
:
docker run --name=pg-docker -p 5433:5432 -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=s123 -v /path/to/pglocations.sql:/docker-entrypoint-initdb.d/pglocations.sql safapostgis:11.2