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.
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.
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
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
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.
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.
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)
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.
"docker.for.mac.localhost"
instead of localhost or '127.0.0.1'
will work :), it worked for me on mac machine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With