This sample demonstrates how to use ASP.NET and Docker together. There are also instructions that demonstrate how to push the sample to Azure Container Registry and test it with Azure Container Instance. The sample can also be used without Docker.
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 Docker 17.06 or later of the Docker client.
You can quickly run a container with a pre-built sample ASP.NET Docker image, based on the [ASP.NET Docker sample].
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.
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.
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.
Multiple variations of this sample have been provided, as follows. Some of these example Dockerfiles are demonstrated later. Specify an alternate Dockerfile via the -f
argument.
After the application starts, navigate to the container IP (as opposed to http://localhost) in your web browser with the the following instructions:
- Open up another command prompt.
- Run
docker exec aspnet_sample ipconfig
. - 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
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.
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.
More Samples
Docs and More Information:
- .NET Docs
- ASP.NET Docs
- dotnet/core for starting with .NET on GitHub.
- dotnet/announcements for .NET announcements.
.NET Framework:
- dotnet/framework/sdk: .NET Framework SDK
- dotnet/framework/aspnet: ASP.NET Web Forms and MVC
- dotnet/framework/runtime: .NET Framework Runtime
- dotnet/framework/wcf: Windows Communication Foundation (WCF)
- dotnet/framework/samples: .NET Framework, ASP.NET and WCF Samples
.NET:
- dotnet: .NET
- dotnet/samples: .NET Samples
- dotnet-nightly: .NET (Preview)