On Docker's documentation pages, all example commands are shown without sudo
, like this one:
docker ps
On Ubuntu, the binary is called docker.io
. It also does not work without sudo:
sudo docker.io ps
How can I configure Docker so that I don't need to prefix every Docker command with sudo?
Good news: the new docker (version 19.03 (currently experimental)) will be able to run rootless negating the problems that can occur using a root user. No more messing with elevated permissions, root and anything that might open up your machine when you did not want to.
Video about this from [DockerCon 2019] Hardening Docker daemon with Rootless mode
As of docker 19.3 this is obsolete (and more dangerous than need be):
The docker manual has this to say about it:
Important to read: post-installation steps for Linux (it also links to Docker Daemon Attack Surface details).
Add the docker group if it doesn't already exist:
Add the connected user "$USER" to the docker group. Change the user name to match your preferred user if you do not want to use your current user:
Either do a
newgrp docker
or log out/in to activate the changes to groups.You can use
to check if you can run docker without sudo.
To run docker command without
sudo
, you need to add your user (who has root privileges) to docker group. For this run following command:Now, have the user logout then login again. This solution is well explained here with proper installation process.
The mechanism by which adding a user to group
docker
grants permission to run docker is to get access to the socket of docker at/var/run/docker.sock
. If the filesystem that contains/var/run
has been mounted with ACLs enabled, this can also be achieved via ACLs.I'm only including this for completeness.
In general, I recommend to avoid ACLs whenever a good alternative based on groups is available: It is better if the privileges in a system can be understood by looking at group memberships only. Having to scan the file system for ACL entries in order to understand system privileges is an additional burden for security audits.
Warning 1: This has the same
root
equivalence as adding$USER
to thedocker
group. You can still start a container in a way that hasroot
access to the host filesystem.Warning 2: ACLs are significantly more difficult for security audits than group-based security. Probably avoid ACLs if possible when you can use groups instead, at least in audit-relevant environments.
After creating the docker group and adding my user to it with
... I still had to give the
/var/run/docker.sock
socket and/var/run/docker
directory the proper permissions to make it work:Logout and login again (with that user) then you'll be able to run docker commands without
sudo
:BTW: This was fixed in Ubuntu 21.10 and is only necessary for Ubuntu versions lower than that.
Docker containers need to be ran by a root user. You can add yourself to the
docker
group (e.g. by runningsudo usermod -aG docker $USER
), but this makes it easy for anyone with access to the$USER
account to gain root access to the machine (e.g. by mounting a root volume in a privileged container).A more security-conscious way of running Docker containers as a non-root user would be to use Podman: https://podman.io/. From its website:
Another alternative is Singularity, which is more commonly deployed in HPC environments: https://sylabs.io/.