Skip to content

Latest commit

 

History

History
127 lines (106 loc) · 8.43 KB

1buildacontainer.md

File metadata and controls

127 lines (106 loc) · 8.43 KB

Build a Docker container for Windows, using an existing ASP.NET web application

Goal: The goal of this section is to have an ASP.NET application running in a Docker container on your Windows 10 developer machine.

Process

Step Procedure
Run the application Run the existing ASP.NET application to verify its functionality, using Visual Studio and IIS Express.
Create a Docker container Create a docker container.
Run the application in a container Run and debug the application running in a container, using Visual Studio.

Run the Application

In this section we will test the ASP.NET application using Visual Studio and IIS Express.

Step Action Result
1 Open the solution eShopLegacyWebForms.sln, which you will find in the \Lab\eShopLegacyWebFormsSolution folder on the desktop.

Note
Visual Studio will ask for login, choose Not now and choose a theme.
Visual Studio will open, and you will see one project with an ASP.NET MVC application in Visual Studio.
2 Expand the Default.aspx file in the Solution Explorer, then open the Default.aspx.cs file in Visual Studio and set a breakpoint at line 25. This will break the application once a browser requests the home page.
3 Change the Solution Configuration to Debug and press F5 to start debugging the application. The application will now build, and Edge will open with the application when it’s ready.
4 When the breakpoint is hit, click F5 to continue code execution. The applications home page shows up in Edge.
5 The application simulates a simple web shop administrative interface. Feel free to browse around the application to see details of the various products and some of the functionality. Note: The application is setup to use mock data loaded from source files, so all changes will be reverted once the debug session closes.
6 Press Shift+F5 to stop the debugging Debugging stops.

Completion

You’ve now complete debugging an ASP.NET application using Visual Studio and IIS Express.

Create a Docker container

In this section we will create a docker container image to run the ASP.NET application eShopLegacyWebForms, which requires Internet Information Server (IIS) to be running in the container.

Step Action Result
1 Publish eShopLegacyWebForms application to a folder from Visual Studio by:

1. Right-Clicking the eShopLegacyWebForms project and choose Publish
2. Choose Folder
3. Click Publish
The web application is now published to a folder and ready to be deployed in to a container image.
2 Open PowerShell and change directory to “C:\Users\Administrator\Desktop\
Lab\eShopLegacyWebFormsSolution\src\eShopLegacyWebForms”
3 Create a new DockerFile (no file extension) and open it in Notepad, by running the following commands:

1. New-Item Dockerfile
2. Notepad Dockerfile
3. A blank Notepad will show up.
4 Add the following content:

FROM microsoft/aspnet:4.7.1-windowsservercore-ltsc2016
COPY bin/Release/Publish /inetpub/wwwroot
5 Save and close the file The Dockerfile contains instructions for the docker engine on how to build a container, when running the “docker build” command in this directory.
6 In PowerShell run the command
“docker build . -t eshopweb:1.0”
in the directory “C:\Users\Administrator\Desktop\
Lab\eShopLegacyWebFormsSolution\src\eShopLegacyWebForms”
This command will tell the Docker host to build a container image and tag it eshopweb:1.0, using the instructions in the Dockerfile. Note: Make sure Docker is running and on Windows containers mode
7 Run the command docker images to see the container images cached on your machine You should see an image with the repository: eshopweb and the tag: 1.0.

Completion

You’ve now published the ASP.NET application eShopLegacyWebForms and built a docker container image with the application.

Run the application in a container

Step Action Result
1

Create an instance of the container and run it, by running the following command in PowerShell:

docker run -p 80:80 -d --name eshoptest eshopweb:1.0

This creates and starts a container, exposing port 80 on the local host.
2 Once the command return, open Edge and browse to http://localhost. You should now see the application being served from the container.
3 Stop the container by running the command docker stop eshoptest in PowerShell This stops the container and persists the state and configuration. You can then restart the container at a later point.
4 Remove the container by running the command docker rm eshoptest in PowerShell This command will remove the container state and configuration. We will do this as in the next step we create a new instance of the container with a different configuration.
5

The application has a feature to add an environment variable to the front-page title.

Create a new configuration of the container image, by running the following command in PowerShell:

docker run -p 80:80 -d --name eshoptest -e eShopTitle=MyTitle eshopweb:1.0

Note:

This feature is implemented in the Site.Master.cs file.

Environment variables is a common way of passing configurations to containers. The -e parameter is used with docker to pass environment variables to containers.
6 Once the command returns, open Edge and browse to http://localhost. You should now see the application title using your environment variable.
7

You can run commands inside a running container’. Output the environment variables from the container by running the following commands in PowerShell:

docker exec eshoptest cmd.exe /C SET

You will see all the environment variables in the container being output.
8

Let’s stop and remove the container:

  1. docker stop eshoptest

  2. docker rm eshoptest

Completion

In this section of the lab, we created a container image with our application, and ran it. Finally, we used environment variables to configure the application running in the container.