ServerHandbook live check: run log ------------------------------------------------------------------------------ Article https://serverhandbook.com/kb/docker-permission-denied-docker-sock Check scripts/live/checks/docker-permission-denied-docker-sock.mjs (sha256 7b77190fc6290eda8a08364641df3d61b46996eb79e694b81de29f871b92ef09) Ran at 2026-11-15T06:01:00Z (153 s) Result PASS, all 21 claims held Tested on System Ubuntu 24.04.5 LTS Kernel 6.8.0-147-generic Package docker-ce 5:29.9.3-1~ubuntu.24.04~noble Package docker-ce-cli 5:29.9.3-1~ubuntu.24.04~noble Package containerd.io 2.4.2-4~ubuntu.24.04~noble Setup A fresh Ubuntu 24.04 server from Ubuntu’s cloud image with Docker Engine installed from Docker’s apt repository, as the install article leaves it, reached over SSH as the sudo user deploy. A second, ordinary user (intern) shows what a world-writable socket allows. Every version above was read from the server at run time. Passwords used on the throwaway test server are shown as [password]. How we test: https://serverhandbook.com/how-we-test ------------------------------------------------------------------------------ Claims (21), each with what the run saw ------------------------------------------------------------------------------ 1. ok a bare Ubuntu 24.04 server box (web-server) starts 2. ok docker ps as the sudo user: permission denied while trying to connect to the docker API at unix:///var/run/docker.sock | permission denied while trying to connect to the docker API at unix:///var/run/docker.sock 3. ok the socket is srw-rw---- root docker | srw-rw---- 1 root docker 0 Nov 15 06:02 /var/run/docker.sock 4. ok id: deploy is in sudo but not in docker | uid=1000(deploy) gid=1000(deploy) groups=1000(deploy),27(sudo),100(users) 5. ok sudo docker ps works: the daemon is fine, only access is missing | CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6. ok right after usermod, in the same login: still permission denied | permission denied while trying to connect to the docker API at unix:///var/run/docker.sock 7. ok after logging in again: id -nG lists docker | deploy sudo users docker 8. ok after logging in again: docker ps works without sudo | CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9. ok docker run --rm hello-world works without sudo | Unable to find image 'hello-world:latest' locally | latest: Pulling from library/hello-world | | 4f55086f7dd0: Pull complete | Digest: sha256:cf1b20a0c89a666b6950fecda7ccbc1b1a86520e7c53f908f3a21e79d0d60458 | Status: Downloaded newer image for hello-world:latest | | Hello from Docker! | This message shows that your installation appears to be working correctly. | | To generate this message, Docker took the following steps: | 1. The Docker client contacted the Docker daemon. | 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. | (amd64) | 3. The Docker daemon created a new container from that image which runs the | executable that produces the output you are currently reading. | 4. The Docker daemon streamed that output to the Docker client, which sent it | to your terminal. | | To try something more ambitious, you can run an Ubuntu container with: | $ docker run -it ubuntu bash | | Share images, automate workflows, and more with a free Docker ID: | https://hub.docker.com/ | | For more examples and ideas, visit: | https://docs.docker.com/get-started/ 10. ok deploy cannot list /root (Permission denied) | ls: cannot open directory '/root': Permission denied 11. ok through docker, without sudo or a password: root's home is listed | Unable to find image 'alpine:latest' locally | latest: Pulling from library/alpine | | 881fc932939e: Pulling fs layer | 86ff264fe568: Downloading [==================================================>] 5.639kB/5.639kB | Digest: sha256:f59abe656b692d901e184be3445e084786602108599d2a51d229c0f5a48c71bf | Status: Downloaded newer image for alpine:latest | total 24 | drwx------ 4 root root 4096 Nov 15 06:01 . | drwxr-xr-x 1 root root 4096 Nov 15 06:03 .. | -rw-r--r-- 1 root root 3106 Apr 22 2024 .bashrc | drwx------ 2 root root 4096 Nov 15 06:01 .cache | -rw-r--r-- 1 root root 161 Apr 22 2024 .profile | drwx------ 2 root root 4096 Nov 15 06:01 .ssh 12. ok intern, an ordinary user, gets permission denied | permission denied while trying to connect to the docker API at unix:///var/run/docker.sock 13. ok after chmod 666: srw-rw-rw- | srw-rw-rw- 1 root docker 0 Nov 15 06:02 /var/run/docker.sock 14. ok now intern gets in too: every user on the server controls Docker | CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 15. ok the server reboots and comes back | 13 16. ok after the reboot the socket is srw-rw---- again: chmod 666 did not last | srw-rw---- 1 root docker 0 Nov 15 06:03 /var/run/docker.sock 17. ok with Docker stopped: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running? | Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running? 18. ok after systemctl start docker it works again | CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 19. ok (tested on) the box reports its OS, kernel and the version of 3 package(s) | PRETTY_NAME="Ubuntu 24.04.5 LTS" | VIRT=none | KERNEL=6.8.0-147-generic | PKG containerd.io 2.4.2-4~ubuntu.24.04~noble | PKG docker-ce 5:29.9.3-1~ubuntu.24.04~noble | PKG docker-ce-cli 5:29.9.3-1~ubuntu.24.04~noble 20. ok undo: gpasswd -d removes deploy from the docker group | Removing user deploy from group docker 21. ok undo: after logging in again, permission denied once more | permission denied while trying to connect to the docker API at unix:///var/run/docker.sock ------------------------------------------------------------------------------ Commands and what they printed (21) ------------------------------------------------------------------------------ $ docker ps permission denied while trying to connect to the docker API at unix:///var/run/docker.sock $ ls -l /var/run/docker.sock srw-rw---- 1 root docker 0 Nov 15 06:02 /var/run/docker.sock $ id uid=1000(deploy) gid=1000(deploy) groups=1000(deploy),27(sudo),100(users) $ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES $ sudo usermod -aG docker $USER $ docker ps permission denied while trying to connect to the docker API at unix:///var/run/docker.sock $ id -nG deploy sudo users docker $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES $ docker run --rm hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 4f55086f7dd0: Pull complete Digest: sha256:cf1b20a0c89a666b6950fecda7ccbc1b1a86520e7c53f908f3a21e79d0d60458 Status: Downloaded newer image for hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/ $ ls -la /root ls: cannot open directory '/root': Permission denied $ docker run --rm -v /root:/host-root alpine ls -la /host-root Unable to find image 'alpine:latest' locally latest: Pulling from library/alpine 881fc932939e: Pulling fs layer 86ff264fe568: Downloading [==================================================>] 5.639kB/5.639kB Digest: sha256:f59abe656b692d901e184be3445e084786602108599d2a51d229c0f5a48c71bf Status: Downloaded newer image for alpine:latest total 24 drwx------ 4 root root 4096 Nov 15 06:01 . drwxr-xr-x 1 root root 4096 Nov 15 06:03 .. -rw-r--r-- 1 root root 3106 Apr 22 2024 .bashrc drwx------ 2 root root 4096 Nov 15 06:01 .cache -rw-r--r-- 1 root root 161 Apr 22 2024 .profile drwx------ 2 root root 4096 Nov 15 06:01 .ssh $ sudo -u intern docker ps permission denied while trying to connect to the docker API at unix:///var/run/docker.sock $ sudo chmod 666 /var/run/docker.sock $ ls -l /var/run/docker.sock srw-rw-rw- 1 root docker 0 Nov 15 06:02 /var/run/docker.sock $ sudo -u intern docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES $ ls -l /var/run/docker.sock srw-rw---- 1 root docker 0 Nov 15 06:03 /var/run/docker.sock $ sudo systemctl stop docker.socket docker.service $ docker ps Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running? $ sudo systemctl start docker $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES $ sudo gpasswd -d $USER docker Removing user deploy from group docker $ docker ps permission denied while trying to connect to the docker API at unix:///var/run/docker.sock ------------------------------------------------------------------------------ The check's source (scripts/live/checks/docker-permission-denied-docker-sock.mjs) As it ran. ------------------------------------------------------------------------------ /** * KB fix: "permission denied while trying to connect to the docker API at unix:///var/run/docker.sock" (Docker 29's * wording; older releases said "the Docker daemon socket"). The error as a sudo user right after the install; why * (the socket is root:docker, mode 660; `id` has no docker group; `sudo docker ps` works); the fix (usermod -aG * docker) and why it does NOT work in the same session (groups are read at login); after a new login it works; what * the docker group really gives (root's files through a bind mount); why `chmod 666` on the socket is wrong (any user * gets in) and does not even last (a reboot puts 660 back); the other error, "Cannot connect to the Docker daemon" * (the service is stopped); undo (gpasswd -d). * * Runs on a fresh server (the local VM box: the reboot needs it). Docker Engine is installed first exactly as the * install article does it (hidden). */ import { rebootBox } from '../box.mjs' import { bareBox, reportBox } from '../lib.mjs' export const meta = {} const INSTALL = [ 'sudo apt-get update -qq', 'sudo install -m 0755 -d /etc/apt/keyrings', 'sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc', 'sudo chmod a+r /etc/apt/keyrings/docker.asc', `printf 'Types: deb\\nURIs: https://download.docker.com/linux/ubuntu\\nSuites: noble\\nComponents: stable\\nArchitectures: amd64\\nSigned-By: /etc/apt/keyrings/docker.asc\\n' | sudo tee /etc/apt/sources.list.d/docker.sources >/dev/null`, 'sudo apt-get update -qq', 'sudo DEBIAN_FRONTEND=noninteractive apt-get install -y -qq docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin >/dev/null', ].join(' && ') const DENIED = /permission denied while trying to connect to the docker API at unix:\/\/\/var\/run\/docker\.sock/ const check = (ctx) => bareBox(ctx, async ({ sudoUser }) => { const { run, L } = sudoUser() // ── hidden: Docker Engine as the install article leaves it, and a second, ordinary user ── L(INSTALL, 900000) L('sudo adduser --disabled-password --gecos "" intern >/dev/null') // ── the error ──────────────────────────────────────────────────────────────────────────── const e = run('docker ps', { id: 'error' }) ctx.check('docker ps as the sudo user: permission denied while trying to connect to the docker API at unix:///var/run/docker.sock', e.exit !== 0 && DENIED.test(e.out), e.out) const ls = run('ls -l /var/run/docker.sock', { id: 'socket' }) ctx.check('the socket is srw-rw---- root docker', /^srw-rw---- 1 root docker 0 .* \/var\/run\/docker\.sock$/m.test(ls.out), ls.out) const id1 = run('id', { id: 'id-before' }) ctx.check('id: deploy is in sudo but not in docker', /\(sudo\)/.test(id1.out) && !/\(docker\)/.test(id1.out), id1.out) const sp = run('sudo docker ps', { id: 'sudo-ps' }) ctx.check('sudo docker ps works: the daemon is fine, only access is missing', sp.exit === 0 && /^CONTAINER ID\s+IMAGE/m.test(sp.out), sp.out) // ── the fix, and the same-session trap ─────────────────────────────────────────────────── const same = run('sudo usermod -aG docker $USER\ndocker ps', { id: 'same-session' }) ctx.check('right after usermod, in the same login: still permission denied', DENIED.test(same.out), same.out) const id2 = run('id -nG', { id: 'id-after' }) ctx.check('after logging in again: id -nG lists docker', /\bdocker\b/.test(id2.out), id2.out) const ok = run('docker ps', { id: 'fixed' }) ctx.check('after logging in again: docker ps works without sudo', ok.exit === 0 && /^CONTAINER ID\s+IMAGE/m.test(ok.out), ok.out) const hw = run('docker run --rm hello-world', { id: 'hello', timeout: 300000 }) ctx.check('docker run --rm hello-world works without sudo', hw.exit === 0 && /Hello from Docker!/.test(hw.out), hw.out) // ── what the group gives ───────────────────────────────────────────────────────────────── const no = run('ls -la /root', { id: 'root-denied' }) ctx.check("deploy cannot list /root (Permission denied)", no.exit !== 0 && /Permission denied/.test(no.out), no.out) const yes = run('docker run --rm -v /root:/host-root alpine ls -la /host-root', { id: 'root-through-docker', timeout: 300000 }) ctx.check("through docker, without sudo or a password: root's home is listed", yes.exit === 0 && /\.bashrc/.test(yes.out) && /\.ssh/.test(yes.out), yes.out) // ── chmod 666 ──────────────────────────────────────────────────────────────────────────── const ie = run('sudo -u intern docker ps', { id: 'intern-denied' }) ctx.check('intern, an ordinary user, gets permission denied', DENIED.test(ie.out), ie.out) run('sudo chmod 666 /var/run/docker.sock', { id: 'chmod' }) const ls2 = run('ls -l /var/run/docker.sock', { id: 'socket-666' }) ctx.check('after chmod 666: srw-rw-rw-', /^srw-rw-rw- 1 root docker/m.test(ls2.out), ls2.out) const ii = run('sudo -u intern docker ps', { id: 'intern-in' }) ctx.check('now intern gets in too: every user on the server controls Docker', ii.exit === 0 && /^CONTAINER ID/m.test(ii.out), ii.out) const secs = rebootBox() ctx.check('the server reboots and comes back', typeof secs === 'number', String(secs)) const ls3 = run('ls -l /var/run/docker.sock', { id: 'socket-after-reboot' }) ctx.check('after the reboot the socket is srw-rw---- again: chmod 666 did not last', /^srw-rw---- 1 root docker/m.test(ls3.out), ls3.out) // ── the other error ────────────────────────────────────────────────────────────────────── run('sudo systemctl stop docker.socket docker.service', { id: 'stop' }) const down = run('docker ps', { id: 'daemon-down' }) ctx.check('with Docker stopped: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?', down.exit !== 0 && /Cannot connect to the Docker daemon at unix:\/\/\/var\/run\/docker\.sock\. Is the docker daemon running\?/.test(down.out), down.out) run('sudo systemctl start docker', { id: 'start' }) const up = run('docker ps', { id: 'daemon-up' }) ctx.check('after systemctl start docker it works again', up.exit === 0 && /^CONTAINER ID/m.test(up.out), up.out) reportBox( ctx, L, 'A fresh Ubuntu 24.04 server from Ubuntu’s cloud image with Docker Engine installed from Docker’s apt repository, as the install article leaves it, reached over SSH as the sudo user deploy. A second, ordinary user (intern) shows what a world-writable socket allows.', ['docker-ce', 'docker-ce-cli', 'containerd.io'], ) // ── undo ───────────────────────────────────────────────────────────────────────────────── const gd = run('sudo gpasswd -d $USER docker', { id: 'undo' }) ctx.check('undo: gpasswd -d removes deploy from the docker group', /Removing user deploy from group docker/.test(gd.out), gd.out) const back = run('docker ps', { id: 'undo-check' }) ctx.check('undo: after logging in again, permission denied once more', DENIED.test(back.out), back.out) }) export default check