The Error

You run docker ps or docker compose up on Linux and get hit with:

permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock 

Why It Happens

The Docker daemon runs as root and binds to a Unix socket owned by the docker group. If your current user account has not been added to that group, the operating system blocks access.

⚡ The Instant Fix (One-Liner)

Add your current user to the docker group and refresh your group membership immediately without logging out:

sudo usermod -aG docker $USER && newgrp docker 

Verify it works without sudo:

docker ps 

If the Docker Service Isn't Running

If you still get socket errors, ensure the Docker engine daemon is actively running:

sudo systemctl enable --now docker 

Check status:

sudo systemctl status docker 

WSL2 (Ubuntu on Windows) Fix

If you are running Docker inside WSL2 without Docker Desktop:

sudo service docker start sudo usermod -aG docker $USER && newgrp docker 

⚠️ What NOT to Do

Never run sudo chmod 777 /var/run/docker.sock. While it works temporarily, it gives every user and malicious process on your machine unrestricted root privileges to your host system.