This sample demonstrates how to use ASP.NET MVC and Docker together. See ASP.NET Docker Sample and .NET Framework Console Docker Sample for using Docker in other scenarios.
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.
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 aspnetmvcapp
docker build --pull -t aspnetmvcapp .
docker run --name aspnetmvc_sample --rm -it -p 8000:80 aspnetmvcapp
You should see the following console output as the application starts.
C:\git\dotnet-framework-docker\samples\aspnetapp>docker run --name aspnetmvc_sample --rm -it -p 8000:80 aspnetmvcapp
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.
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 aspnetmvc_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\aspnetmvcapp>docker exec aspnetmvc_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\aspnetmvcapp>docker inspect -f "{{ .NetworkSettings.Networks.nat.IPAddress }}" aspnetcore_sample
172.25.157.148
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