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?
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.
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:
or if the VM is running
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
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 thedocker-machine
to theetc\hosts
file in your local machine and map the port80
on your container to the port80
on thedocker-machine
.Example:
1) Get docker host ip address
2) Add this line to
etc/hosts
file in your local machine3) Check that your machine is resolving the domain.
Notes:
C:\Windows\System32\Drivers\etc\hosts