简体   繁体   English

boot2docker命令可在Shell上运行,但不能在脚本中使用

[英]boot2docker command works on shell, but not in script

New to docker here. 这里是docker的新手。 I have a series of commands which, if fire them off on the shell, work just fine, but if I put them in a script, don't. 我有一系列命令,如果将它们在shell上解雇了,就可以正常工作,但是如果我将它们放入脚本中,则不会。

boot2docker destroy
boot2docker init
boot2docker start
boot2docker ssh &
host=$(boot2docker ip 2> /dev/null)
# everything works fine up to here
ssh -i $HOME/.ssh/id_boot2docker -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" docker@$host docker run --net=host my-image

If I don't try to run a command via ssh, everything works. 如果我不尝试通过ssh运行命令,则一切正常。 Viz: 即:

ssh -i $HOME/.ssh/id_boot2docker -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" docker@$host

This brings up the docker ssh prompt. 这会弹出docker ssh提示。 But if I do run the command via the script (and this is what I actually need to do) I get the error message: 但是,如果我确实通过脚本运行命令(这实际上是我要做的),则会收到错误消息:

level="fatal" msg="Post http:///var/run/docker.sock/v1.16/containers/create: dial unix /var/run/docker.sock: no such file or directory. Are you trying to connect to a TLS-enabled daemon without TLS?"

Again, if I just enter that last command, or the whole litany of commands, into the shell, no problems. 同样,如果我仅将最后一个命令或整个命令序列输入到shell中,就没有问题。 How can I make this script work? 如何使该脚本起作用?

Thanks 谢谢

update If I put that last line in its own script, and run the two scripts in sequence from the command line, everything is fine (same as just typing all the commands in sequence.) If I chain the scripts, or create a third to run them in sequence, I get the error. 更新如果我把它自己的脚本最后一行,并运行在命令行序列中的两个剧本,一切都很好(相同只是顺序输入的所有命令。)如果我链上的脚本,或者创建一个三分之一按顺序运行它们,我得到了错误。 What am I to make of this? 我要怎么做?

Thanks 谢谢

host probably isn't defined when you try to use it. 尝试使用host可能未定义host You can probably confirm that by echo ing it's value before running ssh . 您可能可以通过在运行ssh之前echo它的值来确认这一点。 Easiest solution would be to put these two lines together in the same file: 最简单的解决方案是将这两行放在同一文件中:

host=$(boot2docker ip 2> /dev/null)
ssh -i $HOME/.ssh/id_boot2docker -o "StrictHostKeyChecking no" -o "UserKnownHostsFile /dev/null" docker@$host docker run --net=host my-image

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM