shell脚本连接到docker守护进程

我想轻松地将我的shell连接到docker,我可以通过input以下命令来实现:

eval "$(docker-machine env docker-vm)" 

我想我会把这一行放在一个简单的shell脚本中。我发现,虽然命令通过在bash提示符处input行来工作,但是如果放置在shell脚本中然后执行脚本,则该行将不起作用。 运行脚本后,我将收到错误: Cannot connect to the Docker daemon. Is the docker daemon running on this host? Cannot connect to the Docker daemon. Is the docker daemon running on this host?

我使用的是oh-my-zsh和iterm2。

这是让机器运行的小脚本:

 #!/usr/bin/env bash docker-machine start docker-vm docker-machine ip docker-vm docker-machine env docker-vm # this line will not "execute" meaning it does not work from the script # this line does work proerly when executed from the command line eval "$(docker-machine env docker-vm)" 

保持简单,注意当运行一个脚本时,它激发一个新的shell实例,并且eval命令在脚本退出时被销毁的那个实例上执行。

所以,你需要在每个会话上运行eval

我build议你注入.bash_profile或类似的命令,所以它试图连接docker机器和docker每次。 如果没有机器连接,则不会,错误显示没有主机正在运行。