Skip to content

Latest commit

 

History

History
 
 

aspnetapp

ASP.NET Core Docker Production Sample

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.

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/dotnet/dotnet-docker-samples/

You can also download the repository as a zip.

Build and run the sample with Docker for Linux containers

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.

cd aspnetapp
docker build -t aspnetapp .
docker run -it --rm -p 8000:80 aspnetapp

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.

Build and run the sample with Docker for Windows containers

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

cd aspnetapp
docker build -t aspnetapp .
docker run -it --rm --name aspnetcore_sample aspnetapp

You must navigate to the container IP (as opposed to http://localhost) in your browser directly when using Windows containers. You can get the IP address of your container with the following steps:

  1. Open up another command prompt.
  2. Run docker ps to see your running containers. The "aspnetcore_sample" container should be there.
  3. Run docker exec aspnetcore_sample ipconfig.
  4. 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-docker-samples\aspnetapp>docker exec aspnetcore_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 name is used above. It runs a new command (as opposed to the entrypoint) in a running container.

Some people prefer using docker inspect for this same purpose. See the following example, below:

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

Build and run the sample locally

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.

Docker Images used in this sample

The following Docker images are used in this sample

Related Resources