Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset Docker Machine to run Docker commands on my local machine

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.

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?

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.

like image 454
boulderz Avatar asked Jan 16 '17 21:01

boulderz


2 Answers

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"

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

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

Use a new shell session

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
...

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`

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

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

That is also documented in the docker-machine manual.

Use a wrapper script

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 "${@}"

Which can be then invoked like that:

./rdocker my_machine ps

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


Sidenote

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

env|grep DOCKER
like image 58
zeppelin Avatar answered Oct 11 '22 05:10

zeppelin


To unset the environment variables set by docker-machine, follow the instructions printed by the docker-machine env --unset option;

unset DOCKER_TLS_VERIFY
unset DOCKER_HOST
unset DOCKER_CERT_PATH
unset DOCKER_MACHINE_NAME
unset DOCKER_API_VERSION
# Run this command to configure your shell: 
# eval $(docker-machine env --unset)

So running eval $(docker-machine env --unset) should unset all the environment-variables that docker-machine set.

like image 43
thaJeztah Avatar answered Oct 11 '22 05:10

thaJeztah