Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

The Short Answer

Read this first: an account in the docker group can do anything root can do on the server. Add only accounts you would trust with root. Then:

sudo groupadd docker
sudo usermod -aG docker $USER
newgrp docker
docker run hello-world

The Docker packages usually create the group, so groupadd may say it already exists. Keep -a in usermod: without it, the user is removed from every other group, sudo included. newgrp docker applies the change only in the current shell; log out and back in to apply it everywhere. More on groups is in how to add a user to a group in Linux.

What Membership in the docker Group Grants

The Docker daemon runs as root, and anyone who may talk to its socket can tell it what to do. A group member can start a container that mounts the host's root file system and change any file in it: /etc/shadow, the sudoers files, or root's SSH keys. Docker asks for no password and sudo writes no log entry for it. The group is, in effect, root access without a password, and Docker's documentation says the same.

Who Should Not Be in the Group

  • Service accounts: the user your web server or application runs as. If the application is broken into, the attacker gets root.
  • Deploy accounts with narrow rights: a CI or deploy login that should only copy files is no longer narrow once it can run containers.
  • Shared logins: an account several people use, or one that allows password login over SSH.
  • Anyone you would not give sudo: the effect is the same.

Check That It Worked

id
docker ps

If id does not list docker, the shell started before the change. A tmux server keeps the groups it started with, so close it and start a new one; see the tmux cheat sheet. If docker ps still answers with a permission or connection error, follow Cannot connect to the Docker daemon. Never make the socket writable for everyone with chmod 666.

Fix Files Created with sudo Earlier

If you ran sudo docker before, the folder ~/.docker may belong to root, and the client warns that it cannot load its configuration file. Docker's post-install steps give it back to your user:

sudo chown "$USER":"$USER" "$HOME/.docker" -R
sudo chmod g+rwx "$HOME/.docker" -R

Rootless Mode: the Safer Alternative

In rootless mode the daemon and its containers run as an ordinary user inside a user namespace. A process that breaks out of a container gets only that user's rights, not root. Setting it up means installing docker-ce-rootless-extras from Docker's repository and the uidmap package, giving the user at least 65,536 subordinate IDs in /etc/subuid and /etc/subgid, stopping and disabling the system-wide Docker service if it runs, and then running dockerd-rootless-setuptool.sh install as that user, not as root. Follow Docker's rootless documentation for your distribution. The main limits:

  • Low ports: ports below 1024 cannot be published without an extra system setting.
  • Resource limits: CPU, memory, and process limits need cgroup v2 and systemd, and CPU and I/O limits need extra delegation.
  • Networking: it works through a user-space layer and can be slower than the normal mode.
  • Separate data: each user has their own images, containers, and socket.

Remove a User from the Group

sudo gpasswd -d deploy docker

The change applies at the next login; sessions that are open keep the right until they end. Containers the user started keep running.

Or Keep Using sudo

For an administrator account, sudo docker is a valid choice: it asks for the password and sudo logs every use. How to set up such an account is in how to create a sudo user on Ubuntu.

Was this answer helpful?
Loading...