简体   繁体   English

在.bashrc中导出DOCKER_HOST会对终端中的同一命令产生不同的结果

[英]exporting DOCKER_HOST in .bashrc produces a different result to the same command in the terminal

Before I can use my docker container (using Boot2Docker on OSX) I always have to remember to enter 之前,我可以用我的docker容器(OSX上使用Boot2Docker)我总是要记住进入

export DOCKER_HOST=tcp://$(boot2docker ip 2>/dev/null):2375

in my terminal, and naturally I often forget this. 在我的终端,自然我经常忘记这一点。

So I figured I'd just add that line to my ~/.bashrc file but when I've done this and check the value of DOCKER_HOST it's tcp://192.168.42.43:4243 instead of tcp://192.168.42.43:2375 . 所以我想我只是将那行添加到我的~/.bashrc文件中,但是当我完成这个并检查DOCKER_HOST的值时,它是tcp://192.168.42.43:4243而不是tcp://192.168.42.43:2375

Breaking it down: 打破它:

  • boot2docker ip => "The VM's Host only interface IP address is: 192.168.59.103" boot2docker ip =>“VM的主机唯一接口IP地址是:192.168.59.103”
  • boot2docker ip 2 => "The VM's Host only interface IP address is: 192.168.59.103" boot2docker ip 2 =>“VM的主机唯一接口IP地址是:192.168.59.103”
  • boot2docker ip 2>/dev/null => "192.168.59.103" (okay I sort of get that but I have no idea how that works, and I have no idea where the :4243 is coming from." boot2docker ip 2>/dev/null =>“192.168.59.103”(好吧我有点理解,但我不知道它是如何工作的,我不知道:4243来自哪里。“

What's actually going on here and why is the port different? 实际上这里发生了什么,为什么端口不同?

If you want your DOCKER_HOST environment variable to be set automatically for every terminal you open, use the "boot2docker shellinit" command. 如果要为打开的每个终端自动设置DOCKER_HOST环境变量,请使用“boot2docker shellinit”命令。 You can add this line to your .bash_profile to take care of business: 您可以将此行添加到.bash_profile以处理业务:

$(boot2docker shellinit)

Unfortunately, this will give you an annoying error message if your boot2docker VM is not running when you open the terminal ("error in run: VM "boot2docker-vm" is not running.") Put this in your .bash_profile instead to suppress the error message: 不幸的是,如果你的boot2docker虚拟机在你打开终端时没有运行,这会给你一个恼人的错误信息(“运行中的错误:VM”boot2docker-vm“没有运行。”)把它放在.bash_profile而不是压制错误信息:

$(boot2docker shellinit 2>/dev/null)

More details at Github Github的更多细节

NOTE: if you're using Docker Machine instead to manage boot2docker, the equivalent command is 注意:如果您使用Docker Machine来管理boot2docker,则等效命令为

eval "$(docker-machine env MACHINE-NAME)"

where MACHINE-NAME is the name of your boot2docker machine. 其中MACHINE-NAME是boot2docker机器的名称。

Something else is setting that environment variable for you. 还有其他东西正在为您设置该环境变量。 Why are you dumping stderr from that command to /dev/null . 为什么要将stderr从该命令转储到/dev/null Is some extra info coming to stderr ? 是什么额外的信息来到stderr

I would do 我会做

export DOCKER_HOST="tcp://$(boot2docker ip 2>/dev/null):2375" ;
echo "Docker Host is set to ${DOCKER_HOST}"

For some debugging as it is set, then query the value at a later stage to see if something else is messing with it. 对于设置的某些调试,然后在稍后阶段查询该值以查看是否有其他东西在弄乱它。

I think the boot2docker's socket command is more useful here, also we can simplify things a bit. 我认为boot2docker的socket命令在这里更有用,我们也可以简化一些事情。

If you've not set this up before just run this in your terminal: 如果您在终端中运行此操作之前未进行此设置:

  echo export DOCKER_HOST=\`boot2docker socket 2\>/dev/null\` >> ~/.bashrc

If you've already been messing with this, just change your line in .bashrc to this: 如果你已经搞乱了这个,只需将.bashrc中的行更改为:

  export DOCKER_HOST=`boot2docker socket 2>/dev/null`

Now to test it has worked open a new terminal and run docker run hello-world . 现在测试它已经开启了一个新的终端并运行docker run hello-world

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

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