This ASP.NET Core Docker sample demonstrates a best practice pattern for building Docker images for ASP.NET Core apps for production. The sample works with both Linux and Windows containers.
The sample Dockerfile creates an ASP.NET Core application Docker image based off of the ASP.NET Core Runtime Docker image.
It uses the Docker multi-stage build feature to build the sample in a container based on the larger ASP.NET Core Build Docker image and then copies the final build result into a Docker image based on the smaller ASP.NET Core Docker Runtime image. The build image contains tools that are required to build applications while the runtime image does not.
This sample requires Docker 17.06 or later of the Docker client. You need the latest Windows 10 or Windows Server 2016 to use Windows containers. The instructions assume you have the Git client installed.
The easiest way to get the sample is by cloning the samples repository with git, using the following instructions.
git clone https://github.com/j3r3my/docker101.git
You can also download the repository as a zip.
You can build and run the sample in Docker using Linux containers using the following commands. The instructions assume that you are in the root of the repository.
docker build --no-cache -f Dockerfile -t dotnettest .
docker run -it --rm -p 8000:80 dotnettest
After the application starts, visit http://localhost:8000
in your web browser.
Note: The -p
argument maps port 8000 on you 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 paramaters.
You can launch the sample with docker-compose along with some helper images (Traefik and Portainer)
# attached - see all logs in console
docker-compose up
# detached - run as a local stack
docker-compose up -d
You must navigate to the relevant end-points to see the apps in the browser:
- Portainer Dashboard http://portainer.localhost:8000/#/containers
- Traefik Dashboard http://localhost:8080/dashboard/
- Front End App http://frontend.localhost:8081/
You can build and run the sample locally with the .NET Core 2.0 SDK using the following commands. The commands assume that you are in the root of the repository.
cd aspnetapp
dotnet run
After the application starts, visit http://localhost:8000
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 on Windows using the following command.
dotnet out\aspnetapp.dll
You can run the application on Linux or macOS using the following command.
dotnet out/aspnetapp.dll
Note: The -c release
argument builds the application in release mode (the default is debug mode). See the dotnet run reference for more information on commandline parameters.
The following Docker images are used in this sample