[英]postgresql not reachable in docker-compose
我使用 docker-compose 設置連接到 postgresql 數據庫的 web 服務。 當我運行docker-compose up -d
時,會創建 mydb 和 web 服務,但無法從 web 服務訪問數據庫。 我不明白為什么。
services:
web:
image: etherpad/etherpad:1.8.10
depends_on:
mydb:
condition: service_healthy
ports:
- "6080:9001"
environment:
- DB_TYPE=postgres
- DB_PORT=5432
- DB_HOST=mydb
- DB_NAME=postgres
- DB_USER=etherpad
- DB_PASS=mydbpass
mydb:
image: postgres:13
expose:
- 5432
environment:
- POSTGRES_DB=postgres
- POSTGRES_PASSWORD=mydbpass
- POSTGRES_USER=etherpad
healthcheck:
test: ["CMD-SHELL", "pg_isready"]
interval: 10s
timeout: 5s
retries: 5
我修復了它:
systemctl stop nftables
systemctl start docker
systemctl disable nftables
reboot
systemctl start docker
參見:http://github.com/moby/moby/issues/36151#issuecomment-968356070
對於 2 個容器(=服務實例)進行通信,您需要它們之間的網絡。
確實 depends_on 是不夠的,它只是增加了對啟動/構建的約束。
services:
web:
image: etherpad/etherpad:1.8.10
depends_on:
mydb:
condition: service_healthy
ports:
- "6080:9001"
environment:
- DB_TYPE=postgres
- DB_PORT=5432
- DB_HOST=mydb
- DB_NAME=postgres
- DB_USER=etherpad
- DB_PASS=mydbpass
networks:
- backend
mydb:
image: postgres:13
expose:
- 5432
environment:
- POSTGRES_DB=postgres
- POSTGRES_PASSWORD=mydbpass
- POSTGRES_USER=etherpad
healthcheck:
test: ["CMD-SHELL", "pg_isready"]
interval: 10s
timeout: 5s
retries: 5
networks:
- backend
networks:
backend:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.