How to connect to a local Redis server from a Docker container?

15,560

Solution 1

You should not connect to IP address of the container, but IP of the host (one you see on host for Docker bridge). Looking at your question it should be 172.17.0.1

Solution 2

You can do this:

  • Get IP of your host machine, e.g. in an environment variable $host_ip
  • Run docker command like:
    docker run -it --add-host redis_server:$host_ip ubuntu bash
    

Now from inside container you will be able to reach Redis server via hostname redis_server running on your host machine.

Share:
15,560

Related videos on Youtube

kidman01
Author by

kidman01

Updated on September 15, 2022

Comments

  • kidman01
    kidman01 over 1 year

    I'm trying to connect from inside a Docker container to a local Redis server on my machine. What I did so far:

    • Redis is up an running on my machine (can connect to it via redis-cli)
    • started an interactive Docker container (from the Ubuntu image)
    • installed "redis-tool" inside the container
    • tried to connect to Redis via redis-cli:
      > redis-cli -h 172.17.0.3 -p 6379
      (got the IP address via ifconfig inside the container)
    • got an error saying "connection refused"

    What's the trick I'm not thinking of in order to get a working connection?

    I already tried to set "bind 0.0.0.0" in my redis.conf but that had no effect. I also tried to forward the port 6379 to 6379 when running the container but I get an error saying the address is already in use.

    Thanks in advance!

  • kidman01
    kidman01 over 6 years
    Thank you so much!
  • kidman01
    kidman01 over 6 years
    Thank you for the comment, but I wasn't looking to run redis inside a container!
  • Senio Caires
    Senio Caires over 6 years
    Sorry. So try inside redis-cli container: redis-cli -h 0.0.0.0 or redis-cli -h HostIP. 172.17.0.3 is container IP. Use -h HostIP or -h 0.0.0.0