简体   繁体   中英

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

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?

UPDATED as he use his own Dockerfile, not from the github link.


Could you proide the docker run command so that if I build the Dockerfile standalone and run the container, it will execute the sql files inside the folder . You should mount some directory to the container so it can find the SQL files.

I read the Dockerfile at https://github.com/kiasaki/docker-alpine-postgres/blob/master/Dockerfile and asked myself where are the SQL files? It just make the directory without copy SQL files

mkdir /docker-entrypoint-initdb.d

Then, in the docker-entrypoint.sh , it check if there's any SQL file to execute

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

I think we should mount a SQL directory, like

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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