Adding user to the docker group
How To

Add User to docker Group


Table of Contents

So, you have installed Docker on Ubuntu successfully. You can run the Docker commands but you have to add sudo with them all the time.

Otherwise, you'll see this "docker: Got permission denied" error:

This is annoying because you may forget to add sudo with docker command and even when you add sudo, you have to enter the password.

The solution to this problem is to add yourself (or the intended user) to docker group. It is configured in a way that members of the docker group have permission to access the sockets and hence they don't need to run docker with sudo.

In this tutorial, learn how you can add a user to the docker group.

Please ensure that the user you are adding to the docker group has sudo access.

Step 1: Create the docker group

First, you need to create the docker group if it doesn't exist already.

Run the command below for that:

sudo groupadd docker

If the group does not exist, it will create one. If the group exists already, it will show an error message "groupadd: group 'docker' already exists'.

Both scenarios are fine for us.

Step 2: Add user to the docker group

Now that you have made sure that the docker group exists, it's time to add the user to docker group.

You need to specify the username:

Make sure that you are using -aG command option with usermod command here.
sudo usermod -aG docker <username>

Replace <username> with the actual username.

If you are doing it for yourself, you can try this command and it will automatically take your username:

sudo usermod -aG docker $USER

How does the command work? The usermod command modifies the attributes of a user. Here, you ask it to append ( -a option) a new group (G) to the existing groups of the user.

The append option is crucial otherwise, it will replace the existing groups of a user with just this new group and that will have catastrophic results.

Step 3: Make the changes take effect

The things here is that the user won't be added to a new group in the current session.

In other words, if you added yourself to docker group, you need to end the current session by logging out of the current terminal and logging back in.

If that is not an option, you can force the change by using this command:

newgrp docker

You may also restart Docker services if required:

sudo systemctl restart docker

In virtual machines, rebooting Ubuntu becomes necessary:

Step 4: Verify by running a docker command

Now is the moment of truth. See if things are working fine and if you can run docker without sudo:

docker run hello-world

If you don't see the "Got permission denied while trying to connect to the Docker daemon socket" error and the command runs properly, congratulations.

Adding the user to docker group has saved you the trouble of typing sudo with each docker command. Enjoy it.