教程:创建要部署到 Azure 容器实例的容器映像Tutorial: Create a container image for deployment to Azure Container Instances

使用 Azure 容器实例可将 Docker 容器部署到 Azure 基础结构,不需要预配任何虚拟机,也不需要采用更高级别的服务。Azure Container Instances enables deployment of Docker containers onto Azure infrastructure without provisioning any virtual machines or adopting a higher-level service. 在本教程中,我们将一个小型 Node.js Web 应用程序打包成可以使用 Azure 容器实例运行的容器映像。In this tutorial, you package a small Node.js web application into a container image that can be run using Azure Container Instances.

本文(本系列的第一部分)将介绍如何:In this article, part one of the series, you:

  • 克隆 GitHub 中的应用程序源代码Clone application source code from GitHub
  • 从应用程序源创建容器映像Create a container image from application source
  • 在本地 Docker 环境中测试映像Test the image in a local Docker environment

在本教程的第二和第三部分,需要将该映像上传到 Azure 容器注册表,然后将其部署到 Azure 容器实例。In tutorial parts two and three, you upload your image to Azure Container Registry, and then deploy it to Azure Container Instances.

开始之前Before you begin

必须满足以下要求才能完成本教程:You must satisfy the following requirements to complete this tutorial:

Azure CLI:必须在本地计算机上安装 Azure CLI 2.0.29 或更高版本。Azure CLI: You must have Azure CLI version 2.0.29 or later installed on your local computer. 运行 az --version 即可查找版本。Run az --version to find the version. 如需进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install the Azure CLI.

Docker:本教程假设读者基本了解核心 Docker 概念,如容器、容器映像和基本的 docker 命令。Docker: This tutorial assumes a basic understanding of core Docker concepts like containers, container images, and basic docker commands. 有关 Docker 和容器的基础知识,请参阅 Docker 概述For a primer on Docker and container basics, see the Docker overview.

Docker:要完成本教程,需在本地安装 Docker。Docker: To complete this tutorial, you need Docker installed locally. Docker 提供用于在 macOSWindowsLinux 上配置 Docker 环境的包。Docker provides packages that configure the Docker environment on macOS, Windows, and Linux.

重要

必须 在本地计算机 上同时安装 Azure CLI 和 Docker 引擎才能完成本教程。You must install both the Azure CLI and Docker Engine on your local computer to complete this tutorial.

获取应用程序代码Get application code

本教程中的示例应用程序是使用 Node.js 生成的简单 Web 应用。The sample application in this tutorial is a simple web app built in Node.js. 该应用程序提供一个静态 HTML 页面,如以下屏幕截图所示:The application serves a static HTML page, and looks similar to the following screenshot:

显示在浏览器中的教程应用

使用 Git 克隆示例应用程序的存储库:Use Git to clone the sample application's repository:

git clone https://github.com/Azure-Samples/aci-helloworld.git

也可以从 GitHub 直接下载 ZIP 存档You can also download the ZIP archive from GitHub directly.

生成容器映像Build the container image

示例应用程序中的 Dockerfile 演示如何生成容器。The Dockerfile in the sample application shows how the container is built. 它从基于 Alpine Linux正式 Node.js 映像(适用于容器的小型分发版)开始演示。It starts from an official Node.js image based on Alpine Linux, a small distribution that is well suited for use with containers. 然后,它将应用程序文件复制到容器中,使用 Node 包管理器安装依赖项,最后启动应用程序。It then copies the application files into the container, installs dependencies using the Node Package Manager, and finally, starts the application.

FROM node:8.9.3-alpine
RUN mkdir -p /usr/src/app
COPY ./app/ /usr/src/app/
WORKDIR /usr/src/app
RUN npm install
CMD node /usr/src/app/index.js

使用 docker build 命令创建容器映像,并将其标记为 aci-tutorial-app :Use the docker build command to create the container image and tag it as aci-tutorial-app:

docker build ./aci-helloworld -t aci-tutorial-app

docker build 命令的输出类似于以下内容(为方便阅读,已进行截断处理):Output from the docker build command is similar to the following (truncated for readability):

$ docker build ./aci-helloworld -t aci-tutorial-app
Sending build context to Docker daemon  119.3kB
Step 1/6 : FROM node:8.9.3-alpine
8.9.3-alpine: Pulling from library/node
88286f41530e: Pull complete
84f3a4bf8410: Pull complete
d0d9b2214720: Pull complete
Digest: sha256:c73277ccc763752b42bb2400d1aaecb4e3d32e3a9dbedd0e49885c71bea07354
Status: Downloaded newer image for node:8.9.3-alpine
 ---> 90f5ee24bee2
...
Step 6/6 : CMD node /usr/src/app/index.js
 ---> Running in f4a1ea099eec
 ---> 6edad76d09e9
Removing intermediate container f4a1ea099eec
Successfully built 6edad76d09e9
Successfully tagged aci-tutorial-app:latest

使用 docker images 命令查看生成的映像:Use the docker images command to see the built image:

docker images

新生成的映像应会出现在列表中:Your newly built image should appear in the list:

$ docker images
REPOSITORY          TAG       IMAGE ID        CREATED           SIZE
aci-tutorial-app    latest    5c745774dfa9    39 seconds ago    68.1 MB

在本地运行容器Run the container locally

将容器部署到 Azure 容器实例之前,请使用 docker run 在本地运行该容器,并确认它是否正常工作。Before you deploy the container to Azure Container Instances, use docker run to run it locally and confirm that it works. -d 开关可让容器在后台运行,而 -p 可将计算机上的任意端口映射到容器中的端口 80。The -d switch lets the container run in the background, while -p allows you to map an arbitrary port on your computer to port 80 in the container.

docker run -d -p 8080:80 aci-tutorial-app

如果 docker run 命令成功,其输出会显示正在运行的容器的 ID:Output from the docker run command displays the running container's ID if the command was successful:

$ docker run -d -p 8080:80 aci-tutorial-app
a2e3e4435db58ab0c664ce521854c2e1a1bda88c9cf2fcff46aedf48df86cccf

现在,请在浏览器中导航到 http://localhost:8080 ,确认容器是否正在运行。Now, navigate to http://localhost:8080 in your browser to confirm that the container is running. 应会看到如下所示的网页:You should see a web page similar to the following:

在浏览器中以本地方式运行应用

后续步骤Next steps

在本教程中,我们已创建一个可以部署到 Azure 容器实例的容器映像,并已验证它在本地运行。In this tutorial, you created a container image that can be deployed in Azure Container Instances, and verified that it runs locally. 到目前为止,我们已完成以下操作:So far, you've done the following:

  • 从 GitHub 克隆应用程序源Cloned the application source from GitHub
  • 从应用程序源创建容器映像Created a container image from the application source
  • 在本地测试容器Tested the container locally

请转到本系列中的下一篇教程,了解如何在 Azure 容器注册表中存储容器映像:Advance to the next tutorial in the series to learn about storing your container image in Azure Container Registry: