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