【问题标题】:Docker-Compose | why the containers can't reach each other?码头工人组成 |为什么容器不能互相到达?
【发布时间】:2022-12-23 03:13:15
【问题描述】:

我一直在尝试创建一个 docker-compose 文件来仅使用“docker-compose up”来启动和运行我的 Web 应用程序。我无法让容器相互访问,目前,我正在尝试将后端容器连接到 postgres 数据库。

  • 我为 postgres 容器添加了健康检查
  • 我一直在尝试将“network_mode: host”添加到 postgres 容器,但没有成功。
  • 我正在使用 Prisma 作为 ORM 来连接到数据库。

这是我的 docker-compose.yml 文件:

version: "2.1"

services:
  ##############################
  # Database Container
  ##############################
  postgres:
    restart: always
    container_name: db
    hostname: postgres
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    ports:
      - "5432:5432"
    build:
      dockerfile: ./database/Dockerfile
      context: .
    networks:
      - mynet
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -d postgres -U postgres"]
      interval: 10s
      timeout: 5s
      retries: 5

  # ##############################
  # # Backend Container
  # ##############################
  backend:
    restart: always
    container_name: backend
    hostname: backend
    environment:
      - DATABASE_URL=postgresql://postgres:postgres@localhost:5432/postgres?schema=public
    build:
      dockerfile: ./Dockerfile
      context: ./backend
    depends_on:
      postgres:
        condition: service_healthy
    networks:
      - mynet
    ports:
      - "3001:3000"

  # ##############################
  # # Frontend Container
  # ##############################
  # frontend:
  #   restart: always
  #   container_name: frontend
  #   hostname: frontend
  #   build:
  #     dockerfile: ./Dockerfile
  #     context: ./frontend
  #   ports:
  #     - "3000:3000"
  #   depends_on:
  #     - "backend"
networks:
  mynet:
    driver: bridge

这就是我得到的(目前正在尝试在后端和 Postgres 容器之间进行通信。):

我真的很感激你能提供的任何帮助。

【问题讨论】:

    标签: postgresql docker dockerfile prisma sbt-docker-compose


    【解决方案1】:

    只需使用数据库服务的名称作为主机名。

     - DATABASE_URL=postgresql://postgres:postgres@postgres:5432/postgres?schema=public
    

    【讨论】:

      猜你喜欢
      • 2015-11-01
      • 1970-01-01
      • 2019-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-28
      • 1970-01-01
      • 2018-09-02
      相关资源
      最近更新 更多