網頁

2020/5/2

Docker Machine 設定Docker Client連線環境變數 set connection environment variable

設定Docker Client連線到machine的環境變數。

範立環境:

  • Windows 7 64Bit
  • Docker Machine version 0.16.1, build cce350d7
  • Oracle VirtualBox 版本 5.2.20 r125813 (Qt5.6.2)

注意:本範例的Docker Machine及VirtualBox是由Docker Toolbox安裝


在本機執行Docker命令時,實際上是Docker Client(Docker CLI)透過Docker daemon的REST API去控制Docker daemon。

+--------------------------------------------------+
|                  Docker Engine                   |
| +--------------+    +--------------------------+ |
| | Docker Client+--->+ REST API +-------------+ | |
| | (Docker CLI) |    |          |Docker daemon| | |
| +--------------+    |          +-------------+ | |
|                     +--------------------------+ |
+--------------------------------------------------+


而使用Docker Machine在VM中運行的Docker Engine對本機的Docker Client來說實際上是遠端的Docker。

                                 +------------------------------------------+
                                 |            VM OS (VirtualBox)            |
                                 |   +----------------------------------+   |
                                 |   |       Docker host/machine        |   |
                                 |   |            (default)             |   |
                                 |   | +------------------------------+ |   |
                                 |   | |         Docker Engine        | |   |
                                 |   | | +--------------------------+ | |   |
                                 |   | | | REST API +-------------+ | | |   |
                                 |   | | |          |Docker daemon| | | |   |
+--------------------+           |   | | |          +-------------+ | | |   |
|   Docker Machine   |           |   | | +--------------------------+ | |   |
|  +--------------+  |           |   | +------------------------------+ |   |
|  | Docker Client|  +------+    |   +----------------------------------+   |
|  | (Docker CLI) |  |      |    |                                          |
|  +--------------+  |      |    |   +----------------------------------+   |
+--------------------+      |    |   |       Docker host/machine        |   |
                            |    |   |          (demo-docker)           |   |
                            |    |   | +------------------------------+ |   |
                            |    |   | |         Docker Engine        | |   |
                            |    |   | | +--------------------------+ | |   |
                            +--------> | | REST API +-------------+ | | |   |
                                 |   | | |          |Docker daemon| | | |   |
                                 |   | | |          +-------------+ | | |   |
                                 |   | | +--------------------------+ | |   |
                                 |   | +------------------------------+ |   |
                                 |   +----------------------------------+   |
                                 +------------------------------------------+

當VM中有多個Docker時,就必須告訴Docker Client要連線到VM中哪個Docker host/machine來執行操作,也就是以下要說明的內容。

在Docker Client(e.g. MacOS的terminal bash或Windows的cmd或powershell)輸入docker-machine ls查詢VM中的machine列表。

C:\>docker-machine ls
NAME          ACTIVE   DRIVER       STATE     URL                         SWARM  DOCKER     ERRORS
default       *        virtualbox   Running   tcp://192.168.99.100:2376          v19.03.5
demo-docker   -        virtualbox   Running   tcp://192.168.99.101:2376          v19.03.5

例如現在VM中運行的有default及demo-docker兩個machine。若Docker Client要操作demo-docker中的docker,就必須重新配置Docker Client的連線參數,也就是Docker Client的環境變數。

使用docker-machine env <machine-name>印出設定環境變數連線到指定machine的指令(僅印出命令,不會對設定進行修改)。<machine-name>為machine名稱。

例如要連到demo-docker,則輸入docker-machine env demo-docker先印出設定Docker Client環境變數的指令。

C:\>docker-machine env demo-docker
SET DOCKER_TLS_VERIFY=1
SET DOCKER_HOST=tcp://192.168.99.101:2376
SET DOCKER_CERT_PATH=C:\Users\matt\.docker\machine\machines\demo-docker
SET DOCKER_MACHINE_NAME=demo-docker
SET COMPOSE_CONVERT_WINDOWS_PATHS=true
REM Run this command to configure your shell:
REM     @FOR /f "tokens=*" %i IN ('docker-machine env demo-docker') DO @%i

Docker Machine會自動偵測使用的Docker Client並印出相對的指令,例如上面印出的是Windows cmd設定環境變數的指令。

如果是Docker Client是Bash,則印出Bash設定環境變數的指令。

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

透過在Docker Client執行以上印出的註解說明中的指令即可設定Docker Client要連線的machine。

例如在Bash輸入eval "$(docker-machine env demo-docker)"即可重設連線到demo-docker的Docker Client環境變數。


沒有留言:

張貼留言