Access Docker from external machine in network

MacOS

Question or issue on macOS:

Is it possible to access an docker service from an external device?
I built the service via fig and exposed the port 3000. I use fig with docker-osx, so docker is running inside a virtualbox.

Now I need to access the service provided from an external device (i.e. a mobile phone or tablet).

At the moment I could only access the service with localdocker:3000 from the machine hosting the VirtualBox-Environment.

How to solve this problem?

Solution no. 1:

You’ll have to tell your local machine to listen for incoming connections on that port and then forward those requests on to your docker container.

Nginx is pretty good at this, and a simple config like this:

/etc/nginx/sites-enabled/your-file.conf

server {                                                                   
    listen 3000;                                                              

    server_name YOUR_IP_ADDRESS;                                              

    proxy_redirect off;                                                       
    proxy_buffering off;                                                      
    proxy_set_header Host $host;                                              
    proxy_set_header X-Real-IP $remote_addr;                                  
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;              

    location / {                                                              
        proxy_pass http://127.0.0.1:3000;                                            
    }                                                                         
} 

Would work fine if your phone / tablet hits http://YOUR_IP_ADDRESS:3000/

Solution no. 2:

For those using OSX (and Windows) for testing, Docker creates a virtual machine; this works a little differently than running on a Linux-based system.

Try the following:

docker-machine ip

This will return the virtual machine’s IP. In my example, it’s

192.168.99.100

Running docker ps will show you the port mappings (cleaned up the table below a bit)

$ docker ps
CONTAINER ID        IMAGE                STATUS              PORTS                   NAMES
42f88ac00e6f        nginx-local          Up 30 seconds       0.0.0.0:32778->80/tcp

0.0.0.0:32778->80/tcp means docker is mapping 32778 (a randomly assigned port) on my machine (in this case the virtual machine) to my container’s port 80.

You can also get this information from docker port 42f88ac00e6f 80 (42f88ac00e6f being the container ID or name)

In order to access nginx on the container, I can now use the virtual machine’s ip:32778

http://192.168.99.100:32778/ will forward to my docker container’s 80 port (I use this to test locally)

Obviously, the port above will not be accessible from the network but you can configure your firewall to forward to it =)

Solution no. 3:

I suggest adding a port forwarding rule to the VirtualBox VM settings.

Open the VM settings => Network tab => Adapter 1. By default it is attached to NAT.

Press Port forwarding button, then add a new rule.

The Host IP should be your computer IP address. Could be also 127.0.0.1, but then it will be seen only on your computer.

For the Host Port value you will need to experiment a bit – needs to be both unused and allowed by the computer firewall.

Leave the Guest IP empty.

The Guest Port should be 3000, as in your question.

After that, it should be accessible from the local network, address http://HOST_IP:HOST_PORT

Solution no. 4:

For MacOs users.

it seems like sudo ifconfig lo0 alias 10.254.254.254 will do the magic.
you can access external IP host (10.254.254.254) from container

Solution no. 5:

You should be able to access the boot2docker vm by using the IP address reported by book2docker ip.

Hope this helps!