179

I'm suddenly having issues after an update of Ubuntu 18.04: previously I've used docker without issue on the system, but suddenly I cannot. As far as I can tell, the permissions look correct:

$ docker run hello-world
docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.35/containers/create: dial unix /var/run/docker.sock: connect: permission denied.
See 'docker run --help'.
$ ls -last /var/run/docker.sock 
0 srw-rw---- 1 root docker 0 Jul 14 09:10 /var/run/docker.sock
$ whoami
brandon
$ cat /etc/group | grep docker
docker:x:995:brandon
nvidia-docker:x:994:

EDIT:

Group information:

$ groups
brandon
$ groups brandon
brandon : brandon adm cdrom sudo dip plugdev games lpadmin sambashare docker
$ whoami
brandon

Update

Since the original post where I upgraded a system from 17.04 to 18.04, I've done two upgrades from 16.04 to 18.04, and neither of the later systems had the issue. So it might be something to do with the 17.04 to 18.04 upgrade process. I've yet to perform a fresh 18.04 installation.

bbarker
  • 11,636
  • 9
  • 38
  • 62
  • 2
    Does it work if you run `sudo docker run hello-world`? – mviereck Jul 14 '18 at 20:17
  • 4
    What happens if you run `newgrp docker` and try again from the same terminal? – BMitch Jul 14 '18 at 23:04
  • @mviereck @BMitch - `sudo` worked, I had tried earlier but with a script wrapping a docker command, so that failed (oops). `newgrp docker` gives me a shell where running the command without `sudo` works. So is something wonky going on with my login shell? – bbarker Jul 15 '18 at 01:08
  • Following up on the `newgrp` lead, the `groups` output seems a bit suspect. – bbarker Jul 15 '18 at 13:46
  • Have you tried completely logging off of your Ubuntu session and logging back on again? – sachav Jul 15 '18 at 14:03
  • Yes, in fact I rebooted the system. – bbarker Jul 15 '18 at 14:04
  • 1
    Can you try using your secondary TTYs (Ctrl-Alt-F3)? – sachav Jul 15 '18 at 14:07
  • In fact, I just confirmed that logging in remotely to the system works: correct `groups` output is shown for the current user. Previously I'd been doing this through XFCE's terminal – bbarker Jul 15 '18 at 14:13
  • 1
    Possible duplicate of [Docker can't connect to docker daemon](https://stackoverflow.com/questions/21871479/docker-cant-connect-to-docker-daemon) – David Maze Jul 15 '18 at 16:55
  • 1
    @DavidMaze - I don't believe so - the `newgrp` suggestion above worked, as did logging into the system via `ssh` – bbarker Jul 15 '18 at 17:40
  • This fixed mine. `sudo chmod 666 /var/run/docker.sock` – nithin_cs Feb 15 '20 at 01:52
  • @bbarker - with Linux Mint 20.1 (based on Ubuntu 20.04), a reboot was required after "apt install docker.io" and adding myself to the "docker" group. New shell after adding myself to the group would not show me in the docker group via command "id". "newgrp docker" worked as a workaround until the reboot. – skitheo May 04 '21 at 23:42

15 Answers15

329
sudo setfacl --modify user:<user name or ID>:rw /var/run/docker.sock

It doesn't require a restart and is more secure than usermod or chown.

as @mirekphd pointed out, the user ID is required when the user name only exists inside the container, but not on the host.

Nahshon paz
  • 4,005
  • 2
  • 21
  • 32
  • Thanks. As it happens, I still have this buggy system, though I'm not sure for how many more months! As it happens, this works. Will check to see if it persist on reboot. – bbarker Feb 03 '19 at 18:17
  • 6
    (It doesn't seem to persist through reboots) – bbarker Feb 03 '19 at 18:32
  • 1
    @bbarker https://unix.stackexchange.com/questions/372244/how-can-i-make-acl-settings-on-run-media-persistent Though on many systems this is persistant – Nahshon paz Feb 04 '19 at 19:43
  • @Tvde1 https://raspberrypi.stackexchange.com/questions/92717/is-there-a-raspbian-package-that-installs-setfacl – Nahshon paz Jun 03 '19 at 11:19
  • 1
    setfacl -m jenkins:docker:rw /var/run/docker.sock setfacl: Option -m: Invalid argument near character 9 – Shahar Hamuzim Rajuan Jun 25 '19 at 11:57
  • 1
    @ShacharHamuzimRajuan you need to run setfacl --modify for a user, e.g: setfacl -m u:docker:rw or u:jenkins:rw . Or per group (g instead of u). see: https://linux.die.net/man/1/setfacl – Nahshon paz Jun 26 '19 at 18:49
  • 2
    Why would I want to use this over adding the user to the docker group? This seems like a short sighted trial and error fix. If there are other permissions necessary the docker group is probably what it is for. Permissions will be adequate for the docker group, and so users that are to use docker should be part of the docker group. Fixing it for only this user and one socket file may lead to further issues now or later on other files. – Kissaki Aug 06 '19 at 08:59
  • 1
    @Kissaki you're right, for robust prod/staging envs you'd want to carefully manage a group rather than a user and a file. This is obviously a dev/test issue where people just want to get on with things, with some user that needs temporary, immediate access. Access control lists are meant to be more precise and secure than groups; in this case this just skips the reboot that usemod requires. It's also more secure than chown, I've seen some clusters crashing with that sudo chmod 777 /var/run/docker.sock trick that somebody posted below – Nahshon paz Aug 13 '19 at 13:10
  • The `Invalid argument near character` error is caused by missing user name (e.g. you used `jenkins` on the host, but such user exists only inside a docker container). To avoid the problem, just use UID instead of user name. – mirekphd Jan 12 '20 at 17:37
  • So basically `sudo setfacl --modify user:$(whoami):rw /var/run/docker.sock` ... why not name it? – 0xC0000022L Oct 04 '20 at 20:06
  • @0xC0000022L as mirekphd mentioned sometimes it's gotta be the user ID – Nahshon paz Oct 18 '20 at 10:07
  • @DanielRch. https://unix.stackexchange.com/questions/372244/how-can-i-make-acl-settings-on-run-media-persistent Though on many systems this is persistant – Nahshon paz Jul 28 '21 at 20:46
  • The chosen correct answer here is more complete for prod ready images: https://stackoverflow.com/questions/53126950/permission-denied-to-docker-daemon-socket-at-unix-var-run-docker-sock?rq=1 – Nahshon paz Dec 05 '22 at 07:52
114

add the user to the docker group.

sudo usermod -aG docker $USER
sudo reboot
Karen Danielyan
  • 1,560
  • 1
  • 9
  • 13
82

Just try to give the right permission to docker.sock file by:

sudo chmod 666 /var/run/docker.sock
Pejman Kheyri
  • 4,044
  • 9
  • 32
  • 39
29

The way to fix it is to run:

sudo addgroup --system docker
sudo adduser $USER docker
newgrp docker

that works for me :)

Jules
  • 1,677
  • 1
  • 19
  • 25
Fahd Rahali
  • 451
  • 4
  • 6
20

Ubuntu 18:04

sudo setfacl --modify user:$USER:rw /var/run/docker.sock
Alex Sandro
  • 301
  • 2
  • 2
9

It looks like a permission issue:

sudo addgroup --system docker
sudo adduser $USER docker
newgrp docker
sudo setfacl -m "g:docker:rw" /var/run/docker.sock

or Simply use this command below, which will fix this issue.

sudo chmod -x /var/run/docker.sock
Praveen Kumar K S
  • 3,024
  • 1
  • 24
  • 31
7

Somehow, i found this page when i have't correct permissons on my docker.sock after my Docker installation. So, if you have the same issue, you can read this:

$ sudo adduser $USER docker does not work because the group is "root" not "docker"

$ ls -l /var/run/docker.sock srw-rw---- 1 root root 0 Jul 11 09:48 /var/run/docker.sock so it should be $ sudo adduser $USER root

from a non-snap installed machine, the group is "docker"

# ls -l /var/run/docker.sock srw-rw---- 1 root docker 0 Jul 3 04:18 /var/run/docker.sock The correct way is, according to docker.help you have to run the followings BEFORE sudo snap install docker

$ sudo addgroup --system docker $ sudo adduser $USER docker $ newgrp docker then the group will be "docker"

$ ls -l /var/run/docker.sock srw-rw---- 1 root docker 0 Jul 11 10:59 /var/run/docker.sock

Source: https://github.com/docker-archive/docker-snap/issues/1 (yes, first issue :D)

The easyest way to fix it is to run:

$ sudo setfacl -m "g:docker:rw" /var/run/docker.sock

And then, as it already metioned, run following commands for your user:

$sudo addgroup --system docker
$sudo adduser $USER docker
$newgrp docker

That's it :) Have fun!

Goldus
  • 109
  • 2
  • 6
6

I did the quick fix and it worked immediately.

sudo chmod 777 /var/run/docker.sock
parth
  • 229
  • 2
  • 1
2

Specific to Ubuntu, there is a known issue with lightdm that removes secondary groups from the user as part of the GUI login. You can follow that issue here: https://bugs.launchpad.net/lightdm/+bug/1781418

You can try switching off of lightdm or apply the workaround mentioned in the bug report:

[Comment out the below lines from /etc/pam.d/lightdm:]

auth optional pam_kwallet.so
auth optional pam_kwallet5.so

Temporary options include logging into your machine with something like an ssh or su -l command, or running the newgrp docker command. These will only affect the current shell and would need to be done again with each new terminal.


Outside of this issue, the general commands to give a user direct access to the docker socket (and therefore root access to the host) are:

sudo usermod -aG docker $(id -un) # you can often use $USER in place of the id command
newgrp docker # affects the current shell, logging out should affect all shells
BMitch
  • 231,797
  • 42
  • 475
  • 450
2

I was able to solve this on my Linux Machine using the below command.

> sudo setfacl --modify user:$USER:rw /var/run/docker.sock

Note: Please checck if you have sudo access. Otherwise this command will fail.

How to check sudo access?

$ whoami
> rahul
$ groups
> useracc
$ groups useracc
> Here you can see sudo and other access details.
rahulnikhare
  • 1,362
  • 1
  • 18
  • 25
2

For ubuntu 20.04

Step1 : Check Ubuntu user

echo $USER

Step2 : give rw permission to docker

sudo setfacl --modify user:<user_name>:rw /var/run/docker.sock

Example

Getting error

enter image description here

Solution

enter image description here

enter image description here

Omkesh Sajjanwar
  • 575
  • 8
  • 13
1

I fixed this issue by using the following command:

sudo chmod -x /var/run/docker.sock
ahmnouira
  • 1,607
  • 13
  • 8
0

Please note: not only group name is important, but apparently also gid of the groups. So if docker group on host system has gid of i.e. 995,

cat /etc/group | grep docker  
docker:x:995:brandon

You must make sure gid of docker group You can do this as a part of a launch script, or simply by using exec and doing it manually:

groupmod -g 995 docker

Hope it helps anyone who comes here, it took me a while to find this answear.

Romeo Kienzler
  • 3,373
  • 3
  • 36
  • 58
Butthead
  • 1
  • 3
0

This issue is resolved by following the process below

  1. Check whether the "docker" group is created or not

    cmd: cat /etc/group | grep docker

    output: docker:x:995

  2. Check the permissions of "/var/run/docker.sock" file

    cmd: ls -l /var/run/docker.sock

    output: rw-rw---- 1 root root 0 Jul 14 09:10 /var/run/docker.sock

  3. add docker group to "/var/run/docker.sock" file

cmd: sudo setfacl -m "g:docker:rw" /var/run/docker.sock

output: rw-rw---- 1 root docker 0 Jul 14 09:10 /var/run/docker.sock

  1. Now it will work, if possible restart the docker service.

  2. To restart the docker service

    cmd: sudo systemctl restart docker

NHol
  • 2,045
  • 14
  • 28
-1

ubuntu 20.x +

sudo usermod -aG docker $USER

exit the current terminal and open another terminal and start using docker, docker compose.

Gajendra D Ambi
  • 3,832
  • 26
  • 30