Access Docker from external machine in network
Asked Answered
G

5

19

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.

Gabriel answered 15/8, 2014 at 13:12 Comment(0)
D
8

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/

Darondarooge answered 15/8, 2014 at 17:13 Comment(2)
Thank you, that helped a lot! Just if someone finds that answer useful, too: you have to add the protocol after proxy_pass (i.e. http://127.0.0.1:3000)Gabriel
Ah - sorry, it was hastily converted from a uwsgi config file! You're welcome.Darondarooge
O
22

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 =)

Organzine answered 12/4, 2016 at 8:31 Comment(7)
so helpful to note docker-machine ip. i was trying to use localhost to no avail.Kc
@RyanTuck glad this helped :-)Organzine
Can you add an update on how to configure the firewall to access the containers port from the local network?Sillimanite
awesome!! I have being trying without quite succeeding, to write a bash script to get the the port a running disposable container exposes from docker. 🍺Robena
On windows I get $ docker-machine ip Error: No machine name(s) specified and no "default" machine existsWhoreson
The error message is clear: you need to figure out your Docker machine's name and provide it to docker-machine ip .Organzine
Should note that these instructions are for Docker Toolbox and not Docker for Mac.Pieter
L
15

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

Losing answered 29/4, 2016 at 12:18 Comment(1)
This simple port forwarding did it for me too, thanks.Demeanor
D
8

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/

Darondarooge answered 15/8, 2014 at 17:13 Comment(2)
Thank you, that helped a lot! Just if someone finds that answer useful, too: you have to add the protocol after proxy_pass (i.e. http://127.0.0.1:3000)Gabriel
Ah - sorry, it was hastily converted from a uwsgi config file! You're welcome.Darondarooge
T
1

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

Theisen answered 25/7, 2018 at 10:25 Comment(0)
D
0

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

Derayne answered 15/8, 2014 at 16:43 Comment(4)
Thanks for your answer, unfortunately I have to use docker-osx, so I am not able to get the address by boot2docker.Gabriel
@DavidGeh i also run on OSX. If you do not have the ip subcommand on boot2docker, try upgrading github.com/boot2docker/osx-installer/releasesAvrom
Hey Abel, I use the following to run docker on osx, because i need to map local volumes with fig: github.com/noplay/docker-osx. This is not possible with boot2docker :-)Gabriel
Oh! I had forgotten that project, my bad! Looking at the code, looks like it only supports private networking… you will need to edit the docker-osx script so it generates a different Vagrantfile, but Nginx is also a good option :-)Avrom

© 2022 - 2024 — McMap. All rights reserved.