Skip to content
This repository has been archived by the owner on May 7, 2024. It is now read-only.

22. Adding Docker Support

Cesar De la Torre edited this page Apr 30, 2018 · 3 revisions

Now that we've kicked around the legacy version of the application, let's work on adding docker support by containerizing the WCF service.

Scenario: Containerize a WCF service

In this scenario you will containerize a WCF service traditionally built with .NET Framework 4.x.

The diagram below shows the scenario for the containerized WCF service running in a development PC with Docker for Windows.

image

Add Docker Support

Adding docker support to your project is a trivial task in Visual Studio. Let's wrap the WCF service in a container. To do this:

  1. Right-click on the WCF project.
  2. Hover over 'add' in the context menu.
  3. Click 'Docker Support'

This actions does two things:

  1. It adds a Dockerfile and .dockerignore file to the selected project.
  2. It adds a docker-compose project to your solution.

Edit the dockerfile

Let's open up the dockerfile that was just added and paste in these changes:

FROM microsoft/wcf:4.7.1
EXPOSE 80
ARG source
WORKDIR /inetpub/wwwroot
COPY ${source:-obj/Docker/publish} .

You can see that your custom WCF service image will be based on the microsoft/wcf:4.7.1 base image which is pretty similar to the ASP.NET base image but it has a few WCF activations in place.

Edit docker-compose files

Now let's edit the newly generated docker-compose files in the docker-compose project. First, open up docker-compose.yml.

Previously, our service was connected to a local database. Why not containerize the database as well? This will be easy. Under 'services', let's define our sql container:

version: '3.4'
services:
  eshopwcfservice:
    image: eshop/wcfservice
    build:
      context: ./src/eShopWCFService
      dockerfile: Dockerfile
    depends_on:
      - sql.data

  sql.data:
    image: microsoft/mssql-server-windows-developer

We define a sql container with the name "sql.data". We're not altering our sql container, so we can simply reference the already published image, "mssql-server-windows-developer". Now, let's open up docker-compose.override.yml and make some changes there in order to customize our services:

services:
   eshopwcfservice:
      environment:
        - ConnectionString=Server=sql.data;Database=eShopDatabase;User Id=sa;Password=YOUR-PASSWORD@@
      ports:
       - "80:80"
   sql.data:
      environment:
       - SA_PASSWORD=YOUR-PASSWORD@@
       - ACCEPT_EULA=Y
      ports:
       - "5433:1433"

At a high level, all that we're doing is defining some environmental variables in these containers to allow for connection between the database and the WCF service. We're also opening up and routing the correct ports between the host and containers so that requests on the host:port will flow to the correct container.

Test our changes

With our docker-compose changes wrapped up, let's hit F5 and kick off the build/debug. It may take a few minutes, as it may have to download the base container images that we're using.

You will know it has succeeded when a browser window opens and shows the CatalogService. We can check the docker processes running to verify that our containers launched by issuing 'docker ps', as shown below:

As we expected, we see one container to run our WCF service and another container to run our SQL database.

Next Steps

Continue on to learn about improvements which can be made in the WinForms app to make it run better on modern hardware: High DPI

Clone this wiki locally