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?
Use --network="host" in your docker run command, then 127.0.0.1 in your docker container will point to your docker host. Note: This mode only works on Docker for Linux, per the documentation.
Docker Desktop 18.03+ for Windows and Mac supports host. docker. internal as a functioning alias for localhost . Use this string inside your containers to access your host machine.
Docker also finds ports you expose with --expose 8080 (assuming you want to expose port 8080). Docker maps all of these ports to a host port within a given epehmeral port range . You can find the configuration for these ports (usually 32768 to 61000) in /proc/sys/net/ipv4/ip_local_port_range .
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 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:
C:\Windows\System32\Drivers\etc\hosts
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With