|
| 1 | +# WCF Docker Sample |
| 2 | +This sample demonstrates how to dockerize WCF services, either IIS-hosted or self-hosted. A simple "hello world" service contract is used in all samples for both HTTP and NET.TCP transport bindings. The sample can also be used without Docker. |
| 3 | + |
| 4 | +The sample builds the application in a container based on the larger [.NET Framework SDK Docker image](https://hub.docker.com/r/microsoft/dotnet-framework-build/). It builds the application and then copies the final build result into a Docker image based on the smaller [WCF Runtime Docker image](https://hub.docker.com/r/microsoft/wcf/) or [.NET Framework Runtime Docker image](https://hub.docker.com/r/microsoft/dotnet-framework/). It uses Docker [multi-stage build](https://github.com/dotnet/announcements/issues/18) and [multi-arch tags](https://github.com/dotnet/announcements/issues/14). |
| 5 | + |
| 6 | +This sample requires [Docker 17.06](https://docs.docker.com/release-notes/docker-ce) or later of the [Docker client](https://store.docker.com/editions/community/docker-ce-desktop-windows). |
| 7 | + |
| 8 | +## Try pre-built WCF Docker Images |
| 9 | +You can quickly run a container with a pre-built [sample WCF Docker image](https://hub.docker.com/r/microsoft/dotnet-framework-samples/), based on the WCF Docker sample. |
| 10 | + |
| 11 | +Type the following [Docker](https://www.docker.com/products/docker) command to start a WCF service container: |
| 12 | +```console |
| 13 | +docker run --name wcfservicesample --rm -it microsoft/dotnet-framework-samples:wcfservice |
| 14 | +``` |
| 15 | +Find the IP address of the container instance. |
| 16 | +```console |
| 17 | +docker inspect --format="{{.NetworkSettings.Networks.nat.IPAddress}}" wcfservicesample |
| 18 | +172.26.236.119 |
| 19 | +``` |
| 20 | +Type the following Docker command to start a WCF client container, set environment variable HOST to the IP address of the wcfservicesample container: |
| 21 | +```console |
| 22 | +docker run --name wcfclientsample --rm -it -e HOST=172.26.236.119 microsoft/dotnet-framework-samples:wcfclient |
| 23 | +``` |
| 24 | + |
| 25 | +## Getting the sample |
| 26 | + |
| 27 | +The easiest way to get the sample is by cloning the samples repository with [git](https://git-scm.com/downloads), using the following instructions. |
| 28 | + |
| 29 | +```console |
| 30 | +git clone https://github.com/microsoft/dotnet-framework-docker/ |
| 31 | +``` |
| 32 | + |
| 33 | +You can also [download the repository as a zip](https://github.com/microsoft/dotnet-framework-docker/archive/master.zip). |
| 34 | + |
| 35 | +## Build and run the sample with Docker |
| 36 | +WCF service is supported on .NET Framework, which can run in Windows Server Core based containers. For simplicity, we disabled security in these samples. |
| 37 | + |
| 38 | +### Build a Container with IIS-hosted WCF Service |
| 39 | +Project `WcfServiceWebApp` is created from 'WCF Service Application' template in Visual Studio. A [Dockerfile](/Dockerfile.web) is added to the project. We use `microsoft/wcf` image as the base image, which has both HTTP and NET.TCP protocols enabled in IIS and exposes ports 80 (for HTTP) and 808 (for NET.TCP) for the container. We use WCF image with tag `4.7.2` for .NET Framework 4.7.2 in this example, but you can change it to use other tags (eg. `microsoft/wcf:4.7` with tag `4.7`) for WCF images with different versions of .NET Framework. The complete list of [WCF image tags](https://hub.docker.com/r/microsoft/wcf/tags/) can be found from Docker Hub. |
| 40 | + |
| 41 | +Run commands below to build the container image with name `iishostedwcfservice` and start an instance of it named `myservice1`. Docker parameter `-d` will run the container in background (detached mode). |
| 42 | +``` |
| 43 | +cd samples |
| 44 | +cd wcfapp |
| 45 | +docker build --pull -t iishostedwcfservice -f Dockerfile.web . |
| 46 | +docker run -d --rm --name myservice1 iishostedwcfservice |
| 47 | +``` |
| 48 | +Find the IP address of the container instance. This will be used later for a WCF client to connect to the service. |
| 49 | +``` |
| 50 | +docker inspect --format="{{.NetworkSettings.Networks.nat.IPAddress}}" myservice1 |
| 51 | +172.23.70.146 |
| 52 | +``` |
| 53 | +### Build a Container with Self-hosted WCF Service |
| 54 | +Project `WcfServiceConsoleApp` is created from Windows Classic Desktop 'Console App' template in Visual Studio. We added a [Dockerfile](/Dockerfile.console) to the project. We use `microsoft/dotnet-framework` image as the base image and expose ports 80 (for HTTP) and 808 (for NET.TCP) for the container. |
| 55 | + |
| 56 | +Run commands below to build the container image with name `selfhostedwcfservice` and start an instance of it named `myservice2`. |
| 57 | +``` |
| 58 | +docker build --pull -t selfhostedwcfservice -f Dockerfile.console . |
| 59 | +docker run -it --rm --name myservice2 selfhostedwcfservice |
| 60 | +``` |
| 61 | +Open another console window to find the IP address of the self-hosted WCF service (alternatively, you can run the self-hosted WCF service container in detached mode by `docker run -d --rm --name myservice2 selfhostedwcfservice`). |
| 62 | +``` |
| 63 | +docker inspect --format="{{.NetworkSettings.Networks.nat.IPAddress}}" myservice2 |
| 64 | +172.23.69.75 |
| 65 | +``` |
| 66 | +### Build a Container to run WCF Client against the Service |
| 67 | +Now that we have WCF services running in containers. Let's run the WCF client against them. Run commands below to build the container image with name `wcfclient`. The IP address of the WCF service is passed through the environment variable `host`. |
| 68 | +``` |
| 69 | +docker build --pull -t wcfclient -f Dockerfile.client . |
| 70 | +docker run -it --rm -e HOST=172.23.70.146 --name myclient wcfclient |
| 71 | +Client OS: Microsoft Windows NT 6.2.9200.0 |
| 72 | +Service Host: 172.23.70.146 |
| 73 | +Hello WCF via Http from Container! |
| 74 | +Hello WCF via Net.Tcp from Container! |
| 75 | +``` |
| 76 | +The result above is from running the WCF client against the IIS-hosted WCF service. Changing the environment variable `host` to the IP address of the self-hosted WCF service, you will get a similar result. |
| 77 | + |
| 78 | +*To run WCF client on a different machine other than the one that hosts container instance*, we will need to map ports of a container to ports of its host by using Docker parameter `-p` when we start a constainer instance. For example, the command below started an instance of the self-hosted WCF service container named `myservice3` with its ports 80 and 808 mapped to ports 80 and 808 of its host machine respectively. |
| 79 | +``` |
| 80 | +C:\wcfapp\WcfServiceConsoleApp>docker run -d -p 80:80 -p 808:808 --name myservice3 selfhostedwcfservice |
| 81 | +``` |
| 82 | +Then for the WCF client to connect to the service, we need to set the `host` to be the IP address (or DNS name) of the container host machine (instead of the IP address of the container instance). The rest will be the same to start the WCF client. |
| 83 | + |
| 84 | +## Build and run the sample with Docker Compose |
| 85 | +[Docker Compose](https://docs.docker.com/compose/overview/) is a tool for defining and running multi-container Docker applications. In this sample, we also added YML files to configure WCF server and client applications. You can directly start and run service and client applications without having to do any work to hookup the client to the WCF service. |
| 86 | + |
| 87 | +Type the following Docker Compose command to start both iis-hosted WCF service container and client container: |
| 88 | +``` |
| 89 | +docker-compose -f docker-compose-iishosted.yml up |
| 90 | +``` |
| 91 | +Alternatively, if you want to build and run self-hosted WCF service container, run commands below: |
| 92 | +``` |
| 93 | +docker-compose -f docker-compose-selfhosted.yml up |
| 94 | +``` |
| 95 | + |
| 96 | +## Build the sample locally |
| 97 | + |
| 98 | +You can build this [.NET Framework 4.7.2](https://www.microsoft.com/net/download/dotnet-framework-runtime/net472) application locally with MSBuild using the following instructions. The instructions assume that you are in the root of the repository and using the `Developer Command Prompt for VS 2017`. |
| 99 | + |
| 100 | +You must have the [.NET Framework 4.7.2 targeting pack](https://go.microsoft.com/fwlink/?LinkId=863261) installed. It is easiest to install with [Visual Studio 2017](https://www.microsoft.com/net/download/Windows/build) with the Visual Studio Installer. |
| 101 | + |
| 102 | +```console |
| 103 | +cd samples |
| 104 | +cd wcfapp |
| 105 | +msbuild wcfapp.sln /p:Configuration=Release |
| 106 | +``` |
| 107 | + |
| 108 | +Note: The /p:Configuration=Release argument builds the application in release mode (the default is debug mode). See the [MSBuild Command-Line reference](https://msdn.microsoft.com/en-us/library/ms164311.aspx) for more information on commandline parameters. |
| 109 | +You can also build, test and debug the application with [Visual Studio 2017](https://www.microsoft.com/net/download/Windows/build). |
| 110 | + |
| 111 | +## .NET Resources |
| 112 | + |
| 113 | +More Samples |
| 114 | + |
| 115 | +* [.NET Framework Docker Samples](../README.md) |
| 116 | +* [.NET Core Docker Samples](https://github.com/dotnet/dotnet-docker/blob/master/samples/README.md) |
| 117 | + |
| 118 | +Docs and More Information: |
| 119 | + |
| 120 | +* [.NET Docs](https://docs.microsoft.com/dotnet/) |
| 121 | +* [WCF Docs](https://docs.microsoft.com/dotnet/framework/wcf/) |
| 122 | +* [ASP.NET Docs](https://docs.microsoft.com/aspnet/) |
| 123 | +* [dotnet/core](https://github.com/dotnet/core) for starting with .NET Core on GitHub. |
| 124 | +* [dotnet/announcements](https://github.com/dotnet/announcements/issues) for .NET announcements. |
| 125 | + |
| 126 | +## Related Repositories |
| 127 | + |
| 128 | +.NET Core Docker Hub repos: |
| 129 | + |
| 130 | +* [microsoft/aspnetcore](https://hub.docker.com/r/microsoft/aspnetcore/) for ASP.NET Core images. |
| 131 | +* [microsoft/dotnet](https://hub.docker.com/r/microsoft/dotnet/) for .NET Core images. |
| 132 | +* [microsoft/dotnet-nightly](https://hub.docker.com/r/microsoft/dotnet-nightly/) for .NET Core preview images. |
| 133 | +* [microsoft/dotnet-samples](https://hub.docker.com/r/microsoft/dotnet-samples/) for .NET Core sample images. |
| 134 | + |
| 135 | +.NET Framework Docker Hub repos: |
| 136 | + |
| 137 | +* [microsoft/wcf](https://hub.docker.com/r/microsoft/wcf/) for WCF images. |
| 138 | +* [microsoft/aspnet](https://hub.docker.com/r/microsoft/aspnet/) for ASP.NET Web Forms and MVC images. |
| 139 | +* [microsoft/dotnet-framework](https://hub.docker.com/r/microsoft/dotnet-framework/) for .NET Framework images. |
| 140 | +* [microsoft/dotnet-framework-build](https://hub.docker.com/r/microsoft/dotnet-framework-build/) for building .NET Framework applications with Docker. |
| 141 | +* [microsoft/dotnet-framework-samples](https://hub.docker.com/r/microsoft/dotnet-framework-samples/) for .NET Framework and ASP.NET sample images. |
0 commit comments