5

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:

  1. 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

  2. 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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Learner
  • 8,379
  • 7
  • 44
  • 82
  • 1
    Does this answer your question? [From inside of a Docker container, how do I connect to the localhost of the machine?](https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach) – leopal Jan 31 '20 at 09:33
  • docker run -p 8080:8080 -p 6379:6379 --network="bridge" -t node-app getting this error Error starting listen tcp 0.0.0.0:6379: bind: address already in use. – Learner Jan 31 '20 at 09:37
  • docker run -p 8080:8080 -p 6379:6379 --network="host" -t capacity-node this gives the same error Error: Redis connection to localhost:6379 failed - connect ECONNREFUSED 127.0.0.1:6379 – Learner Jan 31 '20 at 09:37
  • Bind `redis` instance to localhost instead of `0.0.0.0`. Verify you can connect from docker host to redis before starting working with docker. Then stop and rm all running containers of `node-app`. Finally start docker container `docker run --network="host" -t node-app`. It should work. Note it is not required to specify exposed ports as with network mode host all container's exposed ports are automatically exposed to docker's host. – leopal Jan 31 '20 at 09:45
  • "docker.for.mac.localhost" instead of localhost or '127.0.0.1' will work :), it worked for me – Learner Feb 04 '20 at 12:57

4 Answers4

4

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)
May Noppadol
  • 638
  • 8
  • 9
3

"docker.for.mac.localhost" instead of localhost or '127.0.0.1' will work :), it worked for me on mac machine.

Learner
  • 8,379
  • 7
  • 44
  • 82
  • 1
    Mac is not mentioned in the question. – mfnx Jun 17 '20 at 22:28
  • i didn't tagged platform as mac on it, sorry on that. added the info of the platform on the question. Thank you @mfnx – Learner Jun 18 '20 at 04:59
  • Why not simply access your localhost via de Docker0 network interface? From within the container, the ip 172.17.0.1 should point to your machine localhost. – mfnx Jun 18 '20 at 07:07
  • should but it didn't redis.exceptions.ConnectionError: Error 113 connecting to 172.26.0.1:6379. No route to host. – Nusrat Nuriyev Jan 04 '22 at 23:20
2

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.

mfnx
  • 2,894
  • 1
  • 12
  • 28
0

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.