使用初始架构构建Postgres Docker容器 [英] Build postgres docker container with initial schema

查看:100
本文介绍了使用初始架构构建Postgres Docker容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找构建表示已经存在的公司数据库的dockerfile。同样,我想创建一个通过还原psql转储开始的docker文件。

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.

我有我的 psql_dump.sql 目录中的c>。

I have my psql_dump.sql in the . directory.

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"

我认为这样做就足够了。我想避免使用 .sh 脚本的解决方案。就像此解决方案一样。

I thought this would be good enough to do it. I'd like to avoid solutions that use a .sh script. Like this solution.

我使用template0,因为psql文档说您需要创建与原始数据库中相同的用户,并且在还原之前需要使用template0创建数据库。

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.

但是,它给了我一个错误:

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

我还在整个应用程序中使用docker compose,如果在docker-compose中解决此问题更好,那么我很乐意使用基本的psql映像并使用docker compose做到这一点。

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.

推荐答案

根据用于官方PostreSQL Docker映像的使用指南,全部您需要的是:

According to the usage guide for the official PostreSQL Docker image, all you need is:

FROM postgres
ENV POSTGRES_DB my_database
COPY psql_dump.sql /docker-entrypoint-initdb.d/

POSTGRES_DB 环境变量将指示容器在首次运行时创建 my_database 架构。

The POSTGRES_DB environment variable will instruct the container to create a my_database schema on first run.

以及在 /docker-entrypoint-initdb.d / 中找到的任何 .sql 文件

And any .sql file found in the /docker-entrypoint-initdb.d/ of the container will be executed.

如果要执行 .sh 脚本,也可以在其中提供它们 /docker-entrypoint-initdb.d / 目录。

If you want to execute .sh scripts, you can also provide them in the /docker-entrypoint-initdb.d/ directory.

这篇关于使用初始架构构建Postgres Docker容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆