Connect to docker-machine using 'localhost' Connect to docker-machine using 'localhost' docker docker

Connect to docker-machine using 'localhost'


You can add a VirtualBox port forward to map a port on the docker host to your local machine.

Assuming your docker machine is called "default" and you want to map port 80 in your container to localhost:8888 you can run:

vboxmanage modifyvm default --natpf1 "nameformapping,tcp,,8888,,80"

or if the VM is running

vboxmanage controlvm default natpf1 "nameformapping,tcp,,8888,,80"

This can also be done in the VirtualBox UI in the settings for the VM. Here is the doc from VirtualBox https://www.virtualbox.org/manual/ch06.html#network_nat

You'll also need to map the port on your container to the port on the docker machine, you do that when you start the container (this also assumes that you have a "EXPOSE 80" command in your Dockerfile

docker run -p 80:80 mycontainer

https://docs.docker.com/engine/reference/run/

Also see: https://github.com/boot2docker/boot2docker/blob/master/doc/WORKAROUNDS.md


Editing your hosts file causes that your local machine only looks directly to the IP address specified for a domain. So, you could add the ip address of the docker-machine to the etc\hosts file in your local machine and map the port 80 on your container to the port 80 on the docker-machine.

Example:

1) Get docker host ip address

$ docker-machine ip default192.168.99.100

2) Add this line to etc/hosts file in your local machine

192.168.99.100 domain.com

3) Check that your machine is resolving the domain.

$ ping domain.comPING domain.com (192.168.99.100): 56 data bytes64 bytes from 192.168.99.100: icmp_seq=0 ttl=64 time=0.294 ms64 bytes from 192.168.99.100: icmp_seq=1 ttl=64 time=0.437 ms64 bytes from 192.168.99.100: icmp_seq=2 ttl=64 time=0.556 ms64 bytes from 192.168.99.100: icmp_seq=3 ttl=64 time=0.270 ms

Notes:

  • For Windows users the hosts file is localted at C:\Windows\System32\Drivers\etc\hosts
  • If you want to support multiple domains in just one single docker-machine, you can create a proxy-container with nginx inside on front of your other containers.


The easiest way is to make port forwarding from the VBox

Settings->Network->Adapter 1->Port Forwarding

then add Name, in host add 127.0.0.1(for local host) then proper port bindings and restart the VM.

enter image description here