將 ASP.NET MVC 應用程式遷移到 Windows 容器Migrating ASP.NET MVC Applications to Windows Containers

在 Windows 容器中執行現有的 .NET Framework 架構應用程式,不需要對您的應用程式進行任何變更。Running an existing .NET Framework-based application in a Windows container doesn't require any changes to your app. 若要在 Windows 容器中執行您的應用程式,您可以建立包含您應用程式的 Docker 映像,然後啟動該容器。To run your app in a Windows container you create a Docker image containing your app and start the container. 本主題說明如何才能擷取現有 ASP.NET MVC 應用程式 (ASP.NET MVC application) 並部署到 Windows 容器中。This topic explains how to take an existing ASP.NET MVC application and deploy it in a Windows container.

您可以從現有的 ASP.NET MVC 應用程式開始,然後使用 Visual Studio 建置已發行的資產。You start with an existing ASP.NET MVC app, then build the published assets using Visual Studio. 您可以使用 Docker,建立其中包含並會執行您應用程式的映像。You use Docker to create the image that contains and runs your app. 接著瀏覽到 Windows 容器中正在執行的網站,並確認應用程式可以正常運作。You'll browse to the site running in a Windows container and verify the app is working.

本文假設您具備 Docker 的基本知識。This article assumes a basic understanding of Docker. 您可以閱讀 Docker 概觀,以了解 Docker。You can learn about Docker by reading the Docker Overview.

您將在容器中執行的應用程式是會隨機回答問題的簡單網站。The app you'll run in a container is a simple website that answers questions randomly. 此應用程式是沒有驗證或資料庫儲存體的基本 MVC 應用程式,讓您著重於將 Web 層移至容器。This app is a basic MVC application with no authentication or database storage; it lets you focus on moving the web tier to a container. 未來的主題將說明如何移動和管理容器化應用程式中的永續性儲存體。Future topics will show how to move and manage persistent storage in containerized applications.

移動您的應用程式包含下列步驟:Moving your application involves these steps:

  1. 建立發行工作以建置映像資產Creating a publish task to build the assets for an image.
  2. 建立用以執行應用程式的 Docker 映像Building a Docker image that will run your application.
  3. 啟動執行映像的 Docker 容器Starting a Docker container that runs your image.
  4. 使用瀏覽器確認應用程式Verifying the application using your browser.

完成的應用程式在 GitHub 上。The finished application is on GitHub.

必要條件Prerequisites

開發用的電腦必須具備下列軟體:The development machine must have the following software:

Important

如果您在使用 Windows Server 2016,請遵循容器主機部署 - Windows Server 的指示。If you are using Windows Server 2016, follow the instructions for Container Host Deployment - Windows Server.

安裝並啟動 Docker,以滑鼠右鍵按一下系統匣圖示,然後選取後切換至 Windows 容器After installing and starting Docker, right-click on the tray icon and select Switch to Windows containers. 如此才能根據 Windows 執行 Docker 映像。This is required to run Docker images based on Windows. 此命令需要幾秒鐘的時間執行:This command takes a few seconds to execute:

Windows 容器Windows Container

發行指令碼Publish script

將您必須載入 Docker 映像的所有資產收集在某一位置。Collect all the assets that you need to load into a Docker image in one place. 您可以使用 Visual Studio 的 [發行] 命令來建立應用程式的發行設定檔。You can use the Visual Studio Publish command to create a publish profile for your app. 此設定檔會將所有資產放在一個樹狀目錄中,您稍後會在本教學課程將此目錄複製到目標映像。This profile will put all the assets in one directory tree that you copy to your target image later in this tutorial.

發行步驟Publish Steps

  1. 以滑鼠右鍵按一下 Visual Studio 中的 Web 專案,然後選取 [發行]。Right click on the web project in Visual Studio, and select Publish.
  2. 按一下 [自訂設定檔] 按鈕,然後選取 [檔案系統] 作為方法。Click the Custom profile button, and then select File System as the method.
  3. 選擇目錄。Choose the directory. 依照慣例,下載的範例會使用 bin\Release\PublishOutputBy convention, the downloaded sample uses bin\Release\PublishOutput.

發行連線Publish Connection

開啟 [設定] 索引標籤的 [檔案發行選項] 區段。選取 [發行期間預先編譯]。Open the File Publish Options section of the Settings tab. Select Precompile during publishing. 此最佳化表示您將要編譯 Docker 容器中的檢視,而正在複製預先編譯的檢視。This optimization means that you'll be compiling views in the Docker container, you are copying the precompiled views.

發行設定Publish Settings

按一下 [發行],Visual Studio 會將所有需要的資產複製到目的資料夾。Click Publish, and Visual Studio will copy all the needed assets to the destination folder.

建立映像Build the image

建立新的檔案,名為Dockerfile來定義您的 Docker 映像。Create a new file named Dockerfile to define your Docker image. Dockerfile包含指示,來建置最終映像,並包含任何基礎映像名稱、 所需的元件、 您想要執行的應用程式和其他組態映像。Dockerfile contains instructions to build the final image and includes any base image names, required components, the app you want to run, and other configuration images. Dockerfile是輸入docker build建立映像的命令。Dockerfile is the input to the docker build command that creates the image.

針對此練習中,您將建置基礎的映像microsoft/aspnet映像位於Docker HubFor this exercise, you will build an image based on the microsoft/aspnet image located on Docker Hub. 基礎映像 microsoft/aspnet 是 Windows Server 映像。The base image, microsoft/aspnet, is a Windows Server image. 它包含 Windows Server Core、 IIS 和 ASP.NET 4.7.2。It contains Windows Server Core, IIS, and ASP.NET 4.7.2. 當您在容器中執行此映像時,其將會自動啟動 IIS 和已安裝的網站。When you run this image in your container, it will automatically start IIS and installed websites.

建立映像的 Dockerfile 看起來像這樣:The Dockerfile that creates your image looks like this:

# The `FROM` instruction specifies the base image. You are
# extending the `microsoft/aspnet` image.

FROM microsoft/aspnet

# The final instruction copies the site you published earlier into the container.
COPY ./bin/Release/PublishOutput/ /inetpub/wwwroot

此 Dockerfile 中沒有 ENTRYPOINT 命令。There is no ENTRYPOINT command in this Dockerfile. 您不需要此命令。You don't need one. 執行 Windows Server 與 IIS,IIS 處理序時,進入點,會設定為 aspnet 的基底映像中啟動。When running Windows Server with IIS, the IIS process is the entrypoint, which is configured to start in the aspnet base image.

執行 Docker 建置命令,建立會執行 ASP.NET 應用程式的映像。Run the Docker build command to create the image that runs your ASP.NET app. 若要這樣做,請在您的專案目錄中開啟 PowerShell 視窗並的方案目錄中,輸入下列命令:To do this, open a PowerShell window in the directory of your project and type the following command in the solution directory:

docker build -t mvcrandomanswers .

此命令會建立使用您的 Dockerfile 中的指示新的映像命名 (-t 標記) 為 mvcrandomanswers 映像。This command will build the new image using the instructions in your Dockerfile, naming (-t tagging) the image as mvcrandomanswers. 這可能包括從 Docker Hub 提取基礎映像,然後將您的應用程式加入至該映像。This may include pulling the base image from Docker Hub, and then adding your app to that image.

該命令完成之後,您可以執行 docker images 命令來了解新映像的相關資訊:Once that command completes, you can run the docker images command to see information on the new image:

REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
mvcrandomanswers              latest              86838648aab6        2 minutes ago       10.1 GB

您電腦上的映像識別碼會有所不同。The IMAGE ID will be different on your machine. 現在,讓我們來執行應用程式。Now, let's run the app.

啟動容器Start a container

執行下列 docker run 命令來啟動容器:Start a container by executing the following docker run command:

docker run -d --name randomanswers mvcrandomanswers

-d 引數會指示 Docker 在離線模式中啟動映像。The -d argument tells Docker to start the image in detached mode. 這表示 Docker 映像可在與目前殼層中斷連線的情況下執行。That means the Docker image runs disconnected from the current shell.

在許多 docker 範例中,您可能會看到對應的容器和主機的連接埠-p。In many docker examples, you may see -p to map the container and host ports. 預設的 aspnet 映像已經設定要在連接埠 80 上接聽,並將它公開的容器。The default aspnet image has already configured the container to listen on port 80 and expose it.

--name randomanswers 提供執行中容器的名稱。The --name randomanswers gives a name to the running container. 您可以使用此名稱,而不是大多數命令中的容器識別碼。You can use this name instead of the container ID in most commands.

mvcrandomanswers 是要啟動之映像的名稱。The mvcrandomanswers is the name of the image to start.

在瀏覽器中確認Verify in the browser

一旦容器啟動時,連接到執行的容器使用http://localhost中所示的範例。Once the container starts, connect to the running container using http://localhost in the example shown. 在您的瀏覽器中輸入該 URL,您應該會看到執行中的網站。Type that URL into your browser, and you should see the running site.

Note

某些 VPN 或 Proxy 軟體可能會阻止您瀏覽至您的網站。Some VPN or proxy software may prevent you from navigating to your site. 您可以暫時停用,以確保您的容器可以正常運作。You can temporarily disable it to make sure your container is working.

GitHub 上的範例目錄包含 PowerShell 指令碼,可為您執行這些命令。The sample directory on GitHub contains a PowerShell script that executes these commands for you. 開啟 PowerShell 視窗,將目錄變更為您的方案目錄,然後輸入:Open a PowerShell window, change directory to your solution directory, and type:

./run.ps1

上述命令會建置該映像、 映像清單會顯示您的電腦,並啟動容器。The command above builds the image, displays the list of images on your machine, and starts a container.

若要停止容器,請發出 docker stop 命令:To stop your container, issue a docker stop command:

docker stop randomanswers

若要移除容器,請發出 docker rm 命令:To remove the container, issue a docker rm command:

docker rm randomanswers