Skip to content
FR
  1. LuzineDebug
  2. Docker & DevOps
  3. permission denied while trying to connect to the…
Docker & DevOps Beginner ≈ 5 min to fix

Fix "permission denied while trying to connect to the Docker daemon socket"

Error message
permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/containers/json": dial unix /var/run/docker.sock: connect: permission denied
  • 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.24/images/create": dial unix /var/run/docker.sock: connect: permission denied
  • permission denied while trying to connect to the docker API at unix:///var/run/docker.sock
  • ERROR: permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Head "http://%2Fvar%2Frun%2Fdocker.sock/_ping": dial unix /var/run/docker.sock: connect: permission denied
In short

The docker client is not allowed to open the /var/run/docker.sock socket, which belongs to root and the docker group. The usual fix on Linux: add your user to the docker group with sudo usermod -aG docker $USER, then log out and back in.

Be the first to confirm this fix

What this error means

The docker command does not run anything itself: it sends HTTP requests to the dockerd daemon through a Unix socket, usually /var/run/docker.sock. That file has permissions like srw-rw---- root docker: only root and members of the docker group can connect. If your user is not in that group, or your session has not picked up the change yet, the kernel refuses the connection (connect: permission denied). The daemon is running: access to the socket is what is blocked, unlike "Cannot connect to the Docker daemon", which means the daemon is stopped.

Common causes

  1. User not in the docker group: the typical case right after installing Docker Engine on Ubuntu, Debian or Fedora.
  2. Session not refreshed: you ran usermod -aG docker, but a session’s groups are only read at login. The open terminal does not see the new group yet.
  3. CI service user: Jenkins, GitLab Runner or a CI agent runs under an account (jenkins, gitlab-runner) that is not in the docker group, or was not restarted after being added.
  4. Socket mounted into a container: with -v /var/run/docker.sock:/var/run/docker.sock, the container user does not have the same GID as the host’s docker group.
  5. Socket ownership changed: a chown or a Snap install changed the socket’s group.

Step-by-step fix

  1. Check the socket and your groups

    Look at who owns the socket and whether docker appears in your groups. If the socket belongs to the docker group and that group is missing from id -nG, it is a group problem.

    bash
    ls -l /var/run/docker.sock
    id -nG
  2. Add the user to the docker group (Linux)

    This is the official post-install procedure. Warning: the docker group grants root-equivalent rights on the machine (anyone can mount / into a container). Only grant it to trusted accounts.

    bash
    sudo groupadd docker 2>/dev/null || true
    sudo usermod -aG docker $USER
  3. Refresh the session and test

    Log out and back in (or reboot the VM). For the current terminal only, newgrp docker opens a shell with the new group. Then test without sudo.

    bash
    newgrp docker
    docker run --rm hello-world
  4. Restore the socket permissions

    If someone changed the socket, do not run chmod 666 or chmod 777: any local user would become root. Give it back to the docker group, or restart the daemon, which recreates the socket with the right permissions.

    bash
    sudo chown root:docker /var/run/docker.sock
    sudo systemctl restart docker
  5. CI and containers that use the socket

    For Jenkins or GitLab Runner, add the service account to the group and restart the service. In a container that mounts the socket, add the host socket’s GID with --group-add.

    bash
    sudo usermod -aG docker jenkins
    sudo systemctl restart jenkins
    
    docker run --rm \
      -v /var/run/docker.sock:/var/run/docker.sock \
      --group-add "$(stat -c '%g' /var/run/docker.sock)" \
      docker:cli docker ps
  6. macOS / Windows: check the Docker Desktop context

    Docker Desktop does not use a docker group. If the error shows up, the CLI often points to the wrong socket. List the contexts and select the Docker Desktop one, then check that DOCKER_HOST is not set to a stale value.

    bash
    docker context ls
    docker context use desktop-linux
    echo "$DOCKER_HOST"

Did this fix work for you?

Confirm it in one click: it will rise for the next developers stuck on the same error.

Keep it from coming back

  • Add the usermod -aG docker step to your provisioning scripts (Ansible, cloud-init) for every account that needs Docker.
  • On a shared server, prefer rootless mode (dockerd-rootless-setuptool.sh install): each user gets their own daemon without root rights.
  • Avoid mounting /var/run/docker.sock into containers: it is root access to the host. To build images in CI, tools such as rootless BuildKit or Kaniko avoid that mount.

Frequently asked questions

Why does it work with sudo but not without?

Because root can always open the socket. Without sudo, your user must belong to the docker group and the session must have been reopened after adding it.

Is chmod 666 /var/run/docker.sock a good fix?

No. Any local user could then drive Docker and gain root access to the machine. Use the docker group or rootless mode.

I added the group but the error remains, why?

The session has not been refreshed: log out completely (closing the terminal is not always enough with a desktop session or multiplexed SSH) or reboot. Check with id -nG.

Useful tools for this error

More Docker & DevOps errors

All Docker & DevOps errors →