This sample Dockerfile demonstrates how to use ASP.NET Core and Docker together. The sample works with both Linux and Windows containers and can also be used without Docker. There are also instructions that demonstrate how to push the sample to Azure Container Registry and test it with Azure Container Instance. You can configure ASP.NET Core to use HTTPS with Docker.
The sample builds the application in a container based on the larger .NET Core SDK Docker image. It builds the application and then copies the final build result into a Docker image based on the smaller ASP.NET Core Docker Runtime image.
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 Core Docker image, based on this sample.
Type the following command to run a sample with Docker:
docker run --name aspnetcore_sample --rm -it -p 8000:80 mcr.microsoft.com/dotnet/core/samples:aspnetapp
After the application starts, navigate to http://localhost:8000
in your web browser. On Windows, you may need to navigate to the container via IP address. See ASP.NET Core apps in Windows Containers for instructions on determining the IP address, using the value of --name
that you used in docker run
.
See Hosting ASP.NET Core Images with Docker over HTTPS to use HTTPS with this image.
The easiest way to get the sample is by cloning the samples repository with git, using the following instructions:
git clone https://github.com/dotnet/dotnet-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 aspnetcore_sample --rm -it -p 8000:80 aspnetapp
You should see the following console output as the application starts.
C:\git\dotnet-docker\samples\aspnetapp>docker run --name aspnetcore_sample --rm -it -p 8000:80 aspnetapp
Hosting environment: Production
Content root path: /app
Now listening on: http://[::]:80
Application started. Press Ctrl+C to shut down.
After the application starts, navigate to http://localhost:8000
in your web browser. On Windows, you may need to navigate to the container via IP address. See ASP.NET Core apps in Windows Containers for instructions on determining the IP address, using the value of --name
that you used in docker run
.
Note: The
-p
argument maps port 8000 on your local machine to port 80 in the container (the form of the port mapping ishost:container
). See the Docker run reference for more information on commandline parameters. In some cases, you might see an error because the host port you select is already in use. Choose a different port in that case.
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.
ASP.NET Core 2.1 and newer uses HTTPS by default. You can configure ASP.NET Core to use HTTPS with Docker.
You can build and run the sample for Alpine using the following instructions. Make sure Docker is set to Linux containers if you are on Windows.
cd samples
cd aspnetapp
docker build --pull -t aspnetapp -f Dockerfile.alpine-x64 .
docker run --name aspnetcore_sample --rm -it -p 8000:80 aspnetapp
After the application starts, navigate to http://localhost:8000
in your web browser.
You can also build for Ubuntu 18.04, with a bionic
tag. The bionic
tags are documented at dotnet/core/sdk and dotnet/core/aspnet. You would switch to use the 2.2-bionic
tag for both the build and runtime phases.
You can build and run the sample for ARM32 and Raspberry Pi with Build ASP.NET Core Applications for Raspberry Pi with Docker instructions.
You can develop applications without a .NET Core installation on your machine with the Develop ASP.NET Core applications in a container instructions. These instructions are also useful if your development and production environments do not match.
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 terminated. 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 and run the sample locally with the .NET Core 2.2 SDK using the following commands. The commands assume that you are in the root of the repository.
cd samples
cd aspnetapp
dotnet run
After the application starts, visit http://localhost:5000
in your web browser.
You can produce an application that is ready to deploy to production locally using the following command.
dotnet publish -c Release -o out
You can run the application using the following commands.
cd out
dotnet aspnetapp.dll
Note: The -c Release
argument builds the application in release mode (the default is debug mode). See the dotnet publish reference for more information on commandline parameters.
More Samples
Docs and More Information:
- .NET Docs
- ASP.NET Docs
- dotnet/core for starting with .NET Core on GitHub.
- dotnet/announcements for .NET announcements.
.NET Core:
- dotnet/core: .NET Core
- dotnet/core/samples: .NET Core Samples
- dotnet/core-nightly: .NET Core (Preview)
.NET Framework:
- dotnet/framework: .NET Framework, ASP.NET and WCF
- dotnet/framework/samples: .NET Framework, ASP.NET and WCF Samples