通过 SSH 将命令传递给 docker 虚拟机
Pass a command to docker vm via SSH
How can I use a single command to send the instructions below into a docker virtualbox vm using ssh?
我想 运行 从虚拟机外部执行一个命令,以便该命令可以嵌入到脚本中。但我似乎只能连接交互式控制台。
具体如下:
可用的虚拟机:
我首先获取可用虚拟机列表,如下所示:
[root@localhost ~]# docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
myvm1 - virtualbox Running tcp://192.168.99.102:2376 v18.04.0-ce
myvm2 - virtualbox Running tcp://192.168.99.100:2376 v18.04.0-ce
抛出的错误:
接下来,我尝试使用本教程中的命令向列出的 VM 之一发送命令,如下所示:
[root@localhost ~]# docker-machine ssh myvm1 “docker swarm init --advertise-addr tcp://192.168.99.102:2377”
sh: “docker: not found
exit status 127
Note that the command throws an error because the docker
command cannot be found when the command is passed into the VM as an argument.
交互式控制台有效
为了隔离问题,我检查以确保如果我手动登录到交互式控制台,相同的命令可以正常工作,如下所示:
[root@localhost ~]# docker-machine ssh myvm1
## .
## ## ## ==
## ## ## ## ## ===
/"""""""""""""""""\___/ ===
~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ / ===- ~~~
\______ o __/
\ \ __/
\____\_______/
_ _ ____ _ _
| |__ ___ ___ | |_|___ \ __| | ___ ___| | _____ _ __
| '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__|
| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__| < __/ |
|_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
Boot2Docker version 18.04.0-ce, build HEAD : b8a34c0 - Wed Apr 11 17:00:55 UTC 2018
Docker version 18.04.0-ce, build 3d479c0
docker@myvm1:~$ docker swarm init --advertise-addr 192.168.99.102:2377
Swarm initialized: current node (gnrj1sbx36j8lf82o9bk6yz58) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-57xhc6p7xpmeicznpfc7i6fhz9p9tdt48m5dqhqj3v4u42217g-50vj218ggzvm1shbuj3ei4brb 192.168.99.102:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
docker@myvm1:~$
Given that the command works when called from INSIDE the VM's console, what specific steps are required in order to get the command to work when passed as an argument from outside the VM?
主机为CentOS 7服务器,以防影响docker机器CLI命令。
未找到的命令是 “docker
而不是 docker
尝试从此处输入或复制:
docker-machine ssh myvm1 "docker swarm init --advertise-addr tcp://192.168.99.102:2377"
当我从某些站点复制和粘贴代码时,我经常遇到这个问题。他们通常使用 “
而不是 "
.
弯引号
在 shell 中(通常用于编码)我们只使用正则引号(双引号 "
或 单引号 '
).
对于写作(例如在 Word 中),引号通常被替换为 弯引号 。但是这种引号的解释方式与常规引号不同(它们是不同的字符)。
“docker ...“
与 "docker ..."
不同
How can I use a single command to send the instructions below into a docker virtualbox vm using ssh?
我想 运行 从虚拟机外部执行一个命令,以便该命令可以嵌入到脚本中。但我似乎只能连接交互式控制台。
具体如下:
可用的虚拟机:
我首先获取可用虚拟机列表,如下所示:
[root@localhost ~]# docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
myvm1 - virtualbox Running tcp://192.168.99.102:2376 v18.04.0-ce
myvm2 - virtualbox Running tcp://192.168.99.100:2376 v18.04.0-ce
抛出的错误:
接下来,我尝试使用本教程中的命令向列出的 VM 之一发送命令,如下所示:
[root@localhost ~]# docker-machine ssh myvm1 “docker swarm init --advertise-addr tcp://192.168.99.102:2377”
sh: “docker: not found
exit status 127
Note that the command throws an error because the
docker
command cannot be found when the command is passed into the VM as an argument.
交互式控制台有效
为了隔离问题,我检查以确保如果我手动登录到交互式控制台,相同的命令可以正常工作,如下所示:
[root@localhost ~]# docker-machine ssh myvm1
## .
## ## ## ==
## ## ## ## ## ===
/"""""""""""""""""\___/ ===
~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ / ===- ~~~
\______ o __/
\ \ __/
\____\_______/
_ _ ____ _ _
| |__ ___ ___ | |_|___ \ __| | ___ ___| | _____ _ __
| '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__|
| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__| < __/ |
|_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
Boot2Docker version 18.04.0-ce, build HEAD : b8a34c0 - Wed Apr 11 17:00:55 UTC 2018
Docker version 18.04.0-ce, build 3d479c0
docker@myvm1:~$ docker swarm init --advertise-addr 192.168.99.102:2377
Swarm initialized: current node (gnrj1sbx36j8lf82o9bk6yz58) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-57xhc6p7xpmeicznpfc7i6fhz9p9tdt48m5dqhqj3v4u42217g-50vj218ggzvm1shbuj3ei4brb 192.168.99.102:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
docker@myvm1:~$
Given that the command works when called from INSIDE the VM's console, what specific steps are required in order to get the command to work when passed as an argument from outside the VM?
主机为CentOS 7服务器,以防影响docker机器CLI命令。
未找到的命令是 “docker
而不是 docker
尝试从此处输入或复制:
docker-machine ssh myvm1 "docker swarm init --advertise-addr tcp://192.168.99.102:2377"
当我从某些站点复制和粘贴代码时,我经常遇到这个问题。他们通常使用 “
而不是 "
.
弯引号
在 shell 中(通常用于编码)我们只使用正则引号(双引号 "
或 单引号 '
).
对于写作(例如在 Word 中),引号通常被替换为 弯引号 。但是这种引号的解释方式与常规引号不同(它们是不同的字符)。
“docker ...“
与 "docker ..."