简体   繁体   English

从 docker-compose 文件初始化 postgres 容器

[英]initialize postgres container from docker-compose file

I am trying to start a postgres container form docker-compose file and also initialize it.我正在尝试从 docker-compose 文件启动一个 postgres 容器并对其进行初始化。

docker-compose.yml docker-compose.yml

version: "3"
  postgres:
  image: "postgres"
  command: bash -c "
   postgres &&
   createuser -l \"auser\"
   "

My goal is: 1) start postgres container 2) create a user我的目标是:1)启动 postgres 容器 2)创建用户

The current implementation fails with the following error当前实现失败并出现以下错误

"root" execution of the PostgreSQL server is not permitted.
The server must be started under an unprivileged user ID to prevent
possible system security compromise.  See the documentation for
more information on how to properly start the server.

"root" execution of the PostgreSQL server is not permitted.不允许 PostgreSQL 服务器的“root”执行。

You should not run the DB container with root user.您不应该使用root用户运行数据库容器。 better to run postgres user.最好运行postgres用户。

one way is to specify the user in the docker-compose.一种方法是在 docker-compose 中指定用户。

postgres:
  image: postgres
  container_name: postgres
  user: postgres
  ports:
    - "5432:5432"
  command: 'postgres'

But agains但是再次

  command: bash -c "
   postgres &&
   createuser -l \"auser\"
   "

during the create user command, there might be the case that the DB contains is not ready to accept the connection.在创建用户命令期间,可能存在数据库包含未准备好接受连接的情况。

So you have two best option.所以你有两个最好的选择。

  • Using Environment variables使用环境变量

POSTGRES_USER POSTGRES_USER

This optional environment variable is used in conjunction with POSTGRES_PASSWORD to set a user and its password.此可选环境变量与POSTGRES_PASSWORD结合使用以设置用户及其密码。 This variable will create the specified user with superuser power and a database with the same name.此变量将创建具有超级用户权限的指定用户和同名数据库。 If it is not specified, then the default user of postgres will be used.如果未指定,则将使用postgres的默认用户。

postgres:
  image: postgres
  container_name: postgres
  environment:
    POSTGRES_USER: test
    POSTGRES_PASSWORD: password
    POSTGRES_DB: myapp
  user: postgres
  ports:
    - "5432:5432"

The second option第二种选择

Initialization scripts初始化脚本

If you would like to do additional initialization in an image derived from this one, add one or more *.sql, *.sql.gz, or *.sh scripts under /docker-entrypoint-initdb.d (creating the directory if necessary).如果您想在从该镜像派生的镜像中进行额外的初始化,请在 /docker-entrypoint-initdb.d 下添加一个或多个*.sql, *.sql.gz, or *.sh scripts /docker-entrypoint-initdb.d必要时创建目录)。 After the entrypoint calls initdb to create the default postgres user and database, it will run any *.sql files , run any executable *.sh scripts, and source any non-executable *.sh scripts found in that directory to do further initialization before starting the service.在入口点调用 initdb 以创建默认的 postgres 用户和数据库后,它将运行任何*.sql files ,运行任何可执行的*.sh脚本,并在该目录中找到任何不可执行的*.sh脚本以在之前进行进一步初始化启动服务。

For example, to add an additional user and database , add the following to /docker-entrypoint-initdb.d/init-user-db.sh :例如,要添加额外的用户和数据库,请将以下内容添加到/docker-entrypoint-initdb.d/init-user-db.sh

#!/bin/bash
set -e

psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
    CREATE USER docker;
    CREATE DATABASE docker;
    GRANT ALL PRIVILEGES ON DATABASE docker TO docker;
EOSQL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM