docker-compose.yml
· 1.2 KiB · YAML
Surowy
db:
image: postgres:15
container_name: app_db
environment:
POSTGRES_USER: ${DB_USER:-postgres}
POSTGRES_DB: ${DB_NAME:-app_db}
POSTGRES_PASSWORD: ${DB_PASSWORD:-password}
healthcheck:
test: [ "CMD-SHELL", "pg_isready -d ${DB_NAME:-app_db} -U ${DB_USER:-postgres}" ]
interval: 10s
timeout: 5s
retries: 5
restart: unless-stopped
networks:
- app_network
volumes:
- app_postgres_data:/var/lib/postgresql/data
- type: bind
source: ./database_init # 假設你的初始化腳本放在專案根目錄下的 database_init 資料夾
target: /docker-entrypoint-initdb.d
consistency: cached
command: >
bash -c ' if command -v apt-get >/dev/null 2>&1; then
apt-get update && apt-get install -y dos2unix
elif command -v apk >/dev/null 2>&1; then
apk add --no-cache dos2unix
fi && find /docker-entrypoint-initdb.d -type f -name "*.sh" -exec sh -c '\''
dos2unix "{}" 2>/dev/null || true
chmod +x "{}"
'\'' \; && exec docker-entrypoint.sh postgres '
volumes:
app_postgres_data: # 定義給 PostgreSQL 數據用的 volume
networks:
app_network: # 定義給服務串接用的網路
| 1 | db: |
| 2 | image: postgres:15 |
| 3 | container_name: app_db |
| 4 | environment: |
| 5 | POSTGRES_USER: ${DB_USER:-postgres} |
| 6 | POSTGRES_DB: ${DB_NAME:-app_db} |
| 7 | POSTGRES_PASSWORD: ${DB_PASSWORD:-password} |
| 8 | healthcheck: |
| 9 | test: [ "CMD-SHELL", "pg_isready -d ${DB_NAME:-app_db} -U ${DB_USER:-postgres}" ] |
| 10 | interval: 10s |
| 11 | timeout: 5s |
| 12 | retries: 5 |
| 13 | restart: unless-stopped |
| 14 | networks: |
| 15 | - app_network |
| 16 | volumes: |
| 17 | - app_postgres_data:/var/lib/postgresql/data |
| 18 | - type: bind |
| 19 | source: ./database_init # 假設你的初始化腳本放在專案根目錄下的 database_init 資料夾 |
| 20 | target: /docker-entrypoint-initdb.d |
| 21 | consistency: cached |
| 22 | command: > |
| 23 | bash -c ' if command -v apt-get >/dev/null 2>&1; then |
| 24 | apt-get update && apt-get install -y dos2unix |
| 25 | elif command -v apk >/dev/null 2>&1; then |
| 26 | apk add --no-cache dos2unix |
| 27 | fi && find /docker-entrypoint-initdb.d -type f -name "*.sh" -exec sh -c '\'' |
| 28 | dos2unix "{}" 2>/dev/null || true |
| 29 | chmod +x "{}" |
| 30 | '\'' \; && exec docker-entrypoint.sh postgres ' |
| 31 | |
| 32 | volumes: |
| 33 | app_postgres_data: # 定義給 PostgreSQL 數據用的 volume |
| 34 | |
| 35 | networks: |
| 36 | app_network: # 定義給服務串接用的網路 |