Official mongo image (3.2.6) on docker beta for Mac - can't connect?

I’ve pulled the official repository for mongo from the docker hub - version 3.2.6. I’m running the docker beta for Mac, version 1.11.0.

I start a container with:

docker run --name container-name -d mongo

Then I run:

docker inspect --format ‘{{ .NetworkSettings.IPAddress }}’ container-name

When I ping the ip address from bash, I get a time-out. If I try to connect to using a mongo client like MongoChef using the ip address and the EXPOSED port, I get a timeout…

It seems so simple, yet something is eluding me. Is there anything that I may be missing?

You need to give a -p (or -P) option to cause Docker to publish a port. Something being EXPOSEd in a Dockerfile doesn’t cause anything to happen (except that -P will assign a port for it).

The examples on Docker show docker run --link to make the database accessible from other containers, which is different from accessing it from the host (or from other host).

If you ran, for instance,

docker run -d -p 27017:27017 mongo

then your host code could connect to localhost:27017 to access the database.

I’ve generally found using Docker’s native networking capabilities, like docker run -p to publish a specific port from the host, to work fine, and almost never need to know a container’s IP address. (The recent changes in Docker for Mac beta 9 make it work essentially the same way as Docker for native Linux.)

1 Like

Thank you very much David. That was indeed the problem for me. Appreciate the help.

1 Like

Interesting, leads me to ask why EXPOSE does not implicitly a publish?