.NET 和 Docker 简介Introduction to .NET and Docker

.NET Core 可以在 Docker 容器中轻松运行。.NET Core can easily run in a Docker container. 容器提供一种轻量级方法,用于将应用程序与主机系统的其他组件分隔,以便仅共享内核,并使用提供给应用程序的资源。Containers provide a lightweight way to isolate your application from the rest of the host system, sharing just the kernel, and using resources given to your application. 如果你不熟悉 Docker,强烈建议通读 Docker 的概述文档If you're unfamiliar with Docker, it's highly recommended that you read through Docker's overview documentation.

若要详细了解如何安装 Docker,请参阅 Docker 桌面:社区版的下载页面。For more information about how to install Docker, see the download page for Docker Desktop: Community Edition.

Docker 基础知识Docker basics

首先来熟悉几个概念。There are a few concepts you should be familiar with. Docker 客户端具有可用于管理映像和容器的 CLI。The Docker client has a CLI that you can use to manage images and containers. 如前文所述,应花时间通读 Docker 概述文档。As previously stated, you should take the time to read through the Docker overview documentation.


映像是构成容器基础的文件系统更改的有序集合。An image is an ordered collection of filesystem changes that form the basis of a container. 映像不具有状态,并且是只读的。The image doesn't have a state and is read-only. 大多情况下,一个映像以另一个映像为基础,但具有一些自定义设置。Much the time an image is based on another image, but with some customization. 例如,为应用程序创建新映像时,可将其基于已包含 .NET Core 运行时的现有映像。For example, when you create an new image for your application, you would base it on an existing image that already contains the .NET Core runtime.

由于容器是从映像创建的,因此,映像具有一组在容器启动时运行的运行参数(例如启动可执行文件)。Because containers are created from images, images have a set of run parameters (such as a starting executable) that run when the container starts.


容器是映像的可运行实例。A container is a runnable instance of an image. 生成映像时,部署应用程序和依赖项。As you build your image, you deploy your application and dependencies. 然后可以实例化多个容器,且每个容器相互独立。Then, multiple containers can be instantiated, each isolated from one another. 每个容器实例具有其自己的文件系统、内存和网络接口。Each container instance has its own filesystem, memory, and network interface.


容器注册表是映像存储库的集合。Container registries are a collection of image repositories. 映像可以基于注册表映像。You can base your images on a registry image. 可以直接从注册表中的映像创建容器。You can create containers directly from an image in a registry. Docker 容器、映像和注册表之间的关系构建和生成容器化应用程序或微服务时的一个重要概念。The relationship between Docker containers, images, and registries is an important concept when architecting and building containerized applications or microservices. 此方法大大缩短了开发和部署之间的时间。This approach greatly shortens the time between development and deployment.

Docker 具有一个托管在 Docker 中心的公共注册表,可供用户使用。Docker has a public registry hosted at the Docker Hub that you can use. .NET Core 相关映像均在 Docker 中心列出。.NET Core related images are listed at the Docker Hub.

Microsoft 容器注册表 (MCR) 是 Microsoft 提供的容器映像的官方来源。The Microsoft Container Registry (MCR) is the official source of Microsoft-provided container images. MCR 构建在 Azure CDN 之上,可提供用于全局复制的映像。The MCR is built on Azure CDN to provide globally-replicated images. 但是,MCR 没有面向公众的网站,了解有关 Microsoft 提供的容器映像的主要方法是通过 Microsoft Docker 中心页面However, the MCR does not have a public-facing website and the primary way to learn about Microsoft-provided container images is through the Microsoft Docker Hub pages.


Dockerfile 是定义可创建映像的一组指令的文件。A Dockerfile is a file that defines a set of instructions that creates an image. Dockerfile 中的每个指令创建映像中的一个层。Each instruction in the Dockerfile creates a layer in the image. 大多数情况下,在重新生成映像时,只会重新生成已发生更改的层。For the most part, when you rebuild the image, only the layers that have changed are rebuilt. 可以将 Dockerfile 分发给其他人,便于他们采用你创建映像的方式重新创建一个新映像。The Dockerfile can be distributed to others and allows them to recreate a new image in the same manner you created it. 尽管可以分发有关如何创建映像的指令,但分发映像的主要方式是将其发布到注册表。While this allows you to distribute the instructions on how to create the image, the main way to distribute your image is to publish it to a registry.

.NET Core 映像.NET Core images

官方 .NET Core Docker 映像发布到 Microsoft 容器注册表 (MCR),用户可以在 Microsoft.NET Core Docker 中心存储库中找到这些映像。Official .NET Core Docker images are published to the Microsoft Container Registry (MCR) and are discoverable at the Microsoft .NET Core Docker Hub repository. 每个存储库包含 .NET(SDK 或运行时)和可以使用的操作系统的不同组合的映像。Each repository contains images for different combinations of the .NET (SDK or Runtime) and OS that you can use.

Microsoft 提供适合特定场景的映像。Microsoft provides images that are tailored for specific scenarios. 例如,ASP.NET Core 存储库提供针对在生产环境中运行 ASP.NET Core 应用生成的映像。For example, the ASP.NET Core repository provides images that are built for running ASP.NET Core apps in production.

Azure 服务Azure services

各种 Azure 服务都支持容器。Various Azure services support containers. 为应用程序创建 Docker 映像并将其部署到以下服务之一:You create a Docker image for your application and deploy it to one of the following services:

后续步骤Next steps