2016-09-05 100 views
0

在docker compose中使用https://github.com/kiasaki/docker-alpine-postgres圖像時,當我從日誌中查看時,/docker-entrypoint.initdb.d中的腳本未被執行: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 

泊塢窗,撰寫.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:[email protected]:5432/postgres 

我在這裏做錯了什麼?

回答

0

UPDATED因爲他使用自己的Dockerfile,而不是從github鏈接。


您能否推測碼頭運行命令,以便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 
+0

構建和運行命令看起來像是 'docker build -t postgres。 &&碼頭運行postgres'。 DB的Dockerfile已經將文件傳輸,像這樣的部分: 'FROM kiasaki /高山Postgres的:最新 COPY泊塢窗,入口點,initdb.d /docker-entrypoint-initdb.d CMD [ 「的Postgres」 ]' – Techradar

+0

哦,你可以試着重寫'''ENTRYPOINT'''嗎?我編輯了我的答案。 – Tuan

+0

非常感謝!而已! 只需要修復環境變量,因爲沒有默認設置,但環境支撐很容易。 – Techradar