My docker container failing to connect to mongodb database container [duplicate] - node.js

I'm new to docker. I'm trying to create a MongoDB container and a NodeJS container. My file looks:
version: '2'
services:
backend:
image: node:5.11-onbuild
ports:
- "3001:3001"
volumes:
- .:/code
working_dir: "/code"
links:
- mongodb
mongodb:
image: mongo:3.3
expose:
- 27017
It should run npm install and then node ..
But docker-compose up ends up with [MongoError: connect ECONNREFUSED 127.0.0.1:27017] while the command node ..
I think this is because of the bind_ip = 127.0.0.1 in the file /etc/mongod.conf. Is this right?
I use boot2docker on a Win10 system.
How can I solve this problem so that node can connect to the MongoDB?

In your backend app, connect to mongodb:27017 instead of 127.0.0.1:27017. Where 'mongodb' is the name of your service within docker-compose.yml.

I recently encountered similar issue. I am running docker toolbox under win 10 and this is how it worked for me:
1) I had to verify which URL my default docker machine is using. This can be checked by running docker-machine ls command. It will list available machines:
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
default * virtualbox Running tcp://192.168.99.100:1234 v17.06.0-ce
rancher-client - virtualbox Stopped Unknown
rancher-server - virtualbox Stopped Unknown
2) When running mongodb image specify the port mapping
docker run -d -it -p 27017:27017 mongo
3) At that point the valid mongo url would look something like this
var dbhost = 'mongodb://192.168.99.100:27017/test
where 192.168.99.100 was the default machine URL from the point 1)
Hope it helps someone.

Most likely, yes. 127.0.0.1 points to localhost inside the mongodb container, so is not accessible from outside the container. Binding to 0.0.0.0 will probably work.
With the link you specified in the docker-compose.yml, your backend container should then be able to connect to the mongo container through mongodb:27017

You have to tell the container to use it's own IP Address instead of localhost.
For example, let's assume you generated scaffold code with expressjs, you have to write in routes/index.js
var mongodb = require('mongodb');
router.get('/thelist', function(req, res){
// Get a Mongo client to work with the Mongo server
var MongoClient = mongodb.MongoClient;
// Define where the MongoDB server is
var url = 'mongodb://172.17.0.5:27017/dbname';
// Connect to the server
MongoClient.connect(url, function (err, db) {
.........
where 172.17.0.5 is the $CONTAINER_IP
you can find the container ip via
$ docker inspect $CONTAINER_HOSTNAME | grep IPAddress
If you still can't understand you can take a peek at my Docker NodeJS and MongoDB app

Related

Keep hitting connection error with docker for node mongo app [duplicate]

I'm new to docker. I'm trying to create a MongoDB container and a NodeJS container. My file looks:
version: '2'
services:
backend:
image: node:5.11-onbuild
ports:
- "3001:3001"
volumes:
- .:/code
working_dir: "/code"
links:
- mongodb
mongodb:
image: mongo:3.3
expose:
- 27017
It should run npm install and then node ..
But docker-compose up ends up with [MongoError: connect ECONNREFUSED 127.0.0.1:27017] while the command node ..
I think this is because of the bind_ip = 127.0.0.1 in the file /etc/mongod.conf. Is this right?
I use boot2docker on a Win10 system.
How can I solve this problem so that node can connect to the MongoDB?
In your backend app, connect to mongodb:27017 instead of 127.0.0.1:27017. Where 'mongodb' is the name of your service within docker-compose.yml.
I recently encountered similar issue. I am running docker toolbox under win 10 and this is how it worked for me:
1) I had to verify which URL my default docker machine is using. This can be checked by running docker-machine ls command. It will list available machines:
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
default * virtualbox Running tcp://192.168.99.100:1234 v17.06.0-ce
rancher-client - virtualbox Stopped Unknown
rancher-server - virtualbox Stopped Unknown
2) When running mongodb image specify the port mapping
docker run -d -it -p 27017:27017 mongo
3) At that point the valid mongo url would look something like this
var dbhost = 'mongodb://192.168.99.100:27017/test
where 192.168.99.100 was the default machine URL from the point 1)
Hope it helps someone.
Most likely, yes. 127.0.0.1 points to localhost inside the mongodb container, so is not accessible from outside the container. Binding to 0.0.0.0 will probably work.
With the link you specified in the docker-compose.yml, your backend container should then be able to connect to the mongo container through mongodb:27017
You have to tell the container to use it's own IP Address instead of localhost.
For example, let's assume you generated scaffold code with expressjs, you have to write in routes/index.js
var mongodb = require('mongodb');
router.get('/thelist', function(req, res){
// Get a Mongo client to work with the Mongo server
var MongoClient = mongodb.MongoClient;
// Define where the MongoDB server is
var url = 'mongodb://172.17.0.5:27017/dbname';
// Connect to the server
MongoClient.connect(url, function (err, db) {
.........
where 172.17.0.5 is the $CONTAINER_IP
you can find the container ip via
$ docker inspect $CONTAINER_HOSTNAME | grep IPAddress
If you still can't understand you can take a peek at my Docker NodeJS and MongoDB app

Cannot access mongodb inside docker container

I am currently containerizing my application inside docker. I have two images one for my node application and another for mongodb. I have link two using docker-compose. But whenever I try accessing mongodb inside container using the port 27017.
But it displays like this:
**
It looks like you are trying to access MongoDB over HTTP on the native
driver port.
**
I have mongodb config as:
"url": "mongodb://mongoDB/astroDB"
My docker-compose file is as :
version: '3.0'
services:
web:
image: astrobot-node
build: .
command: "yarn start"
ports:
- "80:3601"
depends_on:
- "mongo"
mongo:
image: mongo
ports:
- "27017:27017"
Can anyone tell me what's actually wrong. Thanks in advance.
But whenever I try accessing mongodb inside container using the port 27017. But it displays like this: **
It looks like you are trying to access MongoDB over HTTP on the native driver port.
This is expected behavior if you try to send HTTP requests to the MongoDB port. What this says to me is you got your network connectivity figured out and that part is working correctly.
Next you just need to use a MongoDB driver to talk to the database from the application instead of hitting it with curl or similar.
the standard mangodb connection url should look like this
mongodb://[username:password#]host1[:port1][,...hostN[:portN]][/[defaultauthdb][?options]]
https://docs.mongodb.com/manual/reference/connection-string/
In you case the mongoDB in your url string should indicate the db host, To make it points to the mangodb container you should change it to "url": "mongodb://mongo/astroDB", cause your service named mango not mangoDB.
you can also achieve that by provide a static IP to your container and write this IP directly, you can also find you container network IP by using the docker container inspect <container-IP> to test it manually.
EDIT
Here is a thread that should help you debugging your problem.
https://forums.docker.com/t/how-mongodb-work-in-docker-how-to-connect-with-mongodb/44763/8

Error: connect ECONNREFUSED 127.0.0.1:5432 docker-compose up

Not sure why am I getting the SequelizeConnectionRefusedError. I verified that I am able to run all my docker images locally but when I try to run 'docker-compose up' command, I am running into Error: connect ECONNREFUSED 127.0.0.1:5432.
Based on my understanding of your question, here are my assumptions:
You are using MacOS
Your Postgres server is running in the host OS instead of in another docker container.
With that being said, this is a common problem with MacOS users who want to connect their docker containers to the Postgres server running in the host machine. As they are not in the same network, there is no way for your container to reach the Postgres server and hence, connecting to it via 127.0.0.1:5432 will definitely not reachable.
It will be trivial to solve in a Linux machine by adding network_mode: host so that the containers will be running in the same network as host machine hence is able to reach the Postgres server. However, due to the implementation of Docker on Mac where Docker host is actually being run in a hidden VM on top of your MacOS, this solution will not work here.
Some suggestions:
Migrate your Postgres server to run in a docker container (in the same docker-compose file if you will). You can always do a port mapping in order to access it from your Postbird.
Or if you still insist on running it locally in your MacOS, here is a workaround that involves creating another docker container in the same docker network and perform a revert SSH tunneling.
Here are the steps to migrate the Postgres server to using docker container
Update your docker-compose with a new db service:
db:
image: postgres:10.5-alpine
environment:
POSTGRES_USER: $UDAGRAM_USERNAME
POSTGRES_PASSWORD: $UDAGRAM_PASSWORD
POSTGRES_DB: $UDAGRAM_DATABASE
ports:
- 35432:5432
volumes:
- <path where you want to persist your database data>:/var/lib/postgresql/data
You can now connect to your new postgres using Postbird at localhost:35432
EDIT 1
If you run your Postgres instance in AWS RDS, you will not need to make the changes above but follow other steps:
Make sure that your network can reach the RDS endpoint at port 5432. A best practice here is to update the security group inbound rules to allow only port 5432 from only your IP address (how to do that is out of the scope of this answer but can easily be found from AWS documentation)
Update the value of UDAGRAM_HOST to be the RDS endpoint which can be found from the AWS RDS console.

Connecting Redis localhost from Docker

I have a Node.js application where I use Redis, I am trying to connect the Docker container and the locally running Redis.
Tried solutions:
vim /usr/local/etc/redis.conf
Updated
bind 127.0.0.1
To
bind 0.0.0.0
Stopped the redis and start it again and tried running the docker
With the above thing tried running docker run -p 4000:8080 -p 6379:6379 -t node-app
Both above didn't worked getting the below error
Error: Redis connection to localhost:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
Update: I am checking it on Mac.
In Dockerfile add this
Docker v19.03
ENV REDIS_HOST "redis://host.docker.internal"
when i using it on node.js
const REDIS_HOST = process.env.REDIS_HOST ? process.env.REDIS_HOST : ""
const client = redis.createClient(REDIS_HOST)
"docker.for.mac.localhost" instead of localhost or '127.0.0.1' will work :), it worked for me on mac machine.
If you use default networking (--network="bridge"), you could simply use the IP address of the gateway between the Docker host and the bridge network, i.e. 172.17.0.1. Here is the documentation. This would work on all platforms, not only on a Mac.
You just need to set the docker internal host in your node app's config.json file:
"redisHost": "host.docker.internal"
You don't need to change any Redis configuration on your local.

Able to connect to MongoDB running in a Docker container locally, but not from another container

Basically what the title says: I am able to connect to my MongoDB database running in a Docker container locally, but I am unable to do so from within another Docker container. I am getting the error "MongoError: failed to connect to server {server address}:27017 on first connect"
Here is my config file:
const mongoose = require('mongoose');
mongoose.Promise = global.Promise
let mongoURI = {
development: 'mongodb://{server address}:27017/database,
}
const db = mongoose.connect(mongoURI.development, function(err, res) {
if(err) {
console.log('Error connecting to the database. ' + err);
} else {
console.log('Connected to Database: ' + mongoURI.development);
}
});
module.exports = db;
It's not clear from your question what configuration of Docker containers you're working with. There are a couple of possibilities:
Two Docker containers, one running a MongoDB image and another running your application, linked together via the --link option.
Assuming you gave the Mongo container a name via the --name option, you should be able to access the MongoDB via the container name: mongodb://{container name}:27017/database.
Two Docker containers NOT linked with the --link option.
In this case, you will need to have exposed the MongoDB port on the Mongo container with the -p option AND provided the application container with an IP address of your Docker host: mongodb://{docker host IP}:27017/database. Note that localhost will not work, since inside your application container localhost will refer to the application container. You could configure an entry in the application's /etc/hosts to give your Docker host a name.
MongoDB running in a container; application running on the Docker host (or some other server).
This is just a variation of #2 above. Again, you need to expose the ports and ensure that the application know how to find the host where the MongoDB container is running. In this case you could use localhost if the application in running on the Docker host.
Solved it, I should have specified this earlier: My configuration was the two containers not linked with the --link option, and MongoDB port exposed. The issue was that my company uses a proxy, and I was not disabling it properly when connecting to a server running on our domain.

Resources