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

  • Docker undo rm container
  • Docker Container DNS resolution
  • Docker mysql_connect(): Can't connect
  • starting docker container from python messes up terminal settings
  • Windows Server 2016 docker image default username/password
  • How Docker swarm mode does load balancing?
  • 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.

  • .dockerignore mentioned files are not ignored
  • Docker-Compose with Docker 1.12 “Swarm Mode”
  • Use the result of RUN to set a variable
  • Updating rather than replacing ECS Task Definition with CloudFormation
  • Am I trying to connect to a TLS-enabled daemon without TLS?
  • Docker receiving multicast traffic
  • 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.