This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
Running an existing .NET Framework-based application in a Windows container doesn't require any changes to your app. To run your app in a Windows container you create a Docker image containing your app and start the container. This topic explains how to take an existingASP.NET MVC application and deploy it in a Windows container.
You start with an existing ASP.NET MVC app, then build the published assets using Visual Studio. You use Docker to create the image that contains and runs your app. You'll browse to the site running in a Windows container and verify the app is working.
This article assumes a basic understanding of Docker. You can learn about Docker by reading theDocker Overview.
The app you'll run in a container is a simple website that answers questions randomly. This app is a basic MVC application with no authentication or database storage; it lets you focus on moving the web tier to a container. Future topics will show how to move and manage persistent storage in containerized applications.
Moving your application involves these steps:
Thefinished application is on GitHub.
The development machine must have the following software:
Important
If you are using Windows Server 2016, follow the instructions forContainer Host Deployment - Windows Server.
After installing and starting Docker, right-click on the tray icon and selectSwitch to Windows containers. This is required to runDocker images based on Windows. This command takes a few seconds to execute:
Collect all the assets that you need to load into a Docker image in one place. You can use the Visual StudioPublish command to create a publish profile for your app. This profile will put all the assets in one directory tree that you copy to your target image later in this tutorial.
Publish Steps
bin\Release\PublishOutput
.Open theFile Publish Options section of theSettings tab. SelectPrecompile during publishing. This optimization means that you'll becompiling views in the Docker container, you are copying the precompiledviews.
ClickPublish, and Visual Studio will copy all the needed assets to the destination folder.
Create a new file namedDockerfile to define your Docker image.Dockerfile contains instructions to build the final image and includes any base image names, required components, the app you want to run, and other configuration images.Dockerfile is the input to thedocker build
command that creates the image.
For this exercise, you will build an image based on themicrosoft/aspnet
image located onDocker Hub.The base image,mcr.microsoft.com/dotnet/framework/aspnet:4.8
, is a Windows Server image. It containsWindows Server Core, IIS, and ASP.NET 4.8. When you run this image in your container, it will automatically start IIS and installed websites.
The Dockerfile that creates your image looks like this:
# The `FROM` instruction specifies the base image. You are# extending the `mcr.microsoft.com/dotnet/framework/aspnet:4.8` image.FROM mcr.microsoft.com/dotnet/framework/aspnet:4.8# The final instruction copies the site you published earlier into the container.COPY ./bin/Release/PublishOutput/ /inetpub/wwwroot
There is noENTRYPOINT
command in this Dockerfile. You don't need one. When running Windows Server with IIS, the IIS process is the entrypoint, which is configured to start in the aspnet base image.
Run the Docker build command to create the image thatruns your ASP.NET app. To do this, open a PowerShellwindow in the directory of your project and type the following command in the solution directory:
docker build -t mvcrandomanswers .
This command will build the new image using the instructions in yourDockerfile, naming (-t tagging) the image as mvcrandomanswers. This may include pulling the base image fromDocker Hub,and then adding your app to that image.
Once that command completes, you can run thedocker images
commandto see information on the new image:
REPOSITORY TAG IMAGE ID CREATED SIZEmvcrandomanswers latest 86838648aab6 2 minutes ago 10.1 GB
The IMAGE ID will be different on your machine. Now, let's run the app.
Start a container by executing the followingdocker run
command:
docker run -d --name randomanswers mvcrandomanswers
The-d
argument tells Docker to start the image in detached mode. Thatmeans the Docker image runs disconnected from the current shell.
In many docker examples, you may see -p to map the container and host ports. The default aspnet image has already configured the container to listen on port 80 and expose it.
The--name randomanswers
gives a name to the running container. You can usethis name instead of the container ID in most commands.
Themvcrandomanswers
is the name of the image to start.
Once the container starts, connect to the running container usinghttp://localhost
in the example shown. Type that URL into your browser, and you should see the running site.
Note
Some VPN or proxy software may prevent you from navigating to your site.You can temporarily disable it to make sure your container is working.
The sample directory on GitHub contains aPowerShell script that executes these commands for you. Open a PowerShell window, change directory to your solution directory, and type:
./run.ps1
The command above builds the image, displays the list of images on your machine, and starts a container.
To stop your container, issue adocker stop
command:
docker stop randomanswers
To remove the container, issue adocker rm
command:
docker rm randomanswers
Was this page helpful?
Was this page helpful?