How to Run Docker Without Sudo
Run Docker commands without sudo
On a fresh installation of Docker, you may be required to use sudo to run Docker commands.
For instance, trying to run a GPU Docker image without sudo will result in an error:
$ docker run --rm --runtime=nvidia --gpus all nvidia/cuda:11.6.2-base-ubuntu20.04 nvidia-smi
docker: 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/containers/create": dial unix /var/run/docker.sock: connect: permission denied.
See 'docker run --help'.
The error occurs because you lack permission to access the Docker daemon. Docker restricts access by default to root users. Follow these steps to run Docker without sudo.
1. Create the docker group
Create the Docker user group if it does not already exist:
$ sudo groupadd docker
2. Add your user to the docker group
Add your current user to the docker group:
$ sudo gpasswd -a $USER docker
IMPORTANT: Log out and log back in (or restart your machine) so that your group membership is re-evaluated and the changes take effect.
3. Restart the docker daemon
Restart the Docker service:
$ sudo service docker restart
Troubleshooting: Still getting permission denied?
If you installed and ran Docker as root previously, and you are still receiving the “permission denied” error after adding your user to the group, the issue may lie with the ownership of the Docker socket file. You can fix this by changing the ownership:
sudo chown $USER /var/run/docker.sock
References:
https://docs.docker.com/engine/install/linux-postinstall/
https://docs.docker.com/engine/security/rootless/
https://raw.githubusercontent.com/sindresorhus/guides/main/docker-without-sudo.md
https://stackoverflow.com/questions/48568172/docker-sock-permission-denied