簡體   English   中英

postgresql 在 docker-compose 中無法訪問

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM