postgresql 使用 postgres 配置 dockerfile

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36975330/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:15:45  来源:igfitidea点击:

Configure dockerfile with postgres

postgresqldockerpsqldockerfile

提问by dmitryvim

Trying to make Dockerfile for postgres db need for my app.

试图为我的应用程序需要的 postgres db 制作 Dockerfile。

Dockerfile

文件

FROM postgres:9.4
RUN mkdir /sql
COPY src/main/resources/sql_scripts/* /sql/
RUN psql -f /sql/create_user.sql
RUN psql -U user -W 123 -f create_db.sql
RUN psql -U user -W 123 -d school_ats -f create_tables.sql

run

docker build .

result:

结果:

Sending build context to Docker daemon 3.367 MB
Step 1 : FROM postgres:9.4
 ---> 6196bca94565
Step 2 : RUN mkdir /sql
 ---> Using cache
 ---> 6f57c1e759b7
Step 3 : COPY src/main/resources/sql_scripts/* /sql/
 ---> Using cache
 ---> 3b496bfb28cd
Step 4 : RUN psql -a -f /sql/create_user.sql
 ---> Running in 33b2230a12fa
psql: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
The command '/bin/sh -c psql -a -f /sql/create_user.sql' returned a non-zero code: 2

How can I specify db in docker for my project?

如何在 docker 中为我的项目指定 db?

回答by jazgot

When building your docker image postgres is not running. Database is started when container is starting, any sql files can be executed after that. Easiest solution is to put your sql files into special directory:

构建 docker 映像时 postgres 未运行。数据库在容器启动时启动,之后可以执行任何 sql 文件。最简单的解决方案是将您的 sql 文件放入特殊目录:

FROM postgres:9.4
COPY *.sql /docker-entrypoint-initdb.d/

When booting startup script will execute all files from this dir. You can read about this in docs https://hub.docker.com/_/postgres/in section How to extend this image.

启动启动脚本时将执行此目录中的所有文件。您可以在文档https://hub.docker.com/_/postgres/部分如何扩展此图像中阅读有关此内容的信息。

Also, if you need different user you should set environment variables POSTGRES_USERand POSTGRES_PASSWORD. It's easier then using custom scripts for creating user.

此外,如果您需要不同的用户,您应该设置环境变量POSTGRES_USERPOSTGRES_PASSWORD. 使用自定义脚本创建用户更容易。

回答by Daniel Stefaniuk

As the comment above says during the image build you don't get a running instance of Postgres.

正如上面的评论所说,在映像构建期间,您不会获得 Postgres 的运行实例。

You could take slightly different approach. Instead of trying to execute SQL scripts yourself you could copy them to /docker-entrypoint-initdb.d/directory. They will be executed when the container starts up.

你可以采取稍微不同的方法。您可以将它们复制到/docker-entrypoint-initdb.d/目录,而不是尝试自己执行 SQL 脚本。它们将在容器启动时执行。

Have a look how postgres:9.4 image is build:

看看 postgres:9.4 镜像是如何构建的:

Also in your Dockerfile use variables to set database details:

同样在您的 Dockerfile 中使用变量来设置数据库详细信息:

  • POSTGRES_DB
  • POSTGRES_USER
  • POSTGRES_PASSWORD
  • POSTGRES_DB
  • POSTGRES_USER
  • POSTGRES_PASSWORD