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
dockerGroup Exists: Run the following command to check if thedockergroup already exists:1cat /etc/group | grep dockerIf it doesn’t exist, you will not see any output from this command.
-
Create the
dockerGroup (if necessary): If thedockergroup doesn’t exist, you can create it using the following command:1sudo groupadd docker -
Add the User to the
dockerGroup: To add the userjohnto thedockergroup, use theusermodcommand with the-aGoption:1sudo usermod -aG docker johnThis command appends (
-a) the userjohnto thedockergroup (-G). -
Verify the User’s Group Membership: To confirm that the user
johnhas been added to thedockergroup, you can use theidcommand:1id johnYou should see
dockerlisted 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
johncan run Docker commands withoutsudo. For example:1docker --versionIf you see the Docker version information without any permission errors, then
johnnow 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.