ASP.NET Core 的 Docker 映像Docker images for ASP.NET Core

本教學課程示範如何在 Docker 容器中執行 ASP.NET Core 應用程式。This tutorial shows how to run an ASP.NET Core app in Docker containers.

在本教學課程中,您:In this tutorial, you:

  • 了解 Microsoft.NET Core Docker 映像Learn about Microsoft .NET Core Docker images
  • 下載 ASP.NET Core 範例應用程式Download an ASP.NET Core sample app
  • 在本機執行範例應用程式Run the sample app locally
  • 在 Linux 容器中執行範例應用程式Run the sample app in Linux containers
  • 在 Windows 容器中執行範例應用程式Run the sample app in Windows containers
  • 手動建置並部署Build and deploy manually

ASP.NET Core Docker 映像ASP.NET Core Docker images

針對本教學課程,您可以下載 ASP.NET Core 範例應用程式,並在 Docker 容器中執行它。For this tutorial, you download an ASP.NET Core sample app and run it in Docker containers. 該範例可與 Linux 或 Windows 容器搭配使用。The sample works with both Linux and Windows containers.

範例 Dockerfile 會使用 Docker 多階段建置功能 (英文),在不同容器中建置並執行。The sample Dockerfile uses the Docker multi-stage build feature to build and run in different containers. 建置和執行容器會從 Docker Hub 中 Microsoft 所提供的映像來建置:The build and run containers are created from images that are provided in Docker Hub by Microsoft:

  • dotnet/core/sdk

    範例會使用此映像來建置應用程式。The sample uses this image for building the app. 此映像包含隨附命令列工具 (CLI) 的 .NET Core SDK。The image contains the .NET Core SDK, which includes the Command Line Tools (CLI). 此映像會最佳化來進行本機開發、偵錯和單元測試。The image is optimized for local development, debugging, and unit testing. 基於開發和編譯而安裝的工具可使此映像成為相對較大的映像。The tools installed for development and compilation make this a relatively large image.

  • dotnet/core/aspnet

    範例會使用此映像來執行應用程式。The sample uses this image for running the app. 此映像包含 ASP.NET Core 執行階段和程式庫,並會進行最佳化,以在生產環境中執行應用程式。The image contains the ASP.NET Core runtime and libraries and is optimized for running apps in production. 專為部署和應用程式啟動速度而設計的映像相對較小,因此,已將從 Docker 登錄到 Docker 主機的網路效能最佳化。Designed for speed of deployment and app startup, the image is relatively small, so network performance from Docker Registry to Docker host is optimized. 只會將執行應用程式所需的程式庫和內容複製到容器中。Only the binaries and content needed to run an app are copied to the container. 內容已準備好執行,可用最短的時間從 Docker run 到應用程式啟動。The contents are ready to run, enabling the fastest time from Docker run to app startup. 在 Docker 模型中,不需要動態程式碼編譯。Dynamic code compilation isn't needed in the Docker model.

必要條件Prerequisites

下載範例應用程式Download the sample app

在本機執行應用程式Run the app locally

  • 瀏覽至 dotnet-docker/samples/aspnetapp/aspnetapp 中的專案資料夾。Navigate to the project folder at dotnet-docker/samples/aspnetapp/aspnetapp.

  • 執行下列命令,在本機建置並執行應用程式:Run the following command to build and run the app locally:

    dotnet run
    
  • 在瀏覽器中移至 http://localhost:5000 以測試應用程式。Go to http://localhost:5000 in a browser to test the app.

  • 在命令提示字元中按 Ctrl + C 以停止應用程式。Press Ctrl+C at the command prompt to stop the app.

在 Linux 容器中執行Run in a Linux container

  • 在 Docker 用戶端中, 切換至 Linux 容器In the Docker client, switch to Linux containers.

  • 瀏覽至 dotnet-docker/samples/aspnetapp 中的 Dockerfile 資料夾。Navigate to the Dockerfile folder at dotnet-docker/samples/aspnetapp.

  • 執行下列命令,在 Docker 中建置並執行範例:Run the following commands to build and run the sample in Docker:

    docker build -t aspnetapp .
    docker run -it --rm -p 5000:80 --name aspnetcore_sample aspnetapp
    

    build 命令引數:The build command arguments:

    • 將映像命名為 aspnetapp。Name the image aspnetapp.
    • 在目前的資料夾 (結束期間) 中尋找 Dockerfile。Look for the Dockerfile in the current folder (the period at the end).

    run 命令引數:The run command arguments:

    • 配置虛擬 TTY,即使未連接,還是要使其保持開啟Allocate a pseudo-TTY and keep it open even if not attached. (與 --interactive --tty 的效果相同)。(Same effect as --interactive --tty.)
    • 結束時自動移除容器。Automatically remove the container when it exits.
    • 將本機電腦上的連接埠 5000 對應至容器中的連接埠 80。Map port 5000 on the local machine to port 80 in the container.
    • 將容器命名為 aspnetcore_sample。Name the container aspnetcore_sample.
    • 指定 aspnetapp 映像。Specify the aspnetapp image.
  • 在瀏覽器中移至 http://localhost:5000 以測試應用程式。Go to http://localhost:5000 in a browser to test the app.

在 Windows 容器中執行Run in a Windows container

瀏覽至 dotnet-docker/samples/aspnetapp 中的 docker 檔案資料夾。Navigate to the docker file folder at dotnet-docker/samples/aspnetapp.

  • 執行下列命令,在 Docker 中建置並執行範例:Run the following commands to build and run the sample in Docker:

    docker build -t aspnetapp .
    docker run -it --rm --name aspnetcore_sample aspnetapp
    
  • 針對 Windows 容器,您需要容器的 IP 位址 (瀏覽至 http://localhost:5000 將無法運作):For Windows containers, you need the IP address of the container (browsing to http://localhost:5000 won't work):

    • 開啟另一個命令提示字元。Open up another command prompt.

    • 執行 docker ps 以查看執行中的容器。Run docker ps to see the running containers. 確認其中有 "aspnetcore_sample" 容器。Verify that the "aspnetcore_sample" container is there.

    • 執行 docker exec aspnetcore_sample ipconfig 來顯示容器的 IP 位址。Run docker exec aspnetcore_sample ipconfig to display the IP address of the container. 此命令的輸出看起來就像下列範例:The output from the command looks like this example:

      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
      
  • 複製容器 IPv4 位址 (例如 172.29.245.43) 並貼入瀏覽器網址列,以測試應用程式。Copy the container IPv4 address (for example, 172.29.245.43) and paste into the browser address bar to test the app.

手動建置並部署Build and deploy manually

在某些情況下,您可能想要藉由將其複製到執行階段所需的應用程式檔案,來將應用程式部署到容器。In some scenarios, you might want to deploy an app to a container by copying to it the application files that are needed at run time. 本節示範如何手動部署。This section shows how to deploy manually.

  • 瀏覽至 dotnet-docker/samples/aspnetapp/aspnetapp 中的專案資料夾。Navigate to the project folder at dotnet-docker/samples/aspnetapp/aspnetapp.

  • 執行 dotnet publish 命令:Run the dotnet publish command:

    dotnet publish -c Release -o published
    

    命令引數:The command arguments:

    • 以發行模式建置應用程式 (預設值是偵錯模式)。Build the application in release mode (the default is debug mode).
    • published 資料夾中建立檔案。Create the files in the published folder.
  • 執行應用程式。Run the application.

    • Windows:Windows:

      dotnet published\aspnetapp.dll
      
    • Linux:Linux:

      dotnet published/aspnetapp.dll
      
  • 瀏覽至 http://localhost:5000 以查看首頁。Browse to http://localhost:5000 to see the home page.

若要在 Docker 容器內使用手動發行的應用程式,請建立新的 Dockerfile 並使用 docker build . 命令來建置容器。To use the manually published application within a Docker container, create a new Dockerfile and use the docker build . command to build the container.

FROM mcr.microsoft.com/dotnet/core/aspnet:2.2 AS runtime
WORKDIR /app
COPY published/aspnetapp.dll ./
ENTRYPOINT ["dotnet", "aspnetapp.dll"]

DockerfileThe Dockerfile

以下是您稍早執行的命令所使用的 Dockerfile docker buildHere's the Dockerfile used by the docker build command you ran earlier. 它會以您在本節所做的相同方式,使用 dotnet publish 進行建置及部署。It uses dotnet publish the same way you did in this section to build and deploy.

FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build
WORKDIR /app

# copy csproj and restore as distinct layers
COPY *.sln .
COPY aspnetapp/*.csproj ./aspnetapp/
RUN dotnet restore

# copy everything else and build app
COPY aspnetapp/. ./aspnetapp/
WORKDIR /app/aspnetapp
RUN dotnet publish -c Release -o out

FROM mcr.microsoft.com/dotnet/core/aspnet:2.2 AS runtime
WORKDIR /app
COPY --from=build /app/aspnetapp/out ./
ENTRYPOINT ["dotnet", "aspnetapp.dll"]
FROM mcr.microsoft.com/dotnet/core/aspnet:3.0 AS runtime
WORKDIR /app
COPY published/aspnetapp.dll ./
ENTRYPOINT ["dotnet", "aspnetapp.dll"]

DockerfileThe Dockerfile

以下是您稍早執行的命令所使用的 Dockerfile docker buildHere's the Dockerfile used by the docker build command you ran earlier. 它會以您在本節所做的相同方式,使用 dotnet publish 進行建置及部署。It uses dotnet publish the same way you did in this section to build and deploy.

FROM mcr.microsoft.com/dotnet/core/sdk:3.0 AS build
WORKDIR /app

# copy csproj and restore as distinct layers
COPY *.sln .
COPY aspnetapp/*.csproj ./aspnetapp/
RUN dotnet restore

# copy everything else and build app
COPY aspnetapp/. ./aspnetapp/
WORKDIR /app/aspnetapp
RUN dotnet publish -c Release -o out

FROM mcr.microsoft.com/dotnet/core/aspnet:3.0 AS runtime
WORKDIR /app
COPY --from=build /app/aspnetapp/out ./
ENTRYPOINT ["dotnet", "aspnetapp.dll"]

如上述 Dockerfile 所述,檔案 *.csproj 會複製並還原為不同的 級。As noted in the preceding Dockerfile, the *.csproj files are copied and restored as distinct layers. docker build 命令建立映射時,它會使用內建的快取。When the docker build command builds an image, it uses a built-in cache. 如果 *.csprojdocker build 命令上次執行之後,檔案尚未變更,則 dotnet restore 不需要再次執行命令。If the *.csproj files haven't changed since the docker build command last ran, the dotnet restore command doesn't need to run again. 相反地,會重複使用對應層的內建快取 dotnet restoreInstead, the built-in cache for the corresponding dotnet restore layer is reused. 如需詳細資訊,請參閱 撰寫 dockerfile 的最佳做法For more information, see Best practices for writing Dockerfiles.

其他資源Additional resources

後續步驟Next steps

包含範例應用程式的 Git 存放庫也會包含文件。The Git repository that contains the sample app also includes documentation. 如需存放庫中可用資源的概觀,請參閱讀我檔案 (英文)。For an overview of the resources available in the repository, see the README file. 特別是了解如何實作 HTTPS:In particular, learn how to implement HTTPS: