我正在嘗試構建一個 docker-compose 來設定一個帶有初始腳本的 postgres 資料庫,該腳本在那里創建一些表。
我有這個 docker-compose.yml
version: '3.9'
services:
postgres:
image: postgres:12.7
#restart: always
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
logging:
options:
max-size: 10m
max-file: "3"
ports:
- '5438:5432'
volumes:
- ./postgres-data:/var/lib/postgresql/data
# copy the sql script to create tables
#- ./sql/create_tables.sql:/docker-entrypoint-initdb.d/create_tables.sql
- ./sql/create_tables.sh:/docker-entrypoint-initdb.d/create_tables.sh
這是create_tables.sh:
#!/bin/bash
echo Start Executing SQL commands
psql -U postgres -f create_tables.sql
和錯誤日志:
postgres_1 | psql: error: create_tables.sql: No such file or directory
取消注釋該行- ./sql/create_tables.sql:/docker-entrypoint-initdb.d/create_tables.sql并注釋該- ./sql/create_tables.sh:/docker-entrypoint-initdb.d/create_tables.sh行使其作業,但我必須使用.sh。
我的碼頭檔案:
FROM postgres:12.7
#WORKDIR ./sql/
ADD ./sql/create_tables.sql ./docker-entrypoint-initdb.d/create_tables.sql
ADD ./sql/create_tables.sh ./docker-entrypoint-initdb.d/create_tables.sh
RUN ./docker-entrypoint-initdb.d/create_tables.sh
任何想法我做錯了什么?謝謝!!
uj5u.com熱心網友回復:
您只需要使用下面的腳本,在卷步驟中指向您的“.SQL”檔案夾。它將自動創建資料庫并執行此檔案夾中的每個 SQL 檔案。您不需要單獨的 .sh 檔案來使用 docker-compose 和正確的卷命令遷移/創建/執行您的 SQL 命令。
version: '3.9'
services:
postgres:
image: postgres:12.7
#restart: always
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
logging:
options:
max-size: 10m
max-file: "3"
ports:
- '5438:5432'
volumes:
- ./postgres-data:/var/lib/postgresql/data
# copy the sql script to create tables
#- ./sql/create_tables.sql:/docker-entrypoint-initdb.d/create_tables.sql
#- ./sql/create_tables.sh:/docker-entrypoint-initdb.d/create_tables.sh
- ./sql:/docker-entrypoint-initdb.d #put all .SQL files inside sql/ folder and all files will be executed.
uj5u.com熱心網友回復:
最后,我將一個 sql 檔案夾和一個 sh 檔案夾分開,它可以作業,但正確的方法是 Paulo 回答。
- ./db-init-scripts/create_tables.sh:/docker-entrypoint-initdb.d/init.sh
- ./sql:/sql
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/478876.html
標籤:PostgreSQL 码头工人 yaml
下一篇:分組為單個json列
