Connect to docker-machine using 'localhost'

26,255

Solution 1

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

Solution 2

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 default
192.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.com
PING domain.com (192.168.99.100): 56 data bytes
64 bytes from 192.168.99.100: icmp_seq=0 ttl=64 time=0.294 ms
64 bytes from 192.168.99.100: icmp_seq=1 ttl=64 time=0.437 ms
64 bytes from 192.168.99.100: icmp_seq=2 ttl=64 time=0.556 ms
64 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.

Solution 3

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

Share:
26,255
bigblind
Author by

bigblind

to learn more about me check out my blog

Updated on February 19, 2020

Comments

  • bigblind
    bigblind about 4 years

    There are certain features, like JavaScript service workers without https, that only work on localhost, but when I run my app inside a docker container, using docker-compose, which runs on top of docker-machine, I need to connect to it using the address I get from

    docker-machine ip default
    

    Is there a way to map localhost to that ip?

  • Matt
    Matt over 8 years
    Good answer. I've normally seen the order of mappings described the other way around. You map localhost:8888 to vm:80 and port 80 on your docker machine to port 80 in the container, the way the packets flow.
  • Ezz Elkady
    Ezz Elkady over 7 years
    natpfN isn't a flag, it's a positional argument. The command should be vboxmanage controlvm default natpf1 "nameformapping,tcp,,8888,,80"
  • Kristofor Carle
    Kristofor Carle over 7 years
    Good catch, it is a flag for modifyvm but not controlvm... very confusing. I updated the answer to show both options
  • NeiL
    NeiL over 7 years
    what should i give in name for mapping ?? is it localhost ?
  • maks
    maks over 5 years
    with localhost this doesn't work, at lest with mongo. I mean if to add 192.168.99.100 localhost to etc/hosts it will not work if you want to connect to mongo just with mongo command(which uses localhost:27017 by default)