I'm building a simple node.js / express app where I us MongoDB Atlas for data storage. When I locally run the app using node index.js
everything works as expected. I can connect to MongoDB Atlas and read/write data.
Now I want this app to run inside a Docker container. The image is build without issues using docker build . -t IMAGENAME --network=host
(had to add --network=host as otherwise some dependencies can not be loaded). When I start the container using docker run -p 4000:3000 -d IMAGENAME:latest
the app starts as expected.
However, I can not connect to MongoDB Atlas and receive the following error:
Error: querySrv ETIMEOUT _mongodb._tcp.dbcluster0.kux1t.mongodb.net
at QueryReqWrap.onresolve [as oncomplete] (node:internal/dns/promises:251:17) {
errno: undefined,
code: 'ETIMEOUT',
syscall: 'querySrv',
hostname: '_mongodb._tcp.dbcluster0.kux1t.mongodb.net'
}
Any idea why I'm able to connect to Atlas without running Docker but not when running the image in a container?
My Dockerfile looks like this:
FROM node:lts-alpine
WORKDIR /backend
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD [ "node", "index.js" ]
This is the connection string I'm using (also tried format for node 2.2.12):
MONGO_URI='mongodb srv://username:[email protected]/database1?retryWrites=true&w=majority
CodePudding user response:
In the end it was related to my host network configuration. Running docker with the --network host
argument during build and run solved the issue