Localhost port forwarder for Docker Windows Containers

Previously, when running a Windows Container, you had to jump through some hoops to access it from your host. In a browser for instance. On january 9th, a new version of Docker for Windows was released which makes this a lot easier.

Example

In this example, I’ve made a simple WCF service which I want to put into a container. It’s just a File > New Project > WCF Service in Visual Studio, the build results of which I have put in a folder called ‘website’.

I added the following Dockerfile:

FROM microsoft/wcf

WORKDIR c:\\inetpub\\wwwroot

EXPOSE 80

COPY website .

Now let’s open a command prompt and build the container:

C:\WcfDocker>docker build -t wcftest .
Sending build context to Docker daemon  119.3kB
Step 1/4 : FROM microsoft/wcf
 ---> 7944d007ef21
Step 2/4 : WORKDIR c:\\inetpub\\wwwroot
 ---> Using cache
 ---> 7823b5487041
Step 3/4 : EXPOSE 80
 ---> Using cache
 ---> 179896a03ccd
Step 4/4 : COPY website .
 ---> 102419c913e1
Successfully built 102419c913e1
Successfully tagged wcftest:latest

Then run it and confirm it’s actually running:

C:\WcfDocker>docker run -p 83:80 --name wcftest wcftest

C:\WcfDocker>docker ps -a
CONTAINER ID        IMAGE               COMMAND                   CREATED             STATUS              PORTS                         NAMES
7f3a925038de        wcftest             "C:\\ServiceMonitor.e…"   10 minutes ago      Up 10 minutes       808/tcp, 0.0.0.0:83->80/tcp   wcftest

Pretty basic. I get some warnings when running but it should all run fine.

Old situation

Now usually if I wanted to view this website in the browser, I would have to do the following (as per the microsoft/wcf documentation page as of today):

C:\WcfDocker>docker inspect -f "{{ .NetworkSettings.Networks.nat.IPAddress }}" wcftest
172.24.65.71

Then I can open the browser and visit the IP address:

Old way to access a Docker for Windows container.

Improvements!

With the new version of Docker for Windows (version Docker Community Edition 17.12.0-ce-win46 2018-01-09 (Stable), though it has been available on Edge for a while longer), we can finally do what was possible with Linux containers from the start. I can now actually access the website through the port I exposed when starting the container:

New way to access a Docker for Windows container.New way to access a Docker for Windows container.

Conclusion

I think this is a pretty nice improvement. Being able to access your containers in a more predictable way, without having to script the inspect command, helps make Windows containers more useful.

Now if we could mix Windows and Linux containers on the same host, we can containerize everything!

Leave a Reply

Your email address will not be published. Required fields are marked *