docker: port mapping not working on non default machine

When playing with docker 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 http://localhost:8080. Finally I figured out that I did

  • .dockerignore mentioned files are not ignored
  • Deploy my application to cluster of multiple vms using orchestration tools
  • Why does apache2 does not remove pid file on reboot on docker image
  • How can I know which docker will restart after docker daemon restarts
  • How avoid docker0 interface starting?
  • CoreOS - Unable to access /mnt due to Permissions issue on /var/lib/docker
  • 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.

  • Docker Compose - Flyway - Unable to obtain Jdbc connection from DataSource
  • Using compose in (fabric8io) docker-maven-plugin deactivates image build
  • Create new container with interactive shell
  • How to change proxy information for New Docker for Mac?
  • docker - multiple projects on one Dockerfile and docker-compose.yml
  • Windows Container with Docker: Cannot reach httpd in container from host (Win2016 TP5)
  • One Solution collect form web for “docker: port mapping not working on non default machine”

    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.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.