Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 670
Learner Avatar asked Jan 31 '20 09:01

Learner


People also ask

Can I access localhost from docker?

Accessing the Host With the Default Bridge Mode You just need to reference it by its Docker network IP, instead of localhost or 127.0. 0.1 . Your host's Docker IP will be shown on the inet line. Connect to this IP address from within your containers to successfully access the services running on your host.

How do I pull and start the Redis Enterprise Software Docker container?

To pull and start the Redis Enterprise Software Docker container, run this docker run command in the terminal or command-line for your operating system. Note: On Windows, make sure Docker is configured to run Linux-based containers. docker run -d --cap-add sys_resource --name rp -p 8443:8443 -p 9443:9443 -p 12000:12000 redislabs/redis

How to access Redis containers from a remote server?

You can use the Docker port-forwarding function to access Redis containers from remote servers. 1. Define the port to be used for the remote connection: sudo docker run --name my-first-redis -p

How do I use localhost with Docker containers?

Docker provides a host network which lets containers share your host’s networking stack. This approach means localhost inside a container resolves to the physical host, instead of the container itself. Containers are launched with the host network by adding the --network=host flag: Now your container can reference localhost or 127.0.0.1 directly.

How do I connect to Redis in Linux terminal?

Connect to Redis with redis-cli Start the interactive redis-cli command shell using the following command: sudo docker exec -it my-first-redis sh Note: You can also use the unique container ID (b36263951bf4) instead of the container name.


3 Answers

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)
like image 134
May Noppadol Avatar answered Oct 19 '22 09:10

May Noppadol


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.

like image 20
mfnx Avatar answered Oct 19 '22 08:10

mfnx


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

like image 1
Learner Avatar answered Oct 19 '22 08:10

Learner