首页 > 解决方案 > 如何在 Postgres Docker-Compose 启动时运行 *.sh 文件

问题描述

在启动新的 Docker-Compose Postgres 映像时,我正在尝试在 Postgres 上创建一个新的 DB + 用户。出于某种原因,该init-user-db.sh文件没有被复制docker-entrypoint-initdb.d到容器上的文件夹内。

知道为什么吗?

谢谢

码头工人-compose.yml:

version: '3'
services:
  db:
    image: postgres:11
    volumes:
      - ./db/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
    ports:
      - '5432:5432'

初始化用户-db.sh:

#!/bin/bash
set -e
psql -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" <<-EOSQL
    CREATE USER bh_ideas_user;
    CREATE DATABASE bh_ideas ENCODING UTF8;
    GRANT ALL PRIVILEGES ON DATABASE bh_ideas TO bh_ideas_user;
    ALTER USER bh_ideas_user WITH PASSWORD 'password123';
    ALTER USER bh_ideas_user WITH SUPERUSER;
EOSQL

这是我收到的输出:

$ docker-compose up && exit 0
Creating ideas-api_db_1 ... done
Attaching to ideas-api_db_1
db_1  | The files belonging to this database system will be owned by user "postgres".
db_1  | This user must also own the server process.
db_1  |
db_1  | The database cluster will be initialized with locale "en_US.utf8".
db_1  | The default database encoding has accordingly been set to "UTF8".
db_1  | The default text search configuration will be set to "english".
db_1  |
db_1  | Data page checksums are disabled.
db_1  |
db_1  | fixing permissions on existing directory /var/lib/postgresql/data ... ok
db_1  | creating subdirectories ... ok
db_1  | selecting default max_connections ... 100
db_1  | selecting default shared_buffers ... 128MB
db_1  | selecting dynamic shared memory implementation ... posix
db_1  | creating configuration files ... ok
db_1  | running bootstrap script ... ok
db_1  | performing post-bootstrap initialization ... ok
db_1  | syncing data to disk ... ok
db_1  |
db_1  | Success. You can now start the database server using:
db_1  |
db_1  |     pg_ctl -D /var/lib/postgresql/data -l logfile start
db_1  |
db_1  |
db_1  | WARNING: enabling "trust" authentication for local connections
db_1  | You can change this by editing pg_hba.conf or using the option -A, or
db_1  | --auth-local and --auth-host, the next time you run initdb.
db_1  | ****************************************************
db_1  | WARNING: No password has been set for the database.
db_1  |          This will allow anyone with access to the
db_1  |          Postgres port to access your database. In
db_1  |          Docker's default configuration, this is
db_1  |          effectively any other container on the same
db_1  |          system.
db_1  |
db_1  |          Use "-e POSTGRES_PASSWORD=password" to set
db_1  |          it in "docker run".
db_1  | ****************************************************
db_1  | waiting for server to start....2019-01-19 10:28:21.673 UTC [48] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1  | 2019-01-19 10:28:21.692 UTC [49] LOG:  database system was shut down at 2019-01-19 10:28:21 UTC
db_1  | 2019-01-19 10:28:21.699 UTC [48] LOG:  database system is ready to accept connections
db_1  |  done
db_1  | server started
db_1  |
db_1  | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
db_1  |
db_1  | 2019-01-19 10:28:21.763 UTC [48] LOG:  received fast shutdown request
db_1  | waiting for server to shut down....2019-01-19 10:28:21.766 UTC [48] LOG:  aborting any active transactions
db_1  | 2019-01-19 10:28:21.767 UTC [48] LOG:  background worker "logical replication launcher" (PID 55) exited with exit code 1
db_1  | 2019-01-19 10:28:21.769 UTC [50] LOG:  shutting down
db_1  | 2019-01-19 10:28:21.794 UTC [48] LOG:  database system is shut down
db_1  |  done
db_1  | server stopped
db_1  |
db_1  | PostgreSQL init process complete; ready for start up.
db_1  |
db_1  | 2019-01-19 10:28:21.903 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1  | 2019-01-19 10:28:21.903 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1  | 2019-01-19 10:28:21.910 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1  | 2019-01-19 10:28:21.930 UTC [57] LOG:  database system was shut down at 2019-01-19 10:28:21 UTC
db_1  | 2019-01-19 10:28:21.937 UTC [1] LOG:  database system is ready to accept connections

从 中docker inspect,绑定已正确完成:

"Binds": ["/host_mnt/d/Projects/ideas-api/db/initdb.d:/docker-entrypoint-initdb.d:rw"],

标签: postgresqldockerdocker-compose

解决方案


推荐阅读