Beginner
Beginner

Reputation: 9095

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:

  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.

Upvotes: 8

Views: 18261

Answers (4)

Eduardo Latorre
Eduardo Latorre

Reputation: 1

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.

Upvotes: 0

May Noppadol
May Noppadol

Reputation: 648

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)

Upvotes: 4

mfnx
mfnx

Reputation: 3018

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.

Upvotes: 2

Beginner
Beginner

Reputation: 9095

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

Upvotes: 4

Related Questions