Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Docker from external machine in network

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.

like image 439
David Geh Avatar asked Aug 15 '14 13:08

David Geh


People also ask

How do I access Docker network from outside?

Your Docker container can connect to the outside world, but the outside world cannot connect to the container. To make the ports accessible for external use or with other containers not on the same network, you will have to use the -P (publish all available ports) or -p (publish specific ports) flag.

How do you expose Docker ports outside?

In order to make a port available to services outside of Docker, or to Docker containers which are not connected to the container's network, we can use the -P or -p flag. This creates a firewall rule which maps a container port to a port on the Docker host to the outside world.


1 Answers

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

like image 169
Abdo Avatar answered Oct 21 '22 07:10

Abdo