Running a WCF service in Docker

Posted in software by Christopher R. Wirz on Tue May 22 2018



Docker gained windows support in 2016. Since then, Windows Communication Foundation (WCF) services can run in Docker containers using the microsoft/wcf base image. Given this approach, it is straight-forward to containerize a WCF project.

Note: In order to do this, assume the service is published to a folder called "Published" within the root directory of the service project.

First, we need a "Dockerfile" (yes, the file is simply "Dockerfile", with no extension, placed in the root folder of the project):


# install WCF basic docker image
FROM microsoft/wcf

# Create a directory on the container
RUN mkdir C:\Service

# Copy to cointainer
COPY Published "C:\\Service"

# run IIS
RUN powershell -NoProfile -Command \
Import-module IISAdministration; \
New-IISSite -Name "Service" -PhysicalPath C:\Service -BindingInformation "*:83:"

# This instruction tells the container to listen on port 83.
EXPOSE 83

Open up powershell and move to the base directory of the WCF project. Since the Dockerfile is already created, Docker can be used to build and run the container image.


#######
### In Powershell
#######
# How to build
docker build -t wcf-iis-service .

# How to run
docker run -d -p 83:83 --name wcf wcf-iis-service

# List all the containers.  wcf should be listed
docker container ls --all

# How to find the IP address
docker inspect -f "{{.NetworkSettings.Networks.nat.IPAddress}}" wcf

# Wait for the user to press a key to shutdown the container
Write-Host -NoNewLine 'Press any key to shutdown wcf...';
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');

# How to stop the container
docker container stop wcf

# How to remove the container if needed
docker container rm wcf

# Show all containers.  wcf should not be listed
docker container ls --all