8

Created a apache webserver as Docker container but want to access it on windows os browser as localhost.

I can access the webserver with boot2docker private ip address which is 192.168.59.103 but would like to access the webserver as localhost i.e 127.0.0.1.

Following is my Docker Container setup

Running Boot2docker on Oracle VM Exposed ports : "EXPOSE 80 443" in docker file

Command used to create Docker File :

docker run --net=host --name=webserver1 -v /home/data:/data/www/www.samplewebserber.com -v `password`:/scripts -d folder/serverfolder  /scripts/run.sh
user1581721
  • 177
  • 3
  • 10
  • possible duplicate of [Docker, can't reach "rails server" development from localhost:3000 using docker flag -p 3000:3000](http://stackoverflow.com/questions/23422540/docker-cant-reach-rails-server-development-from-localhost3000-using-docker) – Joe Niland Feb 09 '15 at 05:31
  • Command user to create a dockerfile or to run a container? – vmonteco Mar 26 '15 at 13:50

2 Answers2

15

boot2docker actually created a vm with linux core in your Mac OS with VirtualBox, and 192.168.59.103 is the ip for that vm.

So you need to set a port forward for that vm

Notice that in Mac OS, port 80 need a high permission, so I use 8080 instead in this example.

enter image description here

Doug Hou
  • 530
  • 3
  • 14
6

If you want to access localhost to ports 80 and 443 you need to perform two actions:

  1. First, when you create your container, you must specify the port mapping specifically. If you run docker run with -P option, the ports set in dockerfile's EXPOSE will be expose to random ports in the Boot2Docker environment. If you want to map it specifically you must run:

    docker run \
      --net=host \
      --name=webserver1 \
      -v /home/data:/data/www/www.samplewebserber.com \
      -v `password`:/scripts \
      -d -p 80:80 -p 443:443 \
      folder/serverfolder  \
      /scripts/run.sh
    
  2. And in order to map Boot2Docker port to your host environment, as Joe Niland link suggested, you must do a port forwarding using SSH tunneling:

    boot2docker ssh -L 80:localhost:80

    boot2docker ssh -L 443:localhost:443

You can change to port mappings if you wish.

Ian Vaughan
  • 17,906
  • 13
  • 53
  • 70
Javier Cortejoso
  • 8,068
  • 3
  • 24
  • 27