Search code examples
javascripttypescriptdockerredis

Docker & Redis - dynamically set IP address


I'm running a redis container, which I'm able to connect to fine from my local app using the following url: 'redis://default:redispw@localhost:6379'

However, this of course will not work when I want to deploy my local app inside of a docker container as well, as I wanted the redis docker container to communicate with my app's container.

Both of these containers are running on the same network, using the following command:

docker run --network bridge gateway:latest

However, in my code I had to hardcode in the IP of the redis container on that network - something which could of course change if I restart the container.

I am currently using the following url:

'redis://default:[email protected]:6379'

So my question is, what URL would I connect using so that I don't have to hardcode in a changing IP, or is there a way to set the redis container IP so that it can't change?


Solution

  • Have you tried replacing localhost with host.docker.internal? Depending on your docker deployment (version, os, etc.), that may be just the ticket. Another "trick" is to try 172.17.0.1.

    For more on both see these other SO posts:

    From inside of a Docker container, how do I connect to the localhost of the machine?

    What is the Linux equivalent of "host.docker.internal"?