bash eval "$(docker-machine env 默认)"
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40038572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
eval "$(docker-machine env default)"
提问by Kimmo Hintikka
I have issues with launching docker with docker-compose.
我在使用 docker-compose 启动 docker 时遇到问题。
When I run docker-compose -f dev.yml build
I following error >
当我运行时出现docker-compose -f dev.yml build
以下错误>
Building postgres
ERROR: Couldn't connect to Docker daemon - you might need to run `docker-machine start default`.
However if I run docker-machine ls
machine is clearly up >
但是,如果我运行docker-machine ls
机器显然已启动 >
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
default - virtualbox Running tcp://192.168.99.100:2376 v1.12.1
I fixed the error by running eval "$(docker-machine env default)"
after which docker-compose -f dev.yml build
completes successfully.
我通过运行来修复错误,eval "$(docker-machine env default)"
然后docker-compose -f dev.yml build
成功完成。
My question why did this work, what actually happens and how do I undo it?
我的问题是为什么这行得通,实际发生了什么以及如何撤消它?
Also is this a safe way to fix this? Right now this just my laptop, but these containers are supposed to hit company servers in near future.
这也是解决此问题的安全方法吗?现在这只是我的笔记本电脑,但这些容器应该会在不久的将来访问公司的服务器。
I am not super fluent with bash but I been always told not to run eval
and especially not to run eval with "
我对 bash 不是很流利,但我总是被告知不要运行eval
,尤其是不要使用“
回答by Elton Stoneman
When you run docker
commands, the CLI connects to the Docker daemon's API, and it's the API that actually does the work. You can manage remote Docker hosts from your local CLI by changing the API connection details, which Docker stores in environment variables on the client where the CLI runs.
当您运行docker
命令时,CLI 会连接到 Docker 守护程序的 API,而实际执行工作的是 API。您可以通过更改 API 连接详细信息从本地 CLI 管理远程 Docker 主机,Docker 将这些详细信息存储在运行 CLI 的客户端上的环境变量中。
With Docker Machine, your Docker engine is running in a VM, which is effectively a remote machine, so your local CLI needs to be configured to connect to it. Docker Machine knows the connection details for the engines it manages, so running docker-machine env default
prints out the details for the default
machine. The output is something like this:
使用 Docker Machine,您的 Docker 引擎在虚拟机中运行,这实际上是一台远程机器,因此需要配置您的本地 CLI 以连接到它。Docker Machine 知道它管理的引擎的连接详细信息,因此运行会docker-machine env default
打印出default
机器的详细信息。输出是这样的:
$ docker-machine env default
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://172.16.62.130:2376"
export DOCKER_CERT_PATH="/Users/elton/.docker/machine/machines/default"
export DOCKER_MACHINE_NAME="default"
Using eval
executes each of those export
commands, instead of just writing them to the console, so it's a quick way of setting up your environment variables.
Using 会eval
执行这些export
命令中的每一个,而不仅仅是将它们写入控制台,因此这是设置环境变量的快速方法。
You can undo it and reset the local environment with docker-machine env --unset
, which gives you the output for unsetting the environment (so the CLI will try to connect to the local Docker Engine).
您可以撤消它并使用 重置本地环境docker-machine env --unset
,这将为您提供取消环境设置的输出(因此 CLI 将尝试连接到本地 Docker 引擎)。
回答by nwinkler
This is indeed the expected way to use Docker on a machine that does not natively support Docker, e.g. on Windows or Mac OS X.
这确实是在本机不支持 Docker 的机器上使用 Docker 的预期方式,例如在 Windows 或 Mac OS X 上。
The Docker documentation includes this step in its description for using Docker Machine here: https://docs.docker.com/machine/get-started/
Docker 文档在其使用 Docker Machine 的描述中包含此步骤:https: //docs.docker.com/machine/get-started/
What this step does (I suggest you also try this yourself):
这一步的作用(我建议你也试试这个):
- Run
docker-machine env default
. - Take the output of that command and execute it in the current shell session.
- 运行
docker-machine env default
。 - 获取该命令的输出并在当前 shell 会话中执行它。
If you run docker-machine env default
yourself, you will see that it simply suggests to set some environment variables, which allow the Docker commands to find the VM running the Docker daemon. Without these variables set, Docker simply does not know how to communicate with the Docker daemon.
如果您docker-machine env default
自己运行,您会看到它只是建议设置一些环境变量,从而允许 Docker 命令找到运行 Docker 守护程序的 VM。如果没有设置这些变量,Docker 根本不知道如何与 Docker 守护进程通信。
In a server environment (Linux), you will not need Docker Machine, since the Linux kernel natively supports running containers. You only need Docker Machine (a small VM running a Linux kernel) on operating systems that don't natively support running containers.
在服务器环境 (Linux) 中,您将不需要 Docker Machine,因为 Linux 内核本身支持运行容器。在原生不支持运行容器的操作系统上,您只需要 Docker Machine(运行 Linux 内核的小型虚拟机)。