要在macOS High Sierra上为Docker设置大量VM(使用Docker Desktop 2.0.5.0,但这在以前的版本中已发生。)我运行以下shell脚本:
#!/bin/bash
# some variables
MANAGER="swarmMGR"
WORKER="swarmWKR"
MAXNODE=3
# create VMs for swarm
# manager
docker-machine create --driver virtualbox $MANAGER
# workers
for (( i=1; i<=$MAXNODE; i++ ))
do
docker-machine create --driver virtualbox $WORKER$i
done
# find the manager's IP address
MANAGERIP=$(docker-machine ls | grep $MANAGER | egrep -o '([0-9]{1,3}[.]){3}[0-9]{1,3}')
# initialize the swarm
docker-machine ssh ${MANAGER} "docker swarm init --advertise-addr $MANAGERIP"
# workers join the swarm
# get the token
TOKEN=$(docker-machine ssh $MANAGER "docker swarm join-token worker -q")
# join up
for (( i=1; i<=$MAXNODE; i++ ))
do
echo
docker-machine ssh $WORKER$i "docker swarm join --token ${TOKEN} ${MANAGERIP}"
done
# configure the shell to expose the manager for Docker commands from the host
eval $(docker-machine env $MANAGER)
# list the machines
docker-machine ls
# list the nodes
docker-machine ssh $MANAGER "docker node ls"
eval $(docker-machine env $MANAGER)
似乎可以正常工作-这是脚本结尾处的输出:
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
swarmMGR * virtualbox Running tcp://192.168.99.197:2376 v18.09.6
swarmWKR1 - virtualbox Running tcp://192.168.99.198:2376 v18.09.6
swarmWKR2 - virtualbox Running tcp://192.168.99.199:2376 v18.09.6
swarmWKR3 - virtualbox Running tcp://192.168.99.200:2376 v18.09.6
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
ub2qbyd6ca8yzgsq1f0mjsp0i * swarmMGR Ready Active Leader 18.09.6
dopxb7bgyixqyh3z66rvtik2o swarmWKR1 Ready Active 18.09.6
vwbd11l36idnphdsoutls2hvp swarmWKR2 Ready Active 18.09.6
oe80ejzvnhsmhosvjvus6cvb1 swarmWKR3 Ready Active 18.09.6
如果我执行一个简单的命令,说docker node ls
,它现在应该在集群管理器上运行,则会出现以下错误:
守护程序的错误响应:该节点不是群集管理器。使用“ docker swarm init”或“ docker swarm join”将此节点连接到swarm并重试。
如果我手动运行eval()
命令,响应将正确。我做了很多研究,找不到解决该问题的方法。
从脚本运行eval()
命令时是否需要做些什么才能使其正常工作?
我使用-x
选项运行了有问题的Shell脚本部分,以输出一些调试信息:
#!/bin/bash
MANAGER="swarmMGR"
# configure the shell to expose the manager for Docker commands from the host
eval $(docker-machine env "$MANAGER")
# list the machines
docker-machine ls
# list the nodes
docker-machine ssh $MANAGER "docker node ls"
echo $DOCKER_HOST
以下是输出:
$ bash -x ./export.sh
+ MANAGER=swarmMGR
++ docker-machine env swarmMGR
+ eval export 'DOCKER_TLS_VERIFY="1"' export 'DOCKER_HOST="tcp://192.168.99.228:2376"' export 'DOCKER_CERT_PATH="/Users/foo/.docker/machine/machines/swarmMGR"' export 'DOCKER_MACHINE_NAME="swarmMGR"' '#' Run this command to configure your shell: '#' eval '$(docker-machine' env 'swarmMGR)'
++ export DOCKER_TLS_VERIFY=1 export DOCKER_HOST=tcp://192.168.99.228:2376 export DOCKER_CERT_PATH=/Users/foo/.docker/machine/machines/swarmMGR export DOCKER_MACHINE_NAME=swarmMGR
++ DOCKER_TLS_VERIFY=1
++ DOCKER_HOST=tcp://192.168.99.228:2376
++ DOCKER_CERT_PATH=/Users/jblanchard/.docker/machine/machines/swarmMGR
++ DOCKER_MACHINE_NAME=swarmMGR
+ docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
swarmMGR * virtualbox Running tcp://192.168.99.228:2376 v18.09.6
swarmWKR1 - virtualbox Running tcp://192.168.99.229:2376 Unknown Unable to query docker version: Cannot connect to the docker engine endpoint
swarmWKR2 - virtualbox Running tcp://192.168.99.230:2376 v18.09.6
swarmWKR3 - virtualbox Running tcp://192.168.99.231:2376 v18.09.6
+ docker-machine ssh swarmMGR 'docker node ls'
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
ryplgfjntuj7rfoapbl0bb8nj * swarmMGR Ready Active Leader 18.09.6
n89x4phb9jhilwz74zgta352r swarmWKR2 Ready Active 18.09.6
sp76b5vws2fcdpjuhxcqhywh3 swarmWKR3 Ready Active 18.09.6
+ echo tcp://192.168.99.228:2376
tcp://192.168.99.228:2376
只要脚本是“活动的”,就可以正确设置环境变量。当脚本退出时,然后在从命令行运行echo $DOCKER_HOST
后立即取消设置变量,不会产生任何结果。
答案 0 :(得分:0)
当我继续研究问题时,终于在this question的AskUbuntu上找到了答案。事实证明,在脚本运行期间导出的环境变量不会持续存在。要纠正我需要做的所有事情,就是使用source
运行脚本,这将导致导出的环境变量持续存在直到被取消设置:
await
或使用快捷方式
setState()