Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect to docker-machine using 'localhost'

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?

like image 624
bigblind Avatar asked Feb 12 '16 21:02

bigblind


People also ask

How do I access Docker on localhost?

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.

Does localhost work in Docker?

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.

Does Docker use port 8080?

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 .


2 Answers

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

like image 183
Kristofor Carle Avatar answered Sep 28 '22 04:09

Kristofor Carle


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.
like image 30
Hemerson Varela Avatar answered Sep 28 '22 04:09

Hemerson Varela