【发布时间】: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