De-sudoing Docker

Forgetting to add sudo before docker commands is frustrating. Here is how to fix that

The docker whale with "Making Non-root Docker a Thing" text superimposed over a gradient

Pretty short guide, but I ALWAYS for get to do this when setting up a new server.

To run Docker without using sudo, you need to add your user to the Docker group. This allows you to execute Docker commands without needing administrative privileges. Here’s how you can set it up:

  1. Create the Docker group (if it doesn't already exist):
    sudo groupadd docker
  2. Add your user to the Docker group:
    sudo usermod -aG docker $USER

    Replace $USER with your username if you're scripting this for another user.
  3. Apply the group membership:
  • You can either log out and log back in, or you can use the following command to refresh the group membership without logging out:
    newgrp docker
  1. Verify that you can run Docker without sudo:
    docker run hello-world
    This command should run without any sudo and without errors if everything is set up correctly.

Important Notes:

  • Adding a user to the Docker group grants them equivalent to root privileges. Ensure you trust the user with such permissions.
  • If you still encounter issues, restart the Docker service and try again:bashCopy codesudo systemctl restart docker