如何使用 Docker 機器在 Azure 中建立主機

本文詳細說明如何使用 Docker 機器 在 Azure 中建立主機。 命令會在 docker-machine Azure 中建立 Linux 虛擬機 (VM),然後安裝 Docker。 接著,您可以使用相同的本機工具和工作流程,在 Azure 中管理 Docker 主機。 若要在 Windows 10 中使用 docker-machine,您必須使用 Linux bash。

使用 Docker 機器建立 VM

首先,使用 az account 取得您的 Azure 訂用 帳戶標識碼,如下所示 :

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

您可以藉由將 azure 指定為驅動程式,在 Azure docker-machine create 中建立 Docker 主機 VM。 如需詳細資訊,請參閱 Docker Azure 驅動程序檔

下列範例會根據「標準 D2 v2」方案建立名為 myVM 的 VM、建立名為 azureuser 的用戶帳戶,並在主機 VM 上開啟埠 80 。 請遵循任何提示來登入您的 Azure 帳戶,並授與 Docker Machine 建立和管理資源的許可權。

docker-machine create -d azure \
    --azure-subscription-id $sub \
    --azure-ssh-user azureuser \
    --azure-open-port 80 \
    --azure-size "Standard_DS2_v2" \
    myvm

輸出的結果看起來會像以下範例:

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="westus"
(myvm) Configuring availability set.  name="docker-machine"
(myvm) Configuring network security group.  name="myvm-firewall" location="westus"
(myvm) Querying if virtual network already exists.  rg="docker-machine" location="westus" name="docker-machine-vnet"
(myvm) Creating virtual network.  name="docker-machine-vnet" rg="docker-machine" location="westus"
(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="westus"
(myvm) Creating virtual machine.  location="westus" size="Standard_A2" username="azureuser" osImage="canonical:UbuntuServer:16.04.0-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 的命令列介面

若要連線到 Azure 中的 Docker 主機,請定義適當的連線設定。 如輸出結尾所述,檢視 Docker 主機的連線資訊,如下所示:

docker-machine env myvm

輸出類似於下列範例:

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)),也可以手動設定環境變數。

執行容器

若要查看作用中的容器,請執行基本的 NGINX Web 伺服器。 使用 docker run 建立容器,並將埠 80 用於 Web 流量,如下所示:

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

輸出類似於下列範例:

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。 範例輸出顯示 NGINX 容器在暴露的連接埠 80 上運行:

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

測試容器

取得 Docker 主機的公用 IP 位址,如下所示:

docker-machine ip myvm

若要查看作用中的容器,請開啟網頁瀏覽器,並在上述命令的輸出中輸入公用IP位址:

執行ngnix容器

後續步驟

如需使用 Docker Compose 的範例,請參閱 開始使用 Azure 中的 Docker 和 Compose。