Adding a Non-Root User to Execute Docker Commands
In Docker, it’s common to want non-root users to execute Docker commands without needing to use sudo
each time. This is achieved by adding the user to the docker
group. Here are the steps to do that:
-
Open a Terminal: First, open a terminal on your Linux system.
-
Check If the
docker
Group Exists: Run the following command to check if thedocker
group already exists:1
cat /etc/group | grep docker
If it doesn’t exist, you will not see any output from this command.
-
Create the
docker
Group (if necessary): If thedocker
group doesn’t exist, you can create it using the following command:1
sudo groupadd docker
-
Add the User to the
docker
Group: To add the userjohn
to thedocker
group, use theusermod
command with the-aG
option:1
sudo usermod -aG docker john
This command appends (
-a
) the userjohn
to thedocker
group (-G
). -
Verify the User’s Group Membership: To confirm that the user
john
has been added to thedocker
group, you can use theid
command:1
id john
You should see
docker
listed among the user’s groups. -
Log Out and Log Back In: For the changes to take effect, it’s recommended to log out and log back in as the user
john
. This ensures that the group membership is updated. -
Test Docker Access: After logging back in, you can test if
john
can run Docker commands withoutsudo
. For example:1
docker --version
If you see the Docker version information without any permission errors, then
john
now has the necessary permissions to use Docker without sudo.
Remember that allowing a user to run Docker commands without sudo
means they have significant control over the system, so be cautious when granting this privilege. It’s essential to trust the user and follow best security practices when managing Docker access.