16

I run a docker container, which is named "redis". I want to use the "redis" container redis service, but I can't ping the container!

As the picture shows, my "redis" container is IP address is 172.17.0.15, but I can't connect to it.

I want to use the redis services. What is wrong with my configuration?

enter image description here

Paulo Merson
  • 13,270
  • 8
  • 79
  • 72
allencharp
  • 1,101
  • 3
  • 14
  • 31

1 Answers1

29

Because you're not on the same network. Containers are started on their own network by default, separate to the host's network.

If you run:

docker run -it debian ping 172.17.0.15

You should find it works. Even better, you can link containers and refer to them by name:

$ docker run -d --name redis redis
$ docker run --link redis:redis redis redis-cli -h redis ping
PONG

If you really want to access redis from your host, just publish a port through to the host:

$ docker run -d -p 6379:6379 redis

You should now be able to reach it at localhost:6379 on the host.

Adrian Mouat
  • 44,585
  • 16
  • 110
  • 102
  • how can you know the IP address is 172.17.0.15 before you call `docker run`? doesn't `docker run` dynamically assign an IP? – offwhitelotus Jan 25 '19 at 18:44
  • @offwhitelotus he is starting a new container from which the ip of the first container will be accessible \ relevant – Gil Hiram Sep 26 '20 at 13:35
  • 1
    How come I can ping my host through the container but not the other way around? Since theyre not on the same network, how can one find the other one but not the other way around? – Shervin Rad Feb 08 '21 at 17:09