简体   繁体   中英

Cannot connect mongodb using docker, connection refused

I have the following dockerfile

# Install dependencies only when needed
 FROM node:16-alpine 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
 COPY package.json yarn.lock ./
 RUN yarn install --frozen-lockfile

 # Rebuild the source code only when needed
 FROM node:16-alpine AS builder
 WORKDIR /app
 COPY --from=deps /app/node_modules ./node_modules
 RUN yarn build
 COPY . .

 # Production image, copy all the files and run next
 FROM node:16-alpine AS runner
 WORKDIR /app

 ENV NODE_ENV production

 RUN addgroup -g 1001 nodejs
 RUN adduser -S 1001 nextjs

 # You only need to copy next.config.js if you are NOT using the default configuration
 COPY --from=builder /app/next.config.js ./
 COPY --from=builder /app/public ./public
 COPY --from=builder --chown=nextjs:nodejs /app/.next ./.next
 COPY --from=builder /app/node_modules ./node_modules
 COPY --from=builder /app/package.json ./package.json

 USER nextjs

 EXPOSE 3000

 # 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.
 ENV NEXT_TELEMETRY_DISABLED 1

 ENV HOST=0.0.0.0

 CMD yarn start

and the following docker-compose file

version: '3.5'

services:
  ellis-development:
    image: ellis-development
    container_name: app
    build:
      context: .
      dockerfile: dockerfile.dev
    environment:
      - NEXT_PUBLIC_ENV
      - SENDGRID_API_KEY
      - MONGODB_URI
      - NEXTAUTH_SECRET
      - NEXTAUTH_URL
    ports:
      - 3000:3000
    volumes:
      - .:/app
    links:
      - mongodb
    depends_on:
      - mongodb
  mongodb:
    image: mongo:latest
    # environment:
    #   MONGO_INITDB_ROOT_USERNAME: root
    #   MONGO_INITDB_ROOT_PASSWORD: testing123
    ports:
      - 27017:27017
    volumes:
      - data:/data/db

volumes:
  data:

I have all my envs setup in a.env file like so

NEXT_PUBLIC_ENV=local
SENDGRID_API_KEY=<redacted>
MONGODB_URI=mongodb://localhost:27017/<redacted>
NEXTAUTH_SECRET=eb141u85
NEXTAUTH_URL="http://localhost:3000"

This creates the following when running docker compose在此处输入图像描述

and I can connect to localhost:27017 using mongodb compass.

However, for some reason docker cannot connect to my application.

What am I doing wrong here? First time setting up mongodb with docker so ♂️

Fixed, spotted the error as soon as I posted the question...

Changed

MONGODB_URI=mongodb://localhost:27017/<redacted>

to

MONGODB_URI=mongodb://mongodb:27017/<redacted>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM