lang2 lang2 - 1 year ago 146
Linux Question

docker: port mapping not working on non default machine

When playing with

I discovered something strange (at least to me). I created a container by:

docker run -p 8080:80 nginx

And no matter what I try. I couldn't see anything from
. Finally I figured out that I did

eval $(docker-machine env foo)

and switched to a different machine. When switching back to the default machine, the HTTP server works fine again.

Could somebody explain why this is?

I'm using Docker for Mac 1.12.

Answer Source

I can guess from your info that you're using Docker toolbox (with VirtualBox probably). Docker toolbox creates a linux host (based on the boot2docker image) and runs your nginx container inside that host. the -p 8080:80 will export port 80 in nginx to 8080 on the host. So to access it from your computer, you need to use http://HOST-IP-ADDRESS:8080.

You can find your host IP by using docker-machine ls

An alternative to Docker toolbox is to use Docker for Mac or Docker for Windows (google them). These remove the need for VirtualBox. Your computer essentially becomes the host for running docker containers. In that case, running http://localhost:8080 would give your your nginx server.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download