使用初始模式构建 postgres docker 容器 [英] Build postgres docker container with initial schema

查看:15
本文介绍了使用初始模式构建 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.

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天全站免登陆