postgresql 使用初始模式构建 postgres docker 容器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34751814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Build postgres docker container with initial schema
提问by Jono
I'm looking to build dockerfiles that represent company databases that already exist. Similarly, I'd like create a docker file that starts by restoring a psql dump.
我正在寻找构建代表已经存在的公司数据库的 dockerfiles。同样,我想创建一个通过恢复 psql 转储开始的 docker 文件。
I have my psql_dump.sql
in the .
directory.
我psql_dump.sql
在.
目录中。
FROM postgres
ADD . /init_data
run "createdb" "--template=template0" "my_database"
run "psql" "-d" "my_database" --command="create role my_admin superuser"
run "psql" "my_database" "<" "init_data/psql_dump.sql"
I thought this would be good enough to do it. I'd like to avoid solutions that use a .sh
script. Like this solution.
我认为这足以做到这一点。我想避免使用.sh
脚本的解决方案。喜欢这个解决方案。
I use template0 since the psql documentation says you need the same users created that were in the original database, and you need to create the database with template0 before you restore.
我使用 template0 因为 psql 文档说您需要在原始数据库中创建相同的用户,并且您需要在恢复之前使用 template0 创建数据库。
However, it gives me an error:
但是,它给了我一个错误:
createdb: could not connect to database template1: could not connect to server: No such file or directory
Is the server running locally and accepting
I'm also using docker compose for the overall application, if solving this problem in docker-compose is better, I'd be happy to use the base psql image and use docker compose to do this.
我也在整个应用程序中使用 docker compose,如果在 docker-compose 中解决这个问题更好,我很乐意使用基本的 psql 映像并使用 docker compose 来做到这一点。
回答by Thomasleveil
According to the usage guidefor the official PostreSQL Docker image, all you need is:
根据官方 PostreSQL Docker 镜像的使用指南,您只需要:
Dockerfile
文件
FROM postgres
ENV POSTGRES_DB my_database
COPY psql_dump.sql /docker-entrypoint-initdb.d/
The POSTGRES_DB
environment variable will instruct the container to create a my_database
schema on first run.
该POSTGRES_DB
环境变量将指示容器中创建一个my_database
在首次运行模式。
And any .sql
file found in the /docker-entrypoint-initdb.d/
of the container will be executed.
并且在容器的 中.sql
找到的任何文件/docker-entrypoint-initdb.d/
都将被执行。
If you want to execute .sh
scripts, you can also provide them in the /docker-entrypoint-initdb.d/
directory.
如果要执行.sh
脚本,也可以在/docker-entrypoint-initdb.d/
目录中提供。
回答by yishaiz
As said in the comments, @Thomasleveil answer is great and simple if your schema recreation is fast. But in my case it's slow, and I wanted to use docker volumes, so here is what I did
正如评论中所说,如果您的模式创建速度很快,@Thomasleveil 的回答就很好而且很简单。但就我而言,它很慢,我想使用 docker 卷,所以这就是我所做的
- First use docker image as in @Thomasleveil answer to create a container with postgres with all the schema initialization
- 首先使用@Thomasleveil 答案中的 docker 图像来创建一个带有 postgres 的容器,其中包含所有架构初始化
Dockerfile:
Dockerfile:
FROM postgres
WORKDIR /docker-entrypoint-initdb.d
ADD psql_dump.sql /docker-entrypoint-initdb.d
EXPOSE 5432
then run it and create new local dir which contains the postgres data after its populated from the “psql_dump.sql” file:
docker cp mypg:/var/lib/postgresql/data ./postgres-data
Copy the data to a temp data folder, and start a new postgres docker-compose container whose volume is at the new temp data folder:
然后运行它并创建新的本地目录,其中包含从“psql_dump.sql”文件填充后的 postgres 数据:
docker cp mypg:/var/lib/postgresql/data ./postgres-data
将数据复制到临时数据文件夹,并启动一个新的 postgres docker-compose 容器,其卷位于新的临时数据文件夹中:
startPostgres.sh:
startPostgres.sh:
rm -r ./temp-postgres-data/data
mkdir -p ./temp-postgres-data/data
cp -r ./postgres-data/data ./temp-postgres-data/
docker-compose -p mini-postgres-project up
and the docker-compose.yml file is:
docker-compose.yml 文件是:
version: '3'
services:
postgres:
container_name: mini-postgres
image: postgres:9.5
ports:
- "5432:5432"
volumes:
- ./temp-postgres-data/data:/var/lib/postgresql/data
Now you can run steps #1 and #2 on a new machine or if your psql_dump.sql changes. And each time you want a new clean (but already initialized) db, you can only run startPostgres.sh from step #3. And it still uses docker volumes.
现在您可以在新机器上运行步骤 #1 和 #2,或者如果您的 psql_dump.sql 发生变化。每次你想要一个新的干净(但已经初始化)的数据库时,你只能从第 3 步运行 startPostgres.sh。它仍然使用 docker 卷。