事前準備
- 如果您不熟悉容器,請參閱「Compute Engine 上的容器」。
- 如果您對 Docker 不熟悉,請閱讀 Docker 說明文件。
- 進一步瞭解在 Compute Engine 上部署容器。
-
如果尚未設定,請先設定驗證機制。驗證是指驗證身分,以便存取 Google Cloud 服務和 API 的程序。如要在本機開發環境中執行程式碼或範例,您可以選取下列任一選項,向 Compute Engine 進行驗證:
Select the tab for how you plan to use the samples on this page:
Console
When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.
gcloud
-
After installing the Google Cloud CLI, initialize it by running the following command:
gcloud init
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
- Set a default region and zone.
-
您建立執行個體或執行個體範本,用於在 VM 和 MIG 上部署容器時,請使用 Google Cloud 主控台或 Google Cloud CLI 指定容器設定。
以下各節將說明如何設定 VM 執行個體的選項,不過您也可以在建立執行個體範本時設定下列選項。使用 Google Cloud 控制台或 Google Cloud CLI,為執行個體範本中的 VM 執行個體設定選項。
主控台
前往「Create an instance template」(建立執行個體範本) 頁面。
在「Container」區段中,勾選「Deploy a container image to this VM instance」核取方塊,然後展開「Advanced container options」。
gcloud
- 在 Google Cloud CLI 中,請使用
gcloud compute instance-templates create-with-container
指令,如以下範例所示。
指定重新啟動政策
您可以設定重新啟動政策,以指定是否在退出時重新啟動容器。預設政策是永遠重新啟動。您也能將政策設定為在容器失敗時重新啟動容器,或永遠不重新啟動容器。
主控台
前往「Create an instance」(建立執行個體) 頁面。
在「容器」部分中,按一下「部署容器」。
在「Configure container」頁面上,執行下列操作:
- 指定容器映像檔名稱。
- 在「Restart policy」部分中,選取容器的重新啟動政策。
- 如要確認容器詳細資料,請按一下「選取」。
繼續執行 VM 建立程序。
gcloud
使用 --container-restart-policy
旗標指定容器的重新啟動政策:
always
(預設)on-failure
never
以下範例使用 on-failure
重新啟動政策啟動容器,也就是只在容器結束代碼非零時重新啟動:
gcloud compute instances create-with-container busybox-vm \ --container-image docker.io/busybox:1.27 \ --container-restart-policy on-failure
使用 gcloud compute instances update-container
指令搭配 --container-restart-policy
旗標,作為在 VM 執行容器的重新啟動政策。
在特權模式中執行容器
您可在特權模式中執行容器,以允許存取主機上的所有裝置。容器預設以「無特權」模式執行,且無法存取任何裝置。
主控台
前往「Create an instance」(建立執行個體) 頁面。
在「容器」部分中,按一下「部署容器」。
在「Configure container」頁面上,執行下列操作:
- 指定容器映像檔名稱。
- 選取「Run as privileged」(以特殊權限執行)。
- 如要確認容器詳細資料,請按一下「選取」。
繼續執行 VM 建立程序。
gcloud
使用 --container-privileged
旗標執行具備執行階段特權的容器。下列範例會在特權模式中啟動 busybox 容器:
gcloud compute instances create-with-container busybox-vm \ --container-image docker.io/busybox:1.27 \ --container-privileged
使用 gcloud compute instances update-container
指令搭配 --container-privileged
旗標在 VM 更新容器。使用 --no-container-privileged
旗標關閉特權模式。
為容器執行階段中的 STDIN 分配緩衝區
您可為容器執行階段中的 STDIN
分配緩衝區,以保持容器中的 STDIN
串流開啟。如果沒有做這項設定,從容器中 STDIN
的讀取將永遠造成 EOF
。
您必須分配虛擬 TTY (還必須讓 STDIN
串流保持開啟),以便在容器建立互動殼層,並讓容器能夠透過管道接收自己的標準輸入內容。
主控台
前往「Create an instance」(建立執行個體) 頁面。
在「容器」部分中,按一下「部署容器」。
在「Configure container」頁面上,執行下列操作:
- 指定容器映像檔名稱。
- 選取「Allocate a buffer for STDIN」。
- 如要確認容器詳細資料,請按一下「選取」。
繼續執行 VM 建立程序。
gcloud
使用 --container-stdin
旗標為容器執行階段的 STDIN
分配緩衝區。下列範例會啟動容器並保持其 STDIN
開啟:
gcloud compute instances create-with-container busybox-vm \ --container-image docker.io/busybox:1.27 \ --container-stdin
使用 gcloud compute instances update-container
指令搭配 --container-stdin
旗標來更新 VM 上的容器。使用 --no-container-stdin
旗標關閉 STDIN
的緩衝區分配。
分配虛擬 TTY
您必須為容器分配虛擬 TTY (還要為 STDIN 分配緩衝區),以便在容器中建立互動殼層。
主控台
前往「Create an instance」(建立執行個體) 頁面。
在「容器」部分中,按一下「部署容器」。
在「Configure container」頁面上,執行下列操作:
- 指定容器映像檔名稱。
- 選取「Allocate a pseudo-TTY」。
- 如要確認容器詳細資料,請按一下「選取」。
繼續執行 VM 建立程序。
gcloud
使用 --container-tty
旗標分配虛擬 TTY。以下範例會啟動容器並分配虛擬 TTY:
gcloud compute instances create-with-container busybox-vm \ --container-image docker.io/busybox:1.27 \ --container-stdin \ --container-tty
使用 gcloud compute instances update-container
指令搭配 --container-tty
旗標在 VM 更新容器。使用 --no-container-tty
旗標不分配虛擬 TTY。
覆寫在容器啟動時執行的預設指令
容器映像檔的 ENTRYPOINT
指定在容器啟動時執行的執行檔,並讓您將容器當成該二進位檔來執行。
您可覆寫容器映像檔的 ENTRYPOINT
指令。
主控台
前往「Create an instance」(建立執行個體) 頁面。
在「容器」部分中,按一下「部署容器」。
在「Configure container」頁面上,執行下列操作:
- 指定容器映像檔名稱。
- 在「Command」欄位中,輸入不含參數的單一可執行指令,例如
uptime
。 - 如要確認容器詳細資料,請按一下「選取」。
繼續執行 VM 建立程序。
gcloud
使用 --container-command
旗標覆寫容器映像檔 ENTRYPOINT
。以下範例在 busybox 容器執行 uptime
指令,顯示自上次開機後經過的時間:
gcloud compute instances create-with-container busybox-vm \ --container-image docker.io/busybox:1.27 \ --container-command "uptime"
使用 gcloud compute instances update-container
指令搭配 --container-command
旗標更新 VM 上容器的指令。
使用 --clear-container-command
旗標搭配 update-container
指令清除已更新容器的預設指令。
將引數傳送至容器 ENTRYPOINT 指令
您可以將引數傳送 (附加) 到容器 ENTRYPOINT
指令,或是覆寫預設的容器 CMD
指令。
主控台
前往「Create an instance」(建立執行個體) 頁面。
在「容器」部分中,按一下「部署容器」。
在「Configure container」頁面上,執行下列操作:
- 指定容器映像檔名稱。
- 在「引數」部分中,按一下「新增引數」。
- 在每個方塊中輸入一個指令引數。
- 如要確認容器詳細資料,請按一下「選取」。
繼續執行 VM 建立程序。
gcloud
使用 --container-arg
旗標將引數傳送至容器映像檔 ENTRYPOINT
指令。為每一引數使用個別旗標。
以下範例會在設定為自動執行 busybox 的容器中,執行含 -c 'ls -l'
引數的 /bin/ash
指令:
gcloud compute instances create-with-container busybox-vm \ --container-image docker.io/busybox:1.27 \ --container-command "/bin/ash" \ --container-arg="-c" \ --container-arg="ls -l"
使用 gcloud compute instances update-container
指令搭配 --container-arg
旗標,針對在 VM 執行的容器更新指令引數。更新時會以新清單取代完整的引數清單。
使用 --clear-container-args
旗標搭配 update-container
指令由容器宣告移除所有引數。
設定記錄驅動程式選項
如果您需要設定記錄驅動程式選項,可以建立 VM 啟動指令碼,以所需的記錄選項更新 Docker 設定檔。這些選項適用於在 VM 上執行且未指定記錄驅動程式選項的所有容器。
舉例來說,以下啟動指令碼會設定多個選項 (包括限制容器記錄大小的選項),然後在 VM 上重新啟動 Docker:
cat <<EOF > /etc/docker/daemon.json { "live-restore": true, "storage-driver": "overlay2", "log-opts": { "max-size": "10m" } } EOF systemctl restart docker
設定環境變數
您可設定容器內的環境變數。當 KEY
不只重複一次時,系統只會採用 KEY
的最後一個值。
主控台
前往「Create an instance」(建立執行個體) 頁面。
在「容器」部分中,按一下「部署容器」。
在「Configure container」頁面上,執行下列操作:
- 指定容器映像檔名稱。
- 在「環境變數」部分,按一下「新增變數」。
- 視需要新增或移除環境變數,每行一個。
- 如要確認容器詳細資料,請按一下「選取」。
繼續執行 VM 建立程序。
gcloud
使用 --container-env
旗標設定容器內的環境變數。下列範例設定三個環境變數:HOME
、MODE
與 OWNER
:
gcloud compute instances create-with-container busybox-vm \ --container-image docker.io/busybox:1.27 \ --container-env HOME=/home,MODE=test,OWNER=admin
使用 --container-env-file
旗標由本機檔案設定環境變數。以下範例會由 env.txt
檔案設定兩個環境變數:
gcloud compute instances create-with-container busybox-vm \ --container-image docker.io/busybox:1.27 \ --container-env-file ./env.txt
env.txt
檔案的內容如下:
# this is a comment
HOME=/home
MODE=test
OWNER=admin
使用 gcloud compute instances update-container
指令搭配 --container-env
或 --container-env-file
旗標,更新 VM 容器的環境變數。這會更新 VM 執行個體容器宣告中的任何變數,並會新增不在容器宣告中的變數。
更新 VM 上的容器時,請使用 --remove-container-env
旗標移除環境變數。以下範例移除名為 MODE
及 OWNER
的環境變數:
gcloud compute instances update-container busybox-vm \ --remove-container-env MODE,OWNER
如果指定的環境變數不存在,表示它遭到無訊息忽略。
將主機目錄作為資料磁碟區來掛接
您可將主機 VM 的目錄掛接到容器中。
主控台
前往「Create an instance」(建立執行個體) 頁面。
在「容器」部分中,按一下「部署容器」。
在「Configure container」頁面上,執行下列操作:
- 指定容器映像檔名稱。
- 在「Volume mounts」(磁碟區掛接) 部分,按一下「Add volume」(新增磁碟區)。
在「Volume type」清單中選取「Directory」,然後執行下列操作:
- 在「掛接路徑」欄位中,指定容器目錄結構中用於掛接主機目錄的掛接路徑。
- 在「Host path」欄位中,指定要掛接的主機目錄主機路徑。
- 在「Mode」清單中,指定是否在讀取/寫入或唯讀模式中掛接目錄。
如要確認容器詳細資料,請按一下「選取」。
繼續執行 VM 建立程序。
gcloud
使用 --container-mount-host-path
旗標將主機 VM 目錄掛接至容器。以下範例以讀/寫模式將主機目錄 /tmp
掛接至 /logs
的容器:
gcloud compute instances create-with-container busybox-vm \ --container-image docker.io/busybox:1.27 \ --container-mount-host-path mount-path=/logs,host-path=/tmp,mode=rw
指定 mode=ro
以唯讀模式掛接主機目錄。
使用 gcloud compute instances update-container
指令搭配 --container-mount-host-path
旗標更新掛接在容器的主機目錄。使用 --remove-container-mounts
旗標移除具指定掛接路徑的磁碟區掛接。以下範例利用 mount-path=/logs
移除主機路徑掛接:
gcloud compute instances update-container busybox-vm \ --remove-container-mounts /logs
如果指定的掛接路徑不存在,系統將略過而不顯示任何訊息。
將 tmpfs 檔案系統作為資料磁碟區來掛接
您可將空白的 tmpfs 檔案系統掛接到容器中。
主控台
前往「Create an instance」(建立執行個體) 頁面。
在「容器」部分中,按一下「部署容器」。
在「Configure container」頁面上,執行下列操作:
- 指定容器映像檔名稱。
- 在「Volume mounts」(磁碟區掛接) 部分,按一下「Add volume」(新增磁碟區)。
- 從「Volume type」清單中選取「TmpFS」。
- 在「掛接路徑」欄位中,指定您要掛接 TmpFS 磁碟區所在的容器目錄結構內的掛接路徑。
- 在「Mode」清單中,指定是否以讀取/寫入或唯讀模式掛接 TmpFS 磁碟區。
- 如要確認容器詳細資料,請按一下「選取」。
繼續執行 VM 建立程序。
gcloud
使用 --container-mount-tmpfs
旗標將空的 tmpfs
檔案系統掛接至容器。以下範例以讀/寫模式將 tmpfs
檔案系統掛接至 /cache
的容器:
gcloud compute instances create-with-container busybox-vm \ --container-image docker.io/busybox:1.27 \ --container-mount-tmpfs mount-path=/cache
使用 gcloud compute instances update-container
指令搭配 --container-mount-tmpfs
旗標更新容器上的 tmpfs
掛接。更新時使用 --remove-container-mounts
旗標移除具指定掛接路徑的 tmpfs
掛接。以下範例會移除含 mount-path=/cache
的 tmpfs
掛接:
gcloud compute instances update-container busybox-vm \ --remove-container-mounts /cache
如果指定的掛接路徑不存在,系統將略過而不顯示任何訊息。
將永久磁碟掛接為資料磁碟區
您只要利用 Container-Optimized OS 69 或之後的版本,就可以將主機 VM 的永久磁碟掛接到容器中。
事前準備
- 磁碟必須具備
ext4
檔案系統,或是沒有檔案系統。如果沒有初始檔案系統,容器啟動代理程式會將磁碟格式化為ext4
,且只支援讀取/寫入附件和掛接功能。 - 磁碟必須附加到 VM。
支援無分區裝置和分區掛接。針對分區掛接,磁碟不得空白,其中必須包含現有的分區資料表。
主控台
前往「Create an instance」(建立執行個體) 頁面。
在「容器」部分中,按一下「部署容器」。
在「Configure container」頁面上,執行下列操作:
- 指定容器映像檔名稱。
- 在「Volume mounts」(磁碟區掛接) 部分,按一下「Add volume」(新增磁碟區)。
- 在「Volume type」(磁碟區類型) 清單中選取「Disk」。
- 在「掛接路徑」欄位中,指定您要掛接永久磁碟所在的容器目錄結構內部路徑。
- 從「Disk name」(磁碟名稱) 清單中選取要掛接的現有磁碟。
- 如果磁碟有磁區分割表,請在「Partition」欄位中指定要掛接的分區號碼。如果磁碟沒有分區,請將這個欄位留空。
- 在「Mode」清單中,指定是否在讀取/寫入或唯讀模式中掛接目錄。
- 如要確認容器詳細資料,請按一下「選取」。
繼續執行 VM 建立程序。
gcloud
使用 gcloud compute instances create-with-container
指令或 gcloud compute instances update-container
指令搭配 --container-mount-disk
旗標將永久磁碟掛接至容器。
以下範例會將 my-data-disk
和 my-scratch-disk
這兩個磁碟掛接至 /disks/data-disk
和 /disks/scratch-disk
掛接路徑的容器中。
gcloud compute instances create-with-container busybox-vm \ --disk name=my-data-disk \ --create-disk name=my-scratch-disk,auto-delete=yes,image=ubuntu-1710-artful-v20180315,image-project=ubuntu-os-cloud \ --container-image docker.io/busybox:1.27 \ --container-mount-disk mount-path="/disks/data-disk",name=my-data-disk,mode=ro \ --container-mount-disk mount-path="/disks/scratch-disk",name=my-scratch-disk
請注意 --disk
旗標會附加 my-data-disk
,--create-disk
旗標會建立及附加 my-scatch-disk
,而 --container-mount-disk
旗標會將附加的磁碟掛接至容器。由於 mode
並未指定用於 my-scratch-disk
,該磁碟將依據預設以讀取/寫入模式掛接至容器。
使用 gcloud compute instances update-container
指令搭配 --container-mount-disk
旗標掛接額外附加的磁碟,或是修改現有磁碟掛接。
使用 --remove-container-mounts
旗標移除具有指定掛接路徑的磁碟區掛接。以下範例將 my-data-disk
掛接模式變更為讀取/寫入,並以 mount-path="/disks/scratch-disk"
移除磁碟掛接。
gcloud compute instances update-container busybox-vm \ --container-mount-disk mount-path="/disks/data-disk",name=my-data-disk,mode=rw \ --remove-container-mounts "/disks/scratch-disk"
如果您傳送至 --remove-container-mounts
旗標的掛接路徑不存在,系統會無訊息忽略該路徑。
發佈容器通訊埠
擁有容器的 VM 會使用主機網路模式,讓容器能夠共用主機的網路堆疊,還能使用該主機的所有介面。
容器通訊埠具有對於主機 VM 通訊埠的一對一對應。例如容器通訊埠 80 對應至主機 VM 通訊埠 80。Compute Engine 不支援通訊埠發布 (-p
) 旗標,您不必指定這項旗標,對應就會發揮作用。
如要發布容器通訊埠,請設定防火牆規則以存取主機 VM 執行個體通訊埠。容器對應的通訊埠可依據防火牆規則自動存取。
範例:NGINX 容器的發布通訊埠 80
以下範例顯示如何使用 NGINX 容器建立 VM 執行個體,並允許流量傳送至容器通訊埠 80。
以 NGINX 容器建立 VM 執行個體:
gcloud compute instances create-with-container nginx-vm \ --container-image gcr.io/cloud-marketplace/google/nginx1:1.15 \ --tags http-server
容器共用主機 VM 的網路堆疊,而且容器的通訊埠 80 發布到主機 VM 的通訊埠 80。
http-server
標記作為防火牆規則的目標標記,於下個步驟建立。建立防火牆規則,以便連線至 VM 執行個體的通訊埠 80。 下列防火牆規則允許利用
http-server
標記與 VM 執行個體進行 HTTP 連線。gcloud compute firewall-rules create allow-http \ --allow tcp:80 --target-tags http-server
容器會自動開始在通訊埠 80 接收流量。您不需要進行任何其他設定。
您可以為主機 VM 通訊協定組合建立防火牆規則,其中通訊協定為
tcp
或udp
。這些規則可有效管理從 VM 以外的地方存取相對應容器通訊埠的權限。