Error:
auth-service-1 | Error: getaddrinfo ENOTFOUND cluster0.random.mongodb.net
auth-service-1 | at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:107:26)
auth-service-1 | at GetAddrInfoReqWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
auth-service-1 | errno: -3008,
auth-service-1 | code: 'ENOTFOUND',
auth-service-1 | syscall: 'getaddrinfo',
auth-service-1 | hostname: 'cluster0.random.mongodb.net'
auth-service-1 | }
auth-service-1 |
auth-service-1 | Node.js v18.15.0
Dockerfile
FROM node:18-alpine
ARG SERVICE_PORT=8004
ARG NODE_ENV=dev
ENV NODE_ENV=${NODE_ENV}
# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
# Install app dependencies
COPY src/package.json /usr/src/app/
RUN cd /usr/src/app/ && npm install
# Bundle app source
COPY src /usr/src/app
EXPOSE ${SERVICE_PORT}
CMD ["sh", "-c", "npm run start:${NODE_ENV}"]
Locally API is able to connect to mongoDB cluster but inside docker container it can't able to connect, it is raising DNS error.
On mongoDB cluster I've Allow Access From Anywhere
and if it is working in my local machine without docker then it should also work with docker as the ip will remain same
In the docker-compose file you should have a network option for each service, when they are on the same network this error will not occur
services:
api:
networks:
backend
mongodb:
networks:
backend