docker alpine postgres in compose 不执行 docker-entrypoint-initdb.d 脚本

docker alpine postgres in compose not executing docker-entrypoint-initdb.d scripts

当在 docker compose 中使用 https://github.com/kiasaki/docker-alpine-postgres 图像时,/docker-entrypoint.initdb.d 中的脚本没有被执行,正如我从日志中看到的那样:

 db_1   | LOG:  database system was shut down at 2016-09-05 18:16:02 UTC
 db_1   | LOG:  MultiXact member wraparound protections are now enabled
 db_1   | LOG:  database system is ready to accept connections
 db_1   | LOG:  autovacuum launcher started

问题是,如果我独立构建 Dockerfile 和 运行 容器,它将执行文件夹内的 sql 文件,就像您从日志中看到的那样:

waiting for server to start....LOG:  database system was shut down at 2016-09-05 18:28:18 UTC
LOG:  MultiXact member wraparound protections are now enabled
LOG:  database system is ready to accept connections
LOG:  autovacuum launcher started
 done
server started

/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/syna-setup.sql
CREATE TABLE
INSERT 0 1
CREATE TABLE

waiting for server to shut down...LOG:  received fast shutdown request
.LOG:  aborting any active transactions
LOG:  autovacuum launcher shutting down
LOG:  shutting down
LOG:  database system is shut down
 done
server stopped
LOG:  database system was shut down at 2016-09-05 18:28:21 UTC
LOG:  MultiXact member wraparound protections are now enabled
LOG:  database system is ready to accept connections
LOG:  autovacuum launcher started
LOG:  received fast shutdown request
LOG:  aborting any active transactions
LOG:  autovacuum launcher shutting down
LOG:  shutting down
LOG:  database system is shut down

docker-compose.yml:

version: '2'
services:
  db:
    build: ./db
    environment:
     - POSTGRES_PASSWORD=testpass
    restart: always
  app:
    build: ./app
    ports:
     - "5000:5000"
    volumes:
     - .:/app/src
    depends_on:
     - db
    environment:
     - DB_SERVER=postgres://postgres:testpass@db:5432/postgres

我做错了什么?

UPDATED 因为他使用自己的 Dockerfile,而不是来自 github link.


您能否提供 docker 运行 命令以便 if I build the Dockerfile standalone and run the container, it will execute the sql files inside the folder。您应该将一些目录挂载到容器中,以便它可以找到 SQL 个文件。

我在 https://github.com/kiasaki/docker-alpine-postgres/blob/master/Dockerfile 阅读了 Dockerfile 并问自己 SQL 文件在哪里?它只是使目录没有复制 SQL 文件

mkdir /docker-entrypoint-initdb.d

然后,在 docker-entrypoint.sh 中,它检查是否有任何 SQL 文件要执行

for f in /docker-entrypoint-initdb.d/*; do
        case "$f" in
            *.sh)  echo "[=11=]: running $f"; . "$f" ;;
            *.sql) echo "[=11=]: running $f"; psql --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" < "$f" && echo ;;
            *)     echo "[=11=]: ignoring $f" ;;
        esac
        echo
done

我想我们应该挂载一个 SQL 目录,比如

version: '2'
services:
  db:
    build: ./db
    environment:
     - POSTGRES_PASSWORD=testpass
    restart: always
    entrypoint:
     - docker-entrypoint.sh
    volumes:
     - ./docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
     - ./docker-entrypoint.sh:/docker-entrypoint.sh