eval $(docker-machine env myvm1) does not switch to shell to talk to myvm1

Viewed 3775

Folks,

I'm following the Docker tutorial here: https://docs.docker.com/get-started/part4/#configure-a-docker-machine-shell-to-the-swarm-manager and coming up against resistance when running this particular command:

eval $(docker-machine env myvm1)

I'm actually running (as above but with addition of sudo).

eval $(sudo docker-machine env myvm1)

I get no output from the command line to tell me anything has been done and when I run:

sudo docker-machine ls

I see that myvm1 does not have an active state as expected. I do know that this step isn't necessary but I'd like to understand why the command is not working and try to fix it.

I am running docker 17.09.0-ce

On Ubuntu 16.04 LTS

zsh shell (have tried switching to bash)

This is just on my local machine by the way, not a server.

Any help would be much appreciated.

4 Answers

There's less to go wrong if you run the eval on the far side of sudo:

sudo sh -c 'eval "$(docker-machine env myvm1)"; docker-machine ls'

Otherwise, the environment variables set by evaling the output of docker-machine env aren't necessarily (barring some very specific /etc/sudoers configuration) propagated through to the future docker-machine invocation.


If you wanted to automate this with a shell function, that can be done:

# docker-env sudo; usage: desudo vm-name command-to-run
desudo() {
  local cmd1 cmd2
  printf -v cmd1 'eval "$(docker-machine env %q)"' "$1"; shift
  printf -v cmd2 '%q ' "$@"
  sudo bash -c "${cmd1} && exec ${cmd2}"
}

...used as:

desudo vm1 docker-machine ls

You should run eval $(docker-machine env myvm1). In fact, you don't have to add sudo.

But you may doesn't have permission to run docker without sudo, here is how to solve this issue on Linux.


Following the steps in this article "Post-installation steps for Linux"

  1. Create the docker group. sudo groupadd docker
  2. Add your user to the docker group. sudo usermod -aG docker $USER
  3. Log out and log back in so that your group membership is re-evaluated.
  4. Verify that you can run docker commands without sudo.docker run hello-world.

If you see the following error:

WARNING: Error loading config file: /home/user/.docker/config.json -
stat /home/user/.docker/config.json: permission denied

Fix it with:

$ sudo chown "$USER":"$USER" /home/"$USER"/.docker -R
$ sudo chmod g+rwx "/home/$USER/.docker" -R

I'm actually running (as above but with addition of sudo).

eval $(sudo docker-machine env myvm1)

I get no output from the command line to tell me anything has been done and when I run:

sudo docker-machine ls I see that myvm1 does not have an active state as expected.

run this command it will work

sudo sh -c 'eval "$(docker-machine env myvm1)"; docker-machine ls'

Related