
Docker is a powerful tool for deploying applications using lightweight, portable containers. However, one of the most common and frustrating issues users face is the permission denied error. This often happens when you run a Docker command without sudo, for example:
docker ps
Instead of a list of containers, you might get this Docker socket error:
permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock
This error occurs because the Docker daemon runs as a root-owned process and communicates through a Unix domain socket. If the user running the command lacks the necessary permissions to access this socket, Docker commands will be blocked due to permission issues.
In this guide, you’ll learn how to fix the permission denied error in Docker on an Ubuntu-based VPS with clear, step-by-step instructions. Whether you’re a beginner or an experienced developer, you can learn to run Docker commands confidently without needing sudo every time.
Prerequisites
Before you begin, make sure you have:
- Docker installed. Make sure to install Docker on your Ubuntu VPS. Hostinger offers VPS hosting starting at ₹429.00/month with a single-click template to install Docker with Ubuntu.
- Administrative access to the system. You need a user account with sudo privileges to run system-level commands and modify permissions.
How to fix a permission denied error in Docker
1. Verify Docker installation
First, make sure Docker is installed by running the following command:
docker --version
If Docker is installed, you should see an output similar to:
Docker version 28.0.2, build 0442a73
However, if you receive a “command not found” error message, Docker may not be installed. In that case, try reinstalling Docker on your VPS.
2. Add user to the Docker group
By default, Docker runs as a root-owned service. To manage Docker as a non-root user, your user needs to be part of the docker group. This allows you to run Docker commands without needing sudo.
Use the following command to add user to docker group:
sudo usermod -aG docker $USER
This command appends your user to the docker group, allowing permission to access the Docker daemon files and directories.
After adding your user to the group, you must log out and log back in for the new group membership to take effect. Alternatively, you can run the following command to log in to a new group:
newgrp docker
You can check if your user has docker group membership by running:
groups
Once done, try running one of the docker commands:
docker ps
If it lists all the containers without needing root privileges, your user has correct permissions.
3. Adjust file and directory permissions
Sometimes the permission denied error is caused by restricted access to specific files or directories Docker interacts with, such as mounted volumes or configuration directories. Fixing these permissions ensures the Docker engine can read and write where it needs to.
If you’re mounting a volume or working with shared directories, you can adjust file permissions to make sure the current user owns them and has sufficient privileges. Replace /path/to/your/files with the actual path you’re working with:
sudo chown -R $USER:$USER /path/to/your/files
In some cases, you might also need to adjust the permissions:
chmod -R 750 /path/to/your/files
This setup allows the file owner (you) full control, allows your group read-only access with execution rights – which is useful for some setups – and blocks everyone else. It’s a secure and practical default for most use cases.
4. Check Docker socket permissions
The Docker daemon communicates through a Unix socket, typically located at:
/var/run/docker.sock
If your user doesn’t have access to this socket, Docker commands will fail with a permission denied error, even if Docker is correctly installed.
You can run this command to see current ownership and permissions:
ls -l /var/run/docker.sock
You’ll likely see output like:
srw-rw---- 1 root docker 0 2025-04-05 07:55 /var/run/docker.sock
This means only the root user and members of the docker group can access the socket.
It’s technically possible to change the file permissions with:
sudo chmod 666 /var/run/docker.sock
This gives all users on the system unrestricted access to Docker, which is a major security risk. It’s strongly discouraged on production systems or shared environments. Instead of changing socket permissions, the recommended approach is to add your user to the docker group. This maintains security while still allowing you to run Docker without sudo.
5. Restart Docker and verify
After adjusting user groups or permissions, you’ll often need to restart the Docker service and login again for the changes to take effect.
You can restart Docker using system control command:
sudo systemctl restart docker
If you’re using an older system that doesn’t support systemctl, try using service command:
sudo service docker restart
Now test if everything is working by running a new container using a docker run command:
docker run hello-world
This command downloads and runs a test Docker container. If it runs without getting a permission denied error, your setup is working correctly, and you’ll be able to run commands without needing to use sudo docker.
Conclusion
Encountering a permission denied error when using Docker on an Ubuntu VPS is a common but fixable issue that has multiple methods. By following a few essential steps, you can resolve this and continue working without needing sudo every time.
Here’s a quick recap of the steps:
- Verify that Docker is installed and running correctly.
- Add your user to the docker group with the sudo usermod -aG docker $USER command.
- Resolve permission issues by setting appropriate permissions for files and directories used by Docker.
- Check the Docker socket’s permissions.
- Restart Docker and run a simple example container to confirm success.
If you’ve followed each step carefully, your Docker setup should now work smoothly. If problems persist and you still encounter a permission denied error when trying to run a container without sudo, consider revisiting the steps or reaching out to your system administrator. Sometimes a fresh perspective can spot what’s missing.
Docker permission denied FAQ
Why does the permission denied error occur in Docker?
This error typically occurs when a non-root user tries to access the Docker daemon without proper Docker group permissions. For example, running a container without proper access to Docker resources can trigger a permission denied message.
What are the risks of modifying Docker socket permissions?
Changing socket permissions to be too permissive (e.g., 777) can allow unauthorized users to control Docker, posing a serious security risk, including potential container escape or system access.
Do I need to restart my VPS after fixing the error?
Restarting the VPS isn’t required, but you must log out and back in for group membership changes to take effect. Restarting the Docker service may also be necessary for permission changes to apply.
Apply for Premium Hosting
Source Credit: https://www.hostinger.in/tutorials/how-to-fix-docker-permission-denied-error