Skip to content

Latest commit

 

History

History
141 lines (89 loc) · 7.21 KB

File metadata and controls

141 lines (89 loc) · 7.21 KB

ASP.NET Docker Sample

This sample demonstrates how to use ASP.NET and Docker together.

The sample builds the application in a container based on the larger .NET Framework SDK Docker image. It builds the application and then copies the final build result into a Docker image based on the smaller ASP.NET Runtime Docker image. It uses Docker multi-stage build and multi-arch tags.

This sample requires the Docker Desktop for Windows.

Try a pre-built ASP.NET Docker Image

You can quickly run a container with a pre-built sample ASP.NET Docker image.

Type the following Docker command:

docker run --name aspnet_sample --rm -it -p 8000:80 mcr.microsoft.com/dotnet/framework/samples:aspnetapp

After the application starts, navigate to http://localhost:8000 in your web browser. You need to navigate to the application via IP address instead of localhost for earlier Windows versions, which is demonstrated in View the ASP.NET app in a running container on Windows.

Getting the sample

The easiest way to get the sample is by cloning the samples repository with git, using the following instructions:

git clone https://github.com/microsoft/dotnet-framework-docker/

You can also download the repository as a zip.

Build and run the sample with Docker

You can build and run the sample in Docker using the following commands. The instructions assume that you are in the root of the repository.

cd samples
cd aspnetapp
docker build --pull -t aspnetapp .
docker run --name aspnet_sample --rm -it -p 8000:80 aspnetapp

You should see the following console output as the application starts.

C:\git\dotnet-framework-docker\samples\aspnetapp>docker run --name aspnet_sample --rm -it -p 8000:80 aspnetapp
Service 'w3svc' has been stopped

Service 'w3svc' started

After the application starts, navigate to http://localhost:8000 in your web browser. You need to navigate to the application via IP address instead of localhost for earlier Windows versions, which is demonstrated in View the ASP.NET app in a running container on Windows.

Note: The -p argument maps port 8000 on your local machine to port 80 in the container (the form of the port mapping is host:container). See the Docker run reference for more information on commandline parameters.

View the ASP.NET app in a running container on Windows

After the application starts, navigate to the container IP (as opposed to http://localhost) in your web browser with the the following instructions:

  1. Open up another command prompt.
  2. Run docker exec aspnet_sample ipconfig.
  3. Copy the container IP address and paste into your browser (for example, 172.29.245.43).

See the following example of how to get the IP address of a running Windows container.

C:\git\dotnet-framework-docker\samples\aspnetapp>docker exec aspnet_sample ipconfig

Windows IP Configuration


Ethernet adapter Ethernet:

   Connection-specific DNS Suffix  . : contoso.com
   Link-local IPv6 Address . . . . . : fe80::1967:6598:124:cfa3%4
   IPv4 Address. . . . . . . . . . . : 172.29.245.43
   Subnet Mask . . . . . . . . . . . : 255.255.240.0
   Default Gateway . . . . . . . . . : 172.29.240.1

Note: docker exec supports identifying containers with name or hash. The container name is used in the preceding instructions. docker exec runs a new command (as opposed to the entrypoint) in a running container.

Some people prefer using docker inspect for this same purpose, as demonstrated in the following example.

C:\git\dotnet-framework-docker\samples\aspnetapp>docker inspect -f "{{ .NetworkSettings.Networks.nat.IPAddress }}" aspnetcore_sample
172.25.157.148

Deploying to Production vs Development

The approach for running containers differs between development and production.

In production, you will typically start your container with docker run -d. This argument starts the container as a service, without any console interaction. You then interact with it through other Docker commands or APIs exposed by the containerized application.

In development, you will typically start containers with docker run --rm -it. These arguments enable you to see a console (important when there are errors), terminate the container with CTRL-C and cleans up all container resources when the container is termiantes. You also typically don't mind blocking the console. This approach is demonstrated in prior examples in this document.

We recommend that you do not use --rm in production. It cleans up container resources, preventing you from collecting logs that may have been captured in a container that has either stopped or crashed.

Build the sample locally

You can build this .NET Framework 4.8 application locally with MSBuild and NuGet using the following instructions. The instructions assume that you are in the root of the repository and using the Developer Command Prompt for VS 2019.

You must have the .NET Framework 4.8 targeting pack installed.

cd samples
cd aspnetapp
nuget restore
msbuild

You can test and debug the application with Visual Studio 2019.

.NET Resources

More Samples

Docs and More Information:

Related Docker Hub Repos

.NET Framework:

.NET: