Beruflich Dokumente
Kultur Dokumente
Before you run the solution, you must make sure that you configure Docker to use Windows Containers. To do
that, you right-click the Docker taskbar icon in Windows and select Switch to Windows Containers, as shown in
the figure below.
If the menu item says "Switch to Linux containers", you are already running Docker with Windows Containers.
Visual Studio 2017 provides great support for containerizing an application. You right-click the project node and
then select Add and Docker Support. The Docker project template adds a new project to the solution called
docker-compose. The project contains the Docker assets (simple .yml metadata files) that compose the Docker
images and containers' startup settings you need, as shown in the figure below.
Then, in the next figure you can see how your project and solution has been modified with the Docker metadata.
In the simplest monolithic scenarios like this one, the application will be the single application service/container
defined at the docker-compose.yml file that you will need to deploy. In other more complex scenarios like N-Tier
applications, you will have a multi-container application defined in the docker-compose.yml file. The template
also changes your startup project to point to the docker-compose project so it will run on Docker when launching
the solution. Pressing Ctrl+F5 or F5 now compiles the .NET application bits, creates the Docker image and
launches the Docker container all in a single step for you, as shown in the next image.
When VS is building the Docker image, the first time you do this, it takes considerable time (a few minutes). This
is because the build process pulls down the base Windows Server Core image and the additional image for
ASP.NET. Subsequent build and run cycles will be much faster. Let’s take a deeper look at the files added by the
Docker project template. It created several files for you. Visual Studio uses these files to create the Docker image
and launch a container. You can use the same files from the CLI to run Docker commands manually with the
Docker CLI commands like “docker run” and “docker-compose up”. For each application or service in your
solution Visual Studio adds a dockerfile to your project’s root folder, similar to the following Dockerfile example
showing the basic settings for building a Docker image based on the Windows ASP.NET image that runs an
ASP.NET site.
Sample Dockerfile:
`FROM microsoft/aspnet
ARG source
WORKDIR /inetpub/wwwroot
COPY ${source:-obj/Docker/publish} . `
If your application were based on an older version of .NET Framework, like .NET Framework 3.5, you could use a
similar base image directive like the following.
FROM microsoft/dotnet-framework:3.5
This previous Dockerfile will look very similar to those created for running an ASP.NET Core application in
Linux containers. However, there are a few important differences. The most important difference is that the base
image is microsoft/aspnet, which is an image based on Windows Server Core image that includes IIS, the .NET
Framework and special optimizations for ASP.NET. The other files in the docker-compose project are the Docker
assets needed to build and configure the containers. Visual Studio puts the various docker-compose.yml files
under one node (project) to highlight how they are used. The base docker-compose file contains the directives that
are common to all configurations/environments. The docker-compose.override.yml file contains environment
variables and related overrides for a by-default developer configuration.
There are a couple of differences between the development configuration and a production configuration. In the
development environment, you run the ASP.NET application and SQL Server in Windows Containers, within the
same Docker host. In earlier sections, we mentioned that for development environments you could use a SQL
localdb within the same application Windows Container or deploying an independent SQL Server Windows
Container.
The advantage of running the database along with the multiple application containers within the same Docker host
(or even orchestrator cluster looking forward) is that you have a very predictable testing environment with a
consistent database that can have the same pre-generated data generated when deploying to the testing
environment in the Docker host. In the development environment, you usually run all the containers in the same
environment, including the SQL databases, so it’ll be easier while developing and testing.
In a production environment, you can decide if you want to run the application Windows Container in a single
Docker host Azure VM, or in an orchestrator’s cluster like Service Fabric, and most of all, you will usually have
the data stored in a high-available and production ready SQL environment like Azure SQL Database or Azure
SQL Database Managed Instance.
Additional resources
*** Optional finish of the walkthrough *** At this point you already containerized the application. The next
sections focus on alternative approaches and more advanced scenarios you might need and you can also explore.
In order to generate your application's .NET bits, you can do so from VS and the menu option "Publish", then with
the option "to folder" as used in this other Wiki procedure.
As an alternative option, in order to build the .NET bits from the CLI or from a CI pipeline, you need a build
container or Docker build agent that will be internally using MSBuild to compile your .NET code. You can
explore this walkthrough on "How to create a custom image for a .NET Framework build container internally
using MSBuild".
Once you produced the .NET bits of your application, ready for you, in a directory, you’d build the Docker images
with the CLI command “docker image build”, also explained in the same article/walkthrough.
In this case you don’t even need to see the code or open any solution like when using Visual Studio and it has the
advantage of having a tool that explores existing installation and configuration in Windows and will generate the
dockerfile config for that specific configuration.
What is Image2Docker?
Image2Docker is basically a PowerShell module that migrates existing Windows application from VMs to
Windows Container images. Although it supports multiple application types, the main focus is on IIS. You can use
Image2Docker to export ASP.NET websites from a VM, so you can run them in a Windows Container with no
application changes.
Image2Docker first inspects the artifacts in a Windows Server VM image. This original VM could be a Windows
Server 2003, 2008, 2012 or 2016 and its VM format could be WIM, VHD or VHDX.
Image2Docker will extract either an entire VM or specific components from a VHD file. Next, it will generate a
Dockerfile which you can build into a Windows Container image.
For instance, if you have a VM disk image (VHD, VHDX or WIM), you can extract all the IIS websites from it by
installing Image2Docker and running ConvertTo-Dockerfile like this:
`Install-Module Image2Docker
Import-Module Image2Docker
ConvertTo-Dockerfile -ImagePath C:\my-windows-2016.vhd -Artifact IIS -OutputPath
c:\docker\W216iis`
That will produce a Dockerfile which you can use to build a Windows container image, using docker build.
In order to install an MSI within a Windows Container you just need to copy in the .msi file and run msiexec to
install it. For instance, the following dockerfile specifies how to install a .msi in your container.
`# escape=`
FROM microsoft/aspnet:windowsservercore
COPY MySetupSample-1.0.0.0.msi /
RUN msiexec /i c:\ MySetupSample-1.0.0.0.msi RELEASENAME=2017.01 /qn `
FROM specifies the base image to use, in this case a specific version of the ASP.NET image using
Windows Server Core.
COPY copies the existing MSI from the local machine into the Docker image
RUN installs the MSI using msiexec, with the qn switch to install silently, and passing a value to the
custom RELEASENAME variable that the MSI uses.
That .msi setup could install whatever dependency you might need for your application, like the .DLLS of a PDF
creator library (or any other comparable case) that you might need to use from your ASP.NET application.
You can also, of course, add these steps to your previous dockerfile so you may end up with multiple COPY
commands (your application plus additional .MSIs) and therefore you could also execute multiple RUN
commands from your dockerfile.
For instance, the following dockerfile would enable .NET WCF HTTP Activation in your Windows Container:
`# Base image is Windows Server Core image since WCF does not need ASP.NET
FROM microsoft/windowsservercore
The important line in that dockerfile is the line with the RUN command that specifies to add certain Windows
Feature, in this case, .NET WCF HTTP Activation. Note that not all the Windows features can be activated in
Windows Containers since some of them are not supported, like MSMQ or other cases.
Additional resources:
Chocolatey for creating Windows Containers images for infrastructure assets (DBs, RabbitMQ, etc.)
Invoke-WebRequest
"https://github.com/docker/compose/releases/download/$dockerComposeVersion/docke
r-compose-Windows-x86_64.exe" -UseBasicParsing -OutFile
$Env:ProgramFiles\docker\docker-compose.exe