docker - 在入口点 docker 的容器中恢复 pgadmin4 创建的数据库
问题描述
我从我的数据库备份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":""}]
解决方案
首先按照这些步骤
/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
推荐阅读
- python - 根据 pandas Python 中另一个数据帧的条件从一个数据帧中删除行
- reactjs - React-select 多个下拉 onChange 实现
- jenkins - 有没有办法查看詹金斯的工作有多少失败的构建?
- gitlab-ci - 将要持续的 Mkdocs gitlab-ci 模板,为什么要使用变体?
- flutter - “onError”处理程序无法返回“Null”类型的值
- php - Laravel 数据透视表与附加模型
- c++ - 为什么 C++ 中的函数没有 constexpr 参数?
- django - 多个用户使用 django-allauth 注册。两个注册表单未呈现
- ruby-on-rails - 默认 form_with 不远程也不期望通过 JS 响应
- angular - RxJS Angular - 将 Observable 的对象属性作为 Observable 传递