重置Docker Machine以在我的本地计算机上运行Docker命令 [英] Reset Docker Machine to run Docker commands on my local machine

查看:53
本文介绍了重置Docker Machine以在我的本地计算机上运行Docker命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在学习docker,遇到了一些令人困惑的事情.我开始使用 docker-machine ,这对于创建本地VM和在Digital Ocean上旋转服务器非常有用.但是,我现在想回到在本地计算机上执行docker命令的状态,以便我可以添加功能,构建和推送docker存储库.

I'm learning docker right now and I have run into something that's confusing. I started using docker-machine, which was great for creating a local VM and spinning up a server on Digital Ocean. However, I now want to return to a state where the docker commands execue on my local machine so that I can add features, build, and push my docker repo.

我知道如何使用 docker-machine env 切换到其他计算机,但是如何回到没有活动计算机的状态?如果我重新启动Docker,则会发生这种情况,但我不想一直都在重新启动docker.似乎应该为此使用命令?

I know how to change to other machines using docker-machine env, but how do I go back to having no active machine? If I restart docker this happens, but I don't want to be restarting docker all of the time. It seems like there should be a command for this?

为了澄清,我没有尝试连接到我的 default 机器.我希望Docker命令像在我设置Docker Machine之前一样在我的计算机上执行.

And just to clarify, I am not trying to connect to my default machine. I want the docker commands to execute on my machine like they did before I set up Docker Machine.

推荐答案

执行 eval $(docker-machine env DOCKER_MACHINE_NAME)只会设置一堆环境变量,即:

Doing eval $(docker-machine env DOCKER_MACHINE_NAME) will just set a bunch of environment variables, namely:

  • DOCKER_TLS_VERIFY ="1"
  • DOCKER_HOST ="tcp://X.Y.Z.W:2376"
  • DOCKER_CERT_PATH ="/machine/machines/DOCKER_MACHINE_NAME"
  • DOCKER_MACHINE_NAME ="DOCKER_MACHINE_NAME"

,然后在使用这些设置运行"docker"时,它将识别出它们并连接到远程计算机.

and when you then run "docker" with these set, it will recognize them and connect to the remote machine.

因此,您需要做的是取消它们的设置,或者只是退出当前的Shell会话.

So what you need to do, is unset them, or just exit your current shell session.

您可能采取的最简单的方法是在调用 docker-machine env 之前启动一个新的Shell,以便仅在其中设置这些变量.

Probably the most simple approach you can take is to start a new shell, before you invoke docker-machine env, so that these variables will only be set inside it.

#start a new shell
bash 

#configure docker to connect to the remote machine
eval $(docker-machine env DOCKER_MACHINE_NAME)

#run some commands against the remote docker machine
docker ps
...

#once, done, return to the parent shell
exit

#run some commands against your local docker daemon
docker ps
...

重置 DOCKER 变量(在您当前的shell中)

或者,您可以重置它们,而无需退出当前的shell,就像这样:

Reset DOCKER variables (in your current shell)

Alternatively, you can just reset them, w/o exiting your current shell, like that:

unset `env|grep DOCKER|cut -d\= -f1`

或使用"--unset"选项设置为"env"(因为@thaJeztah已 指出了已经)

or use the "--unset" option to "env" (as @thaJeztah has pointed out already)

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

这也记录在docker-machine 手册中.

That is also documented in the docker-machine manual.

如果偶尔只需要对远程计算机运行 docker 命令,则可以使用一个简单的包装器脚本,如下所示:

If you only need to run docker commands against the remote machines occasionally, you can use a simple wrapper script like this:

#!/bin/bash
DOCKER_MACHINE=$1
shift

if [ -z "$DOCKER_MACHINE" ] 
then
    echo "Usage: rdocker DOCKER_MACHINE COMMAND"
    exit 1
fi

eval $(docker-machine env "$DOCKER_MACHINE")
docker "${@}"

然后可以像这样调用:

./rdocker my_machine ps

获取在 my_machine 上运行的容器的列表(或执行任何其他 Docker 命令),而不会干扰您当前的shell会话.

to get the list of containers running on my_machine (or execute any other Docker command), w/o disturbing your current shell session.

边注

要检查当前处于活动状态的 DOCKER _ 变量集,只需执行以下操作:

To check the currently active set of DOCKER_ variables, just do:

env|grep DOCKER

这篇关于重置Docker Machine以在我的本地计算机上运行Docker命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆