我通过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":""}]
首先按照以下步骤操作
第二:您不应该在Build
阶段复制/var/lib/postgresql/data
中的任何文件 - 将 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