Next.js + Prisma 无法连接Docker PostgreSQL问题求助
问题描述
我想要将Next.js全栈应用Docker化,但出现错误提示:Please make sure your database server is running at db:5433。我尝试修改数据库名称为localhost、db、pgdb等,但均无效。
容器启动后,我可以通过pgAdmin使用localhost作为数据库名称、5433作为端口连接PostgreSQL,但Next.js应用无法连接。
我的Dockerfile:
FROM node:18-alpine AS base # Install dependencies only when needed FROM base AS deps # Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed. RUN apk add --no-cache libc6-compat WORKDIR /app # Install dependencies based on the preferred package manager COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./ RUN \ if [ -f yarn.lock ]; then yarn --frozen-lockfile; \ elif [ -f package-lock.json ]; then npm ci; \ elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm i --frozen-lockfile; \ else echo "Lockfile not found." && exit 1; \ fi # Rebuild the source code only when needed FROM base AS builder WORKDIR /app COPY --from=deps /app/node_modules ./node_modules COPY . . # Next.js collects completely anonymous telemetry data about general usage. # Learn more here: https://nextjs.org/telemetry # Uncomment the following line in case you want to disable telemetry during the build. # ENV NEXT_TELEMETRY_DISABLED 1 RUN \ if [ -f yarn.lock ]; then yarn run build; \ elif [ -f package-lock.json ]; then npm run build; \ elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm run build; \ else echo "Lockfile not found." && exit 1; \ fi # Production image, copy all the files and run next FROM base AS runner WORKDIR /app ENV NODE_ENV production # Uncomment the following line in case you want to disable telemetry during runtime. # ENV NEXT_TELEMETRY_DISABLED 1 RUN addgroup --system --gid 1001 nodejs RUN adduser --system --uid 1001 nextjs COPY --from=builder /app/public ./public # Set the correct permission for prerender cache RUN mkdir .next RUN chown nextjs:nodejs .next # Automatically leverage output traces to reduce image size # https://nextjs.org/docs/advanced-features/output-file-tracing COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static USER nextjs EXPOSE 3000 ENV PORT 3000 # set hostname to localhost ENV HOSTNAME "0.0.0.0" # server.js is created by next build from the standalone output # https://nextjs.org/docs/pages/api-reference/next-config-js/output CMD ["node", "server.js"]
我的docker-compose.yml配置:
services: app: container_name: frontend build: context: . dockerfile: Dockerfile ports: - "3000:3000" depends_on: db: condition: service_healthy restart: always db: container_name: pgdb image: postgres:latest restart: always environment: POSTGRES_USER: postgres POSTGRES_PASSWORD: postgres POSTGRES_DB: docker ports: - "5433:5432" healthcheck: test: ["CMD-SHELL", "pg_isready -U postgres"] interval: 5s timeout: 5s retries: 5
解决方案
问题核心是容器间通信的端口与主机名配置错误,按以下步骤修复:
修正数据库连接端口
Docker Compose中,db容器内部的PostgreSQL默认运行在5432端口,宿主机的5433端口只是给外部工具(如pgAdmin)用的。容器间通信必须使用容器内部端口,所以Next.js应用的数据库连接配置里,端口要设为5432,而非5433。确认主机名使用
db
Docker Compose会自动为同一网络下的容器创建DNS解析,db是docker-compose.yml里定义的服务名,是容器间通信的标准主机名,不要用localhost或容器名pgdb。通过环境变量传递连接配置
建议在docker-compose.yml的app服务中添加环境变量,避免硬编码连接字符串:app: # 保留原有配置 environment: - DATABASE_URL=postgresql://postgres:postgres@db:5432/docker然后在Next.js项目中读取这个环境变量建立数据库连接。
验证容器网络连通性
如果仍有问题,可进入frontend容器测试与db的连通性:# 测试DNS解析 docker exec -it frontend ping db # 测试端口连通 docker exec -it frontend apk add telnet && telnet db 5432若能连通,说明网络正常,问题出在应用的连接配置上。
内容的提问来源于stack exchange,提问作者ReactLover
相关产品推荐
相关产品推荐

