如何使用 Docker Machine 在 Azure 中创建主机How to use Docker Machine to create hosts in Azure

本文详细介绍如何使用 Docker Machine 在 Azure 中创建主机。This article details how to use Docker Machine to create hosts in Azure. docker-machine 命令在 Azure 中创建一个 Linux 虚拟机 (VM),然后安装 Docker。The docker-machine command creates a Linux virtual machine (VM) in Azure then installs Docker. 然后,可以使用相同的本地工具和工作流来管理 Azure 中的 Docker 主机。You can then manage your Docker hosts in Azure using the same local tools and workflows. 若要在 Windows 10 中使用 docker-machine,必须使用 Linux bash。To use docker-machine in Windows 10, you must use Linux bash.

使用 Docker 计算机创建 VMCreate VMs with Docker Machine

首先,使用 az account show 获取 Azure 订阅 ID,如下所示:First, obtain your Azure subscription ID with az account show as follows:

sub=$(az account show --query "id" -o tsv)

通过指定 azure 作为驱动程序,在 Azure 中使用 docker-machine create 创建 Docker 主机 VM。You create Docker host VMs in Azure with docker-machine create by specifying azure as the driver. 有关详细信息,请参阅 Docker Azure 驱动程序文档For more information, see the Docker Azure Driver documentation

以下示例基于“标准 D2 v2”计划创建一个名为“myVM”的 VM,创建一个名为“azureuser”的用户帐户,并在主 VM 上打开端口 80The following example creates a VM named myVM, based on "Standard D2 v2" plan, creates a user account named azureuser, and opens port 80 on the host VM. 按照任何提示登录 Azure 帐户,并授予 Docker Machine 创建和管理资源的权限。Follow any prompts to log in to your Azure account and grant Docker Machine permissions to create and manage resources.

docker-machine create -d azure \
    --azure-subscription-id $sub \
    --azure-environment AzureChinaCloud \
    --azure-location chinanorth \
    --azure-image Canonical:UbuntuServer:16.04-LTS:latest \
    --azure-ssh-user azureuser \
    --azure-open-port 80 \
    --azure-size "Standard_DS2_v2" \
    myvm

输出与以下示例类似:The output looks similar to the following example:

Creating CA: /Users/user/.docker/machine/certs/ca.pem
Creating client certificate: /Users/user/.docker/machine/certs/cert.pem
Running pre-create checks...
(myvm) Completed machine pre-create checks.
Creating machine...
(myvm) Querying existing resource group.  name="docker-machine"
(myvm) Creating resource group.  name="docker-machine" location="chinanorth"
(myvm) Configuring availability set.  name="docker-machine"
(myvm) Configuring network security group.  name="myvm-firewall" location="chinanorth"
(myvm) Querying if virtual network already exists.  rg="docker-machine" location="chinanorth" name="docker-machine-vnet"
(myvm) Creating virtual network.  name="docker-machine-vnet" rg="docker-machine" location="chinanorth"
(myvm) Configuring subnet.  name="docker-machine" vnet="docker-machine-vnet" cidr="192.168.0.0/16"
(myvm) Creating public IP address.  name="myvm-ip" static=false
(myvm) Creating network interface.  name="myvm-nic"
(myvm) Creating storage account.  sku=Standard_LRS name="vhdski0hvfazyd8mn991cg50" location="chinanorth"
(myvm) Creating virtual machine.  location="chinanorth" size="Standard_A2" username="azureuser" osImage="Canonical:UbuntuServer:16.04-LTS:latest" name="myvm"
Waiting for machine to be running, this may take a few minutes...
Detecting operating system of created instance...
Waiting for SSH to be available...
Detecting the provisioner...
Provisioning with ubuntu(systemd)...
Installing Docker...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Checking connection to Docker...
Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env myvm

配置 Docker shellConfigure your Docker shell

若要连接到 Azure 中的 Docker 主机,请定义适当的连接设置。To connect to your Docker host in Azure, define the appropriate connection settings. 按输出末尾所示,查看 Docker 主机的连接信息,如下所示:As noted at the end of the output, view the connection information for your Docker host as follows:

docker-machine env myvm

输出类似于以下示例:The output is similar to the following example:

export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://40.68.254.142:2376"
export DOCKER_CERT_PATH="/Users/user/.docker/machine/machines/machine"
export DOCKER_MACHINE_NAME="machine"
# Run this command to configure your shell:
# eval $(docker-machine env myvm)

若要定义连接设置,可以运行建议的配置命令 (eval $(docker-machine env myvm)),也可以手动设置环境变量。To define the connection settings, you can either run the suggested configuration command (eval $(docker-machine env myvm)), or you can set the environment variables manually.

运行容器Run a container

若要查看运行中的容器,请运行一个基本的 NGINX Web 服务器。To see a container in action, lets run a basic NGINX webserver. 使用 docker run 创建一个容器,并为 Web 流量公开端口 80,如下所示:Create a container with docker run and expose port 80 for web traffic as follows:

docker run -d -p 80:80 --restart=always nginx

输出类似于以下示例:The output is similar to the following example:

Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
ff3d52d8f55f: Pull complete
226f4ec56ba3: Pull complete
53d7dd52b97d: Pull complete
Digest: sha256:41ad9967ea448d7c2b203c699b429abe1ed5af331cd92533900c6d77490e0268
Status: Downloaded newer image for nginx:latest
675e6056cb81167fe38ab98bf397164b01b998346d24e567f9eb7a7e94fba14a

使用 docker ps 查看正在运行的容器。View running containers with docker ps. 以下示例输出显示使用公开的端口 80 运行的 NGINX 容器:The following example output shows the NGINX container running with port 80 exposed:

CONTAINER ID    IMAGE    COMMAND                   CREATED          STATUS          PORTS                          NAMES
d5b78f27b335    nginx    "nginx -g 'daemon off"    5 minutes ago    Up 5 minutes    0.0.0.0:80->80/tcp, 443/tcp    festive_mirzakhani

测试容器Test the container

获取 Docker 主机的公共 IP 地址,如下所示:Obtain the public IP address of Docker host as follows:

docker-machine ip myvm

若要查看运行中的容器,请打开 Web 浏览器并输入上述命令输出中注明的公共 IP 地址:To see the container in action, open a web browser and enter the public IP address noted in the output of the preceding command:

运行 ngnix 容器

后续步骤Next steps

有关使用 Docker Compose 的示例,请参阅 Azure 中的 Docker 和 Compose 入门For examples on using Docker Compose, see Get started with Docker and Compose in Azure.