By default, docker commands should already work without sudo command after fresh installation. However, this is not always the case. If you have a similar issue, you will need to create a new user group on Linux and assign to it your user. Run the fallowing commands:
To create the docker
group and add your user:
Step 1: Create the docker group:
sudo groupadd docker
Step 2: Add your user to the docker group:
sudo usermod -aG docker $USER
Step 3: Log out and log back in so that your group membership is re-evaluated. If testing on a virtual machine, it may be necessary to restart the virtual machine for changes to take effect.On a desktop Linux environment such as X Windows, log out of your session completely and then log back in.On Linux, you can also run the following command to activate the changes to groups:
newgrp docker
Verify that you can run docker
commands without
sudo
: docker run hello-world
This command downloads a test image and runs it in a container. When the container runs, it prints a message and exits.
If you initially ran Docker CLI commands using sudo
before adding your user to the docker
group, you may see the following error, which indicates that your ~/.docker/
directory was created with incorrect permissions due to the sudo
commands.
WARNING: Error loading config file: /home/user/.docker/config.json -
stat /home/user/.docker/config.json: permission denied
To fix this problem, either remove the ~/.docker/
directory (it is recreated automatically, but any custom settings are lost), or change its ownership and permissions using the following commands:
$ sudo chown "$USER":"$USER" /home/"$USER"/.docker -R
$ sudo chmod g+rwx "$HOME/.docker" -R
Read more here:
Post-installation steps for Linux
Install Docker Desktop on Linux distro