将 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 应用程序,并在 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 be running

重要

如果使用的是 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 映像。Define your Docker image in a Dockerfile. Dockerfile 包含有关基本映像、附加组件、要运行的应用程序和其他配置映像的说明。The Dockerfile contains instructions for the base image, additional components, the app you want to run, and other configuration images. Dockerfile 是用于创建该映像的 docker build 命令的输入。The Dockerfile is the input to the docker build command, which creates the image.

根据位于 Docker 中心microsoft/aspnet 映像生成映像。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.6.2。It contains Windows Server Core, IIS and ASP.NET 4.6.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 中心拉取基本映像,然后将应用程序添加到基本映像中。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

计算机上的 IMAGE ID 会所不同。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 映像会以断开连接当前 shell 的状态运行。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. 可在大多数命令中使用此名称,而不是容器 ID。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

备注

当前的 Windows 容器版本中,您无法浏览到http://localhostWith the current Windows Container release, you can't browse to http://localhost. 这是 WinNAT 中的已知行为,今后将予以解决。This is a known behavior in WinNAT, and it will be resolved in the future. 问题得到解决前,需要使用容器的 IP 地址。Until that is addressed, you need to use the IP address of the container.

在容器启动后,查找其 IP 地址,以便可以从浏览器连接正在运行的容器:Once the container starts, find its IP address so that you can connect to your running container from a browser:

docker inspect -f "{{ .NetworkSettings.Networks.nat.IPAddress }}" randomanswers
172.31.194.61

连接到正在运行的容器使用的 IPv4 地址,http://172.31.194.61在显示的示例。Connect to the running container using the IPv4 address, http://172.31.194.61 in the example shown. 在浏览器中键入该 URL,应该可看到正在运行的站点。Type that URL into your browser, and you should see the running site.

备注

某 VPN 或代理软件可能会阻止你导航到站点。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

上述命令将生成映像,在计算机上显示映像列表,启动容器,然后显示容器的 IP 地址。The command above builds the image, displays the list of images on your machine, starts a container, and displays the IP address for that 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