Dockerized Nextjs App can't access MongoDB Container
Answered
Clytax posted this in #help-forum
ClytaxOP
I have created a MongoDB container and a Next.js container. For some reason, I can access the MongoDB container with 'localhost' when running Next.js locally (not in a container). But as soon as I start the container, the app doesn't have access anymore.
Here is my Docker Compose for the MongoDB database:
and this is my Dockerfile for the client app. I added the ENVs just to try it but it's probably not needed
I don't understand why I can't access the database when inside the Docker website. However, connecting locally works as usual. I tried using 'mongo', 'mongo-1', or 'mongodb' instead of 'localhost', but it's still not working.
my env varibles are in .env.local
This is the first time I am using docker
Here is my Docker Compose for the MongoDB database:
version: "3.8"
services:
mongo:
image: mongo:7.0
environment:
MONGO_INITDB_ROOT_USERNAME: clytax
MONGO_INITDB_ROOT_PASSWORD: 12345678
ports:
- 27017:27017
volumes:
- mongodata:/data/db
volumes:
mongodata:
driver: localand this is my Dockerfile for the client app. I added the ENVs just to try it but it's probably not needed
FROM node:20
ENV MONGODB_URI mongodb://clytax:12345678@localhost:27017
ENV NEXTAUTH_SECRET <removed>
ENV NEXT_PUBLIC_API_URL http://localhost:3000/api
ENV JWT_SECRET <removed>
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "run", "dev"]I don't understand why I can't access the database when inside the Docker website. However, connecting locally works as usual. I tried using 'mongo', 'mongo-1', or 'mongodb' instead of 'localhost', but it's still not working.
my env varibles are in .env.local
This is the first time I am using docker
7 Replies
version: "3.8"
services:
mongo:
image: mongo:7.0
environment:
MONGO_INITDB_ROOT_USERNAME: clytax
MONGO_INITDB_ROOT_PASSWORD: 12345678
ports:
- 27017:27017
volumes:
- mongodata:/data/db
networks:
- my_network
web:
build: ./path-to-dockerfile
ports:
- 3000:3000
networks:
- my_network
volumes:
mongodata:
driver: local
networks:
my_network:
external: true@Ray docker
version: "3.8"
services:
mongo:
image: mongo:7.0
environment:
MONGO_INITDB_ROOT_USERNAME: clytax
MONGO_INITDB_ROOT_PASSWORD: 12345678
ports:
- 27017:27017
volumes:
- mongodata:/data/db
networks:
- my_network
web:
build: ./path-to-dockerfile
ports:
- 3000:3000
networks:
- my_network
volumes:
mongodata:
driver: local
networks:
my_network:
external: true
ClytaxOP
Thank you! do I need to change something to my nextjs docker then or do I use my_network instead of localhost?
@Clytax Thank you! do I need to change something to my nextjs docker then or do I use my_network instead of localhost?
yeah the url for mongodb
mongodb://clytax:12345678@mongo:27017you can connect other container with the name in the same network
ClytaxOP
amazing it works! thanks alot. is there away to get the same network without docker compose? I have to do it for a class exercise, not sure if we can use docker compose for both
@Clytax amazing it works! thanks alot. is there away to get the same network without docker compose? I have to do it for a class exercise, not sure if we can use docker compose for both
yes, you can do that with cli
docker network create my_network
docker network connect my_network web
docker network connect my_network mongo