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:

  1. Open a Terminal: First, open a terminal on your Linux system.

  2. Check If the docker Group Exists: Run the following command to check if the docker group already exists:

    1
    
    cat /etc/group | grep docker

    If it doesn’t exist, you will not see any output from this command.

  3. Create the docker Group (if necessary): If the docker group doesn’t exist, you can create it using the following command:

    1
    
    sudo groupadd docker
  4. Add the User to the docker Group: To add the user john to the docker group, use the usermod command with the -aG option:

    1
    
    sudo usermod -aG docker john

    This command appends (-a) the user john to the docker group (-G).

  5. Verify the User’s Group Membership: To confirm that the user john has been added to the docker group, you can use the id command:

    1
    
    id john

    You should see docker listed among the user’s groups.

  6. 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.

  7. Test Docker Access: After logging back in, you can test if john can run Docker commands without sudo. 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.

0%