Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

docker

zsh

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.

like image 852
Andrew David Tanner Avatar asked Nov 13 '17 20:11

Andrew David Tanner


3 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
like image 96
Charles Duffy Avatar answered Nov 05 '22 03:11

Charles Duffy


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
like image 5
Raven Avatar answered Nov 05 '22 02:11

Raven


I too was having the exact same problem as posted and have spent the better part of the morning googling for an answer. I went back through the documentation and realised that I completely omitted the post-installation steps for Linux.

https://docs.docker.com/install/linux/linux-postinstall/

I followed the instructions laid out in the section labelled Manage Docker as a non-root user and eval $(sudo docker-machine env myvm1) and the subsequent docker-machine ls worked as expected. In addition... it eliminates the need to prefix all your docker commands withsudo.

I should have RTFM I guess?

like image 3
col Avatar answered Nov 05 '22 02:11

col