10

On my MBP, with latest boot2docker installed, I have the following Dockerfile:

FROM redis:3.0.3
CMD redis-server --bind 0.0.0.0

I run the following:

docker build .
docker run --rm ba09b207db42 # where ba09b207db42 is the container id returned by the build command

Then I run:

redis-cli -h `boot2docker ip`

And I get the error:

Could not connect to Redis at 192.168.59.103:6379: Connection refused

What am I missing?

WispyCloud
  • 4,140
  • 1
  • 28
  • 31

1 Answers1

25

You forgot to expose the port. Simply run the container like this:

docker run --rm -p 6379:6379 ba09b207db42

Additionally:

  • You could give the image a name so you would not need to work with ids: docker build -t myimage .

  • You could then start the container in background so that it does not "block" your terminal: docker run --name mycontainer -d -p 6379:6379 myimage

xuesheng
  • 3,396
  • 2
  • 29
  • 38
Henrik Sachse
  • 51,228
  • 7
  • 46
  • 59
  • Thanks, I could see the port open in `docker ps` as the port is exposed by the redis image so I thought it wasn't needed but this definitely solved it (facepalm). – WispyCloud Aug 08 '15 at 09:08
  • 5
    The ports that you see with `docker ps` are open but only to other containers running on the same host. When you need to access them from outside of docker you need to specify such port mappings. – Henrik Sachse Aug 08 '15 at 09:09
  • 1
    Thanks a ton for the additional explanation! – WispyCloud Aug 08 '15 at 09:11
  • What if there is another container and we want to connect this redis inside that container? – Shiv Krishna Jaiswal Apr 02 '22 at 10:19