Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inside tmux, docker commands won't work without sudo

Tags:

docker

ubuntu

I am trying to run docker without sudo on ubuntu 16.04. I followed the Linux post-installation instructions on the docker website:

sudo groupadd docker
sudo usermod -aG docker $USER

I rebooted and then ran

docker ps

this error still appears:

Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.37/containers/json: dial unix /var/run/docker.sock: connect: permission denied

the weird thing is if i ssh from my machine to itself the command is executed correctly. when i run the command from tty it also works correctly.

edit:

output of ls -lah /var/run/docker.sock:

srw-rw---- 1 root docker 0 Aug  9 11:22 /var/run/docker.sock

output of id:

uid=1000(uname) gid=1000(uname) groups=1000(uname),4(adm),24(cdrom),27(sudo),29(audio),30(dip),46(plugdev),113(lpadmin),128(sambashare),999(docker)

what could have gone wrong? thanks for the help!

like image 280
unameuname Avatar asked Aug 09 '18 11:08

unameuname


People also ask

Why do I have to run Docker with sudo?

1. Introduction. Running Docker commands with sudo ensures that the Docker command is executed with the security rights of root (by using sudo) or by a user who is a member of the docker group.


2 Answers

So apparently the problem was the tmux shell.

The problem occurs when an account is added to the docker group, while still having at least one tmux session open. closing all tmux sessions solves the problem [the group accounts will now be updated in the next session].

solution:

  1. kill tmux:

pkill -f tmux

  1. open tmux and test docker:

docker run hello-world

now it works fine

like image 56
unameuname Avatar answered Sep 22 '22 08:09

unameuname


If you initially ran Docker CLI commands using sudo before adding your user to the docker group you may get this error the you need to remove the ~/.docker/ directory (it is recreated automatically, but any custom settings are lost), or change its ownership and permissions using the following commands:

$ sudo chown "$USER":"$USER" /home/"$USER"/.docker -R
$ sudo chmod g+rwx "/home/$USER/.docker" -R
like image 35
kunal Avatar answered Sep 19 '22 08:09

kunal