I have a mongo replica set (version 3.6) like this
mongodb1. corp. com(10.10.10.1) mongodb2. corp. com(10.10.10.2) mongodb3. corp. com(10.10.10.3)
and I have a nodejs application to connect to this RS, and it works fine in dev,qa,prod without docker.
the mongodb connection string like this
now we are going to use docker to deploy the app, so I made a image for my app, the docker file is like below.
FROM node:6 RUN mkdir /usr/src/app # Create app directory WORKDIR /usr/src/app COPY package.json . ... CMD ["grunt"] #Expose the port EXPOSE 8000 CMD ["node", "/usr/src/app/server.js"]
after all, I use this command to run docker but get the error below as well
sudo docker run -e NODE_ENV=dev -p 8000:8000 dockerrepo/$TAGVERSION
error: failed to connect to server [mongodb1.corp.com:27017] on first connect [MongoError: getaddrinfo ENOTFOUND mongodb1. corp. com mongodb1. corp . com:27017]
Docker version 18.06.0-ce, build 0ffa825
you can get that in connection string I used ip address, but in error info it shows hostname, so it means the dns and network is good, right?
please let me know if you need more info, thanks.
and need to mention again that, all the code work fine in dev, qa, prod without docker. and the error msg wouldnt be mongodb1. corp. com always, its random, maybe mongodb1, mongodb2, or mongodb3