Docker Mongodb: MongoNetworkError: connect ECONNREFUSED 192.168.96.2:27017 - mongodb

When I run docker-compose locally, my app works fine. but when I push it on my web server, I get the following error :
connection error : MongoNetworkError: failed to connect to server
[mongo:27017] on first connect [MongoNetworkError: connect ECONNREFUSED
192.168.96.2:27017]
I don't get what's going on.
Here is my docker file :
FROM node:10
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3001
CMD ["node", "server"]
My docker-compose file :
version: '3'
services:
app:
depends_on:
- mongo
container_name: data-foot-api-container
restart: always
build: .
ports:
- '80:3001'
mongo:
image: mongo
container_name: mongodb
ports:
- '27017:27017'
volumes:
- /data/db:/data/db
volumes:
data-football:
If someone have an idea about how it could come from, I would really appreciate it

Thank you all guys for helping me.
It was indeed due to the fact that Node container was ready and running BEFORE mongo container.
Thus, Node was trying to reach mongo, but mongo wasn't up yet.
I found a way to force docker to wait for mongo to be ready before building node container here : https://dev.to/hugodias/wait-for-mongodb-to-start-on-docker-3h8b
There is probably other ways to do it, if someone knows another way or a better way, please comment.

Related

How do I properly set up my Keystone.js app to run in docker with mongo?

I have built my app which runs fine locally. When I try to run it in docker (docker-compose up) it appears to start, but then throws an error message:
Creating mongodb ... done
Creating webcms ... done
Attaching to mongodb, webcms
...
Mongoose connection "error" event fired with:
MongoError: failed to connect to server [localhost:27017] on first connect
...
webcms exited with code 1
I have read that with Keystone.js you need to configure the Mongo location in the .env file, which I have:
MONGO_URI=mongodb://localhost:27017
Here is my Docker file:
# Use node 9.4.0
FROM node:9.4.0
# Copy source code
COPY . /app
# Change working directory
WORKDIR /app
# Install dependencies
RUN npm install
# Expose API port to the outside
EXPOSE 3000
# Launch application
CMD ["node","keystone"]
...and my docker-compose
version: "2"
services:
# NodeJS app
web:
container_name: webcms
build: .
ports:
- 3000:3000
depends_on:
- mongo
# MongoDB
mongo:
container_name: mongo
image: mongo
volumes:
- ./data:/data/db/mongo
ports:
- 27017:27017
When I run docker ps it confirms that mongo is up and running in a container...
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f3e06e4a5cfe mongo "docker-entrypoint.s…" 2 hours ago Up 2 hours 0.0.0.0:27017->27017/tcp mongodb
I am either missing some config or I have it configured incorrectly. Could someone tell me what that is?
Any help would be appreciated.
Thanks!
It is not working properly because you are sending the wrong host.
your container does not understand what is localhost:27017 since it's your computer address and not its container address.
Important to understand that each service has it's own container with a different IP.
The beauty of the docker-compose that you do not need to know your container address! enough to know your service name:
version: "2"
volumes:
db-data:
driver: local
services:
web:
build: .
ports:
- 3000:3000
depends_on:
- mongo
environment:
- MONGO_URI=mongodb://mongo:27017
mongo:
image: mongo
volumes:
- "db-data:/data/db/mongo"
ports:
- 27017:27017
just run docker-compose up and you are all-set
A couple of things that may help:
First. I am not sure what your error logs look like but buried in my error logs was:
...Error: The cookieSecret config option is required when running Keystone in a production environment.Update your app or environment config so this value is supplied to the Keystone constructor....
To solve this problem, in your Keystone entry file (eg: index.js) make sure your Keystone constructor has the cookieSecret parameter set correctly: process.env.NODE_ENV === 'production'
Next. Change the mongo uri from the one Keystone generated (mongoUri: mongodb://localhost/my-keystone) to: mongoUri: 'mongodb://mongo:27017'. Docker needs this because it is the mongo container address. This change should also be reflected in your docker-compose file under the environment variable under MONGO_URI:
... environment: - MONGO_URI=mongodb://mongo:27017 ...
After these changes your Keystone constructor should look like this:
const keystone = new Keystone({
adapter: new Adapter(adapterConfig),
cookieSecret: process.env.NODE_ENV === 'production',
sessionStore: new MongoStore({ url: 'mongodb://mongo:27017' }),
});
And your docker-compose file, something like this (I used a network instead of links for my docker-compose as Docker has stated that links are a legacy option. I've included mine in case its useful for anyone else):
version: "3.3"
services:
mongo:
image: mongo
networks:
- appNetwork
ports:
- "27017:27017"
environment:
- MONGO_URI=mongodb://mongo:27017
appservice:
build:
context: ./my-app
dockerfile: Dockerfile
networks:
- appNetwork
ports:
- "3000:3000"
networks:
appNetwork:
external: false
It is better to use mongo db atlas if you does not want complications. You can use it in local and in deployment.
Simple steps to get the mongo url is available in https://www.mongodb.com/cloud/atlas
Then add a env variable
CONNECT_TO=mongodb://your_url
For passing the .env to docker, use
docker run --publish 8000:3000 --env-file .env --detach --name kb keystoneblog:1.0

Mongo restore docker container returns 'error connecting to db server'

I've been searching for an answer to this using this question here as the basis since last night, however I get this error that I can't solve:
Step 4/4 : RUN mongorestore /trapsdump/ --host mongo:27017
--->
Running in be3d84e526ab
2018-05-18T10:52:50.575+0000 Failed: error
connecting to db server: no reachable servers
I'm using Docker-compose for this. I've used Docker a few times before building containers, but am no expert, however I think everything is correctly configured. I'm using this to seed the database as we have a 'demo' collection stored on mLab.
This is my Dockerfile:
FROM node:9.11.1
RUN mkdir /src
RUN npm install nodemon -g
WORKDIR /src
ADD ./package.json /src/package.json
RUN npm install
COPY . .
EXPOSE 3000
CMD npm start
This is the docker-compose.yml:
version: "2"
services:
mongo:
container_name: mongo
image: mongo
volumes:
- ./data:/data/db
ports:
- "27017:27017"
- "11990:11990"
mongo-seed:
build: ./mongo-seed
links:
- mongo
app:
container_name: traps
restart: always
build: .
ports:
- "3000:3000"
links:
- mongo
And this is the mongo-seed/Dockerfile:
FROM mongo
RUN mongodump --uri=mongodb://xxxxxxxxxxxxx#xxxxxxxxxxxx:xxxxx/xxxxxx -o trapsdump
RUN mongorestore /trapsdump/ --host mongo:27017
If pulls down the collection from mLab and dumps it locally.
So if anyone can point out where i'm going wrong, that would be excellent.

Can't connect to mongo docker image with mongoose

I'm trying to run a simple docker setup with node and mongo:
Dockerfile:
FROM node:8.9.4-alpine
RUN mkdir /app
WORKDIR /app
COPY package.json /app/
COPY package-lock.json /app/
RUN npm install
ADD . /app/
docker-compose.yml:
version: '3'
services:
db:
image: 'mongo'
ports:
- "27017:27017"
api:
build: .
restart: always
command: sh -c "npm install && npm run start"
volumes:
- .:/app
ports:
- "3000:3000"
environment:
PORT: 3000
depends_on:
- db
Now in my app.js I'm connecting to mongo like that:
mongoose.connect('mongodb://localhost:27017')
.catch(err => {
console.log(err)
})
However I'm getting a failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]
The mongo container seems to boot up and run fine giving me waiting for connections on port 27017.
What's wrong with my setup? I also tried swapping out localhost for mongo when connecting, but it had no effect either.
I didn't realise I named my database container db instead of mongo, so all I had to do was to switch that name out in my app.js:
mongoose.connect('mongodb://db:27017')
.catch(err => {
console.log(err)
})
I think without mounting volumes in db service , container won't be able to continue its process. it will start for just a second and then it will be terminated.
so that's why you are getting an error.
you have to look at the port part of your container in the inspector, and my opinion is that you have to put : mongodb://0.0.0.0:27017/
enjoy !

Trying to connect a NodeJS app to MongoDB via Docker Compose

I'm following a MongoDB + NodeJS tutorial with my app. Everything works without Docker.. and I can in fact get the app to work up until it needs to connect to MongoDB.
If my app doesn't see MongoDB, it will print out an error and halt.
Here's my files
.env
NODE_VIEWS_PATH=../
NODE_PUBLIC_PATH=../
MONGODB_URI='mongodb://127.0.0.1:27017/myappsdb'
...
Dockerfile
FROM node:carbon
# Create app directory
WORKDIR /usr/src/mahrio
COPY package*.json ./
RUN npm install
# If you are building your code for production
# RUN npm install --only=production
COPY . .
EXPOSE 6085
CMD ["npm", "start"]
docker-compose.yml
version: "2"
services:
app:
container_name: someappname
restart: always
build: .
ports:
- "6085:6085"
links:
- mongo
depends_on:
- mongo
mongo:
container_name: mongo
image: mongo
volumes:
- ./tmp:/data/db
ports:
- "27017:27017"
When using docker-compose, for a container to connect to another container it can use the service name as a hostname to connect.
In your case, the node app needs to connect to mongo:27017 rather than localhost:27017, since localhost from the respective of the app container will refer to itself and not to your machine.
Therefore, change the mongo url to MONGODB_URI='mongodb://mongo:27017/myappsdb'. Also make sure that you consume the env file by adding:
app:
...
env_file:
- file.env

docker link resolves to localhost

I'm stuck on a very strange docker problem that I've not encountered before. What I want to do is to use docker-compose to make my application available from the internet. It's currently running on a instance on DigitalOcean and I'm currently working with the following docker-compose.yml:
version: '2.2'
services:
mongodb:
image: mongo:3.4
volumes:
- ./mongo:/data/db
ports:
- "27017"
mongoadmin: # web UI for mongo
image: mongo-express
ports:
- "8081:8081"
links:
- "mongodb:mongo"
environment:
- ME_CONFIG_OPTIONS_EDITORTHEME=ambiance
- ME_CONFIG_BASICAUTH_USERNAME=user
- ME_CONFIG_BASICAUTH_PASSWORD=pass
app:
image: project/name:0.0.1
volumes:
- ./project:/usr/src/app
working_dir: /usr/src/app
links:
- "mongodb:mongodb"
environment:
- NODE_ENV=production
command: ["npm", "start"]
ports:
- "3000:3000"
Mongoadmin connects properly and is able to connect to the database, while the database itself cannot be connected to from outside the host.
The problem is that the app won't connect to the right address. It is a express server using mongoose to connect to the database. Before connecting I'm logging the url it will connect to. In my config.js I've listed mongodb://mongodb/project, but this is resolved to localhost thus resulting in MongoError: failed to connect to server [localhost:27017] on first connect. The name of the container is resolved, but not to the proper address.
I've tried to connect to the IP (in the 172.18.0.0 range) that docker addressed to the container, but that also resolved to localhost. I've looked into /etc/hosts but this does not show anything related to this. Furthermore, I'm baffled because the mongo-express container is able to connect.
I've tried changing the name of the container, thinking it might be block for some reason due to previous runs or something like that, but this did not resolve the issue
I've tried both explicit links and implicit using dockers internal DNS resolve, but both did not work.
When binding port 27017 to localhost it is able to connect, but because of security and easy configuration via environment variables, I rather have the mongodb instance not bound to localhost.
I've also tried to run this on my local machine and that works as expected, being that both mongoadmin and app are able to connect to the mongodb container. My localmachine runs Docker version 1.12.6, build 78d1802, while the VPS runs on Docker version 17.06.2-ce, build cec0b72, thus a newer version.
Could this be a newly introduced bug? Or am I missing something else? Any help would be appreciated.
Your docker-compose file seems not have linked the app and mongodb container.
You have this:
app:
image: project/name:0.0.1
volumes:
- ./project:/usr/src/app
working_dir: /usr/src/app
environment:
- NODE_ENV=production
command: ["npm", "start"]
ports:
- "3000:3000"
While I think it should be this:
app:
image: project/name:0.0.1
volumes:
- ./project:/usr/src/app
working_dir: /usr/src/app
links:
- "mongodb:mongodb"
environment:
- NODE_ENV=production
command: ["npm", "start"]
ports:
- "3000:3000"