Access a Linux Docker Container From the Windows Docker-Host Host using Port Publishing (–P)

Well, that’s a fun title but I think it most accurately describes the topic of this post.  To explain further, I’m running Docker Toolbox 1.12.3 on Windows 7 Professional which utilizes VirtualBox (5.0.26) to host a Linux VM as the Docker Host in Linux. 

Lots of blog posts mention you can use the –p option with docker run to bind a port between the container and the host. 

https://docs.docker.com/engine/reference/commandline/run/#/publish-or-expose-port–p—expose

If you attempt to connect to that port using localhost/127.0.0.1 as these posts suggest, it fails:

image

Why?

The port binding is between the container and Docker host.  Our host is running in the VirtualBox virtual machine, so you have to connect to the IP of the VM instead of “localhost”.  Localhost is for the Windows operating system running VirtualBox, the Docker-Host Host.

Find the VM Ip

The first way I found to get the IP for the VirtualBox virtual machines was to open the terminal for the VM, run ifconfig and try each IP address.

image

In this case, we find the IP address “192.168.99.100” for the “eth1” network correctly bound to the right port.  Not sure if you can always assume “eth1” will be the right one but worth trying first.

  image

Another option I found was to lookup the Network Adapter name in VirtualBox and then running arp from a command prompt to find all IPs on that network.

image

>arp -a
...
Interface: 192.168.99.1 --- 0x25
  Internet Address      Physical Address      Type
  192.168.99.100        08-00-27-ae-dc-2f     dynamic
  192.168.99.255        ff-ff-ff-ff-ff-ff     static
  224.0.0.22            01-00-5e-00-00-16     static
  224.0.0.251           01-00-5e-00-00-fb     static
  224.0.0.252           01-00-5e-00-00-fc     static
  239.255.255.250       01-00-5e-7f-ff-fa     static

The dynamic IP is the winner. 

Docker Noobs Unite!

Granted I’m fairly new to Docker, I still struggle with questions/issues that I assume Docker experts think is trivial.  Regardless, I wanted to share my learnings for all my fellow Docker noobs. 

I also recently blogged about accessing a Docker container by its own IP which is provided by the private network that is created by the Docker host.

If this post was helpful or you have further questions please let me know in the comments below or on social media.

1 thought on “Access a Linux Docker Container From the Windows Docker-Host Host using Port Publishing (–P)”

  1. Pingback: Intro to Distributed Config with Consul on ASP.NET COre – dotnet catch

Leave a Reply