DEV Community

DoriDoro
DoriDoro

Posted on

Fixing Docker 'Permission Denied' Error on Ubuntu 24.04: A Step-by-Step Guide

Intrduction

When setting up Docker on Ubuntu 24.04, you might encounter a "permission denied" error when running Docker commands without sudo. This issue typically occurs because the user does not have the correct permissions to access the Docker daemon. Even after adding the user to the docker group, the changes may not take effect immediately, leading to frustration when trying to run docker images or other commands.

1. Remove Old Docker Versions (If Any):

sudo apt remove -y docker docker-engine docker.io containerd runc
Enter fullscreen mode Exit fullscreen mode

2. Update Package Lists:

sudo apt update
Enter fullscreen mode Exit fullscreen mode

3. Add Docker's Official GPG Key:

sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
Enter fullscreen mode Exit fullscreen mode

4. Verify Your Ubuntu Version:

lsb_release -cs
Enter fullscreen mode Exit fullscreen mode

This should return noble (for Ubuntu 24.04). If not, check your installation.

4. Set Up Docker's Stable Repository:

echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu noble stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
Enter fullscreen mode Exit fullscreen mode

5. Update Package Index Again:

sudo apt update
Enter fullscreen mode Exit fullscreen mode

6. Install Docker Engine and Related Components:

sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Enter fullscreen mode Exit fullscreen mode

7. Verify Docker Installation:

sudo systemctl status docker
Enter fullscreen mode Exit fullscreen mode

When the Service is working, the terminal should show:

docker.service - Docker Application Container Engine
Enter fullscreen mode Exit fullscreen mode

and further information

End the running Service with:

q
Enter fullscreen mode Exit fullscreen mode

8. Test Docker with a Sample Container:

sudo docker run hello-world
Enter fullscreen mode Exit fullscreen mode

These steps install Docker on your machine.


When I installed Docker on my machine, there was still a message when trying to access all docker images (docker images) on my machine:

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
Enter fullscreen mode Exit fullscreen mode

this message appears when you want to access docker images without sudo permission on your machine.

Solution: Add Your User to the docker Group
1. Check if the docker group exists:

getent group docker
Enter fullscreen mode Exit fullscreen mode

If nothing is returned, create the group manually:

sudo groupadd docker
Enter fullscreen mode Exit fullscreen mode

2. Add Your User to the docker Group:

sudo usermod -aG docker $USER
Enter fullscreen mode Exit fullscreen mode

3. Apply the Changes: Log out and log back in, or restart your machine:

newgrp docker
Enter fullscreen mode Exit fullscreen mode

4. Test Running Docker without sudo:

docker images
Enter fullscreen mode Exit fullscreen mode

When you want to access the docker system without any sudo commands, restart your machine and it is done.


Conclusion

The "permission denied" error is usually resolved by ensuring the user is correctly added to the docker group and restarting the system to apply the changes. If the issue persists, checking and adjusting the permissions of the Docker socket can help. After completing these steps, you should be able to run Docker commands without sudo and manage containers smoothly.

Top comments (0)