10
votes

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 (cann 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!

3

3 Answers

9
votes

You should not connect to ip of container but ip of host (one you see on host for docker bridge). Looking at your question it should be 172.17.0.1

2
votes

You can do this:

  • Get IP of your host machine, e.g. in ENV 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

-1
votes

Use link between containers.
docker run -d --name=redis_xyz redis

docker run -it --link redis_xyz:redis_local redis_cli_image redis-cli -h redis_local