TL;DR: Unlike what many tutorials on the web propose (add your user account to the docker
group, which is risky! see below), you could just add an alias in your .bashrc
to avoid typing sudo
, while having the "password prompt protection".
To be more precise: the Docker daemon socket is owned by root:docker
:
$ ls -l /var/run/docker.sock
srw-rw---- 1 root docker 0 janv. 28 14:23 /var/run/docker.sock
so with this default setup, you need to prepend all docker CLI commands by sudo
.
To avoid this, you can either:
Add $USER
to the docker
group (but that's quite risky to do this on your personal workstation, as this would amount to provide your user account with root permissions without any sudo
password prompt nor auditing).
See also this page in the official Docker documentation:
https://docs.docker.com/engine/security/#docker-daemon-attack-surface
Or, to prepend sudo
automatically but avoid typing sudo docker
manually, a good practice consists in adding the following alias in your ~/.bashrc
file (see e.g. this thread for details):
__docker() {
if [[ "${BASH_SOURCE[*]}" =~ "bash-completion" ]]; then
docker "$@"
else
sudo docker "$@"
fi
}
alias docker=__docker
Then you can test this by opening a new terminal and typing:
$ docker run --rm -it debian:10 # asks your password
$ \docker run --help # does not ask your password thanks to '\'