Connecting Redis localhost from Docker - node.js

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.

Related

My docker container failing to connect to mongodb database container [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

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

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.

docker container cannot connect to localhost mongodb [duplicate]

This question already has answers here:
Connect to host mongodb from docker container
(4 answers)
Closed 4 years ago.
A. I have a container that includes the following
1. NodeJS version 8.11.4
2. Rocketchat meteor app
B. This is my Dockerfile
FROM node:8.11.4
ADD . /app
RUN npm install -g node-gyp
RUN set -x \
&& cd /app/programs/server/ \
&& npm install \
&& npm cache clear --force
WORKDIR /app/
ENV PORT=3000 \
ROOT_URL=http://localhost:3000
EXPOSE 3000
CMD ["node", "main.js"]
C. This command is executed well
docker build -t memo:1.0 .
When I try to run the container, it encounters the following error in containers log
{"log":"MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]\n","stream":"stderr","time":"2019-01-24T21:56:42.222722362Z"}
So container can not be executed.
The mongodb is running and I've added 0.0.0.0 to bindIp in the mongod.conf file.
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1,0.0.0.0 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
My mongodb is installed in host(outside the container)
The problem was not resolved and my container status is Exited
I put the IP instead of the localhost,but it encounters the following error
{"log":"MongoNetworkError: failed to connect to server [192.168.0.198:27017] on first connect [MongoNetworkError: connect EHOSTUNREACH
The problem here is that you're starting a docker container (a self contained environment) and then trying to reach localhost:27017. However, localhost inside your container is not the same localhost as outside your container (on your host). There are two approaches you could take from this point:
Instead of attempting to connect to localhost:27017, connect to your host's ip (something like 192.x.x.x or 10.x.x.x)
(Better option imo) dockerize your mongodb, then your services will be able to communicate with each other using docker dns. To do this, you would create a docker-compose.yml with one service being your app and the other being mongodb.
When you try with MONGO_URL=mongodb://192.168.0.198:27017/rocketchat add that IP to bindIp as well.
With localhost MONGO_URL=mongodb://127.0.0.1:27017/rocketchat
I also recommend enabling security authorization in mongo config. Then set a user and password for your database.
Keep in mind that any change to config file requires a mongo restart

Connect to host mongodb from docker container

So I want to connect to my mongodb running on my host machine (DO droplet, Ubuntu 16.04). It is running on the default 27017 port on localhost.
I then use mup to deploy my Meteor app on my DO droplet, which is using docker to run my Meteor app inside a container. So far so good.
A standard mongodb://... connection url is used to connect the app to the mongodb.
Now I have the following problem:
mongodb://...#localhost:27017... obviously does not work inside the docker container, as localhost is not the host's localhost.
I already read many stackoverflow posts on this, I already tried using:
--network="host" - did not work as it said 0.0.0.0:80 is already in use or something like that (nginx proxy)
--add-host="local:<MY-DROPLET-INTERNET-IP>" and connect via mongodb://...#local:27017...: also not working as I can access my mongodb only from localhost, not from the public IP
This has to be a common problem!
tl;dr - What is the proper way to expose the hosts localhost inside a docker container so I can connect to services running on the host? (including their ports, e.g. 27017).
I hope someone can help!
You can use: 172.17.0.1 as it is the default host ip that the containers can see. But you need to configure Mongo to listen to 0.0.0.0.
From docker 18.03 onwards the recommendation is to connect to the special DNS name host.docker.internal
For previous versions you can use DNS names docker.for.mac.localhost or docker.for.windows.localhost.
change the bindIp from 127.0.0.1 to 0.0.0.0 in /etc/mongod.conf. Then it will work
or start mongod on ubuntu with a flag to bind all ip address as a temporary workaround (dev/learning purposes)
$ mongod --bind_ip_all
Tried 100500 variants for Windows (using docker desktop), but without any result...
Unfortunately, currently, Windows (at least docker desktop) is not supporting --net=host
Quoted from: https://docs.docker.com/network/network-tutorial-host/#prerequisites
The host networking driver only works on Linux hosts, and is not supported on Docker for Mac, Docker for Windows, or Docker EE for Windows Server.
You can try to use https://docs.docker.com/toolbox/

Resources