0

When using the https://github.com/kiasaki/docker-alpine-postgres image in docker compose, the scripts inside /docker-entrypoint.initdb.d are not being executed as I view from the logs:

 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

The thing is, if I build the Dockerfile standalone and run the container, it will execute the sql files inside the folder, like you can see from the logs:

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

The 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

What am I doing wrong here?

4

1 回答 1

0

更新,因为他使用他自己的 Dockerfile,而不是来自 github 链接


你能提供 docker run 命令吗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 "$0: running $f"; . "$f" ;;
            *.sql) echo "$0: running $f"; psql --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" < "$f" && echo ;;
            *)     echo "$0: 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
于 2016-09-06T03:11:59.797 回答