2

currently I have one personal docker image uploaded to DockerHub, I am making the changes to connect this application to redis (that is running in another docker container as well)

Now, I have the next message from my app when it tries to connect to redis:

 Error saving to redis: dial tcp 127.0.0.1:6379: connect: connection refused

I understand that they're not in the same network, so my app is pointing to 127.0.0.1:6379 that is not executing anything, so, I am looking for the best way to connect those containers in some way that don't make my app depending of the IP where redis is hosted, from my local machine I can use redis, but not from another docker container. Briefly, what I did was:

docker run --name redis_server -p 6379:6379 -d redis
sudo docker run -d --restart=always -p 10000:10000 --link redis_server:redis --name my_app repo/my_app

So, I am looking for solutions on how to make 127.0.0.1:6379 accessible for my_app. I don't use docker-compose by the way

Sredny M Casanova
  • 4,735
  • 21
  • 70
  • 115
  • 1
    I believe running both containers with "--network=host" is your easiest solution. – Neekoy Sep 03 '18 at 16:20
  • @Neekoy thank you very much, it works!! what security concerns should I consider with "--network=host" do that makes me vulnerable or anything? – Sredny M Casanova Sep 03 '18 at 16:41
  • 1
    Well if you don't have a firewall this way you will expose the services on those ports to the outside world on your IP. A safer solution might be to define a network beforehand, and then place both containers on it. I'll write an answer in a bit. – Neekoy Sep 03 '18 at 16:47
  • Possible duplicate of [Docker Redis Connection refused](https://stackoverflow.com/questions/42360356/docker-redis-connection-refused) – David Maze Sep 03 '18 at 23:19

1 Answers1

3

The easiest solution would be to run Docker with "--network=host" which binds the containers to the host network. This is a perfectly fine solution for testing, however it will expose the ports and services respectively to the internet (if you don't have a firewall), which may or may not be secure.

Another way of doing this would be to define a network in the following way:

docker network create -d bridge my-net

And then to run your containers on that same network by running the docker run command with --network=my-net. This way you can reference each container by its name. For example in your case you can ping my_app from the Redis container and vice-versa.

Neekoy
  • 2,325
  • 5
  • 29
  • 48