skip to Main Content

When I run: docker run --rm -it redis, The container receives ip: 172.18.0.2. Then from the host I connect to the container with the following command: redis-cli -h 172.18.0.2, and it connects normally, everything works, the keys are added. Why does this happen without port forwarding? Default docker network – bridge

3

Answers


  1. It’s because the redis docker file exposes the right port for the api which is 6379.

    Login or Signup to reply.
  2. docker run --rm -it redis will not expose the port. Try stop the redis container. Then run redis-cli -h 172.18.0.2 to check if another redis exists.

    Login or Signup to reply.
  3. It is only possible because you’re on native Linux, and the way Docker networking is implemented, it happens to be possible to directly connect to the container-private IP addresses from outside Docker.

    This doesn’t work in a wide variety of common situations (on MacOS or Windows hosts; if Docker is actually running in a VM; if you’re making the call from a different host) and the IP address you get can change if the container is recreated. As such it’s not usually a best practice to look up the container-private IP address. Use docker run -p to publish a port, and connect to that published port and the host’s IP address.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search