SoFunction
Updated on 2025-03-09

Three ways to configure access by docker

🌕Method 1: All users can use docker

🌙Create a docker user group

sudo groupadd docker

🌙Add the current user to the docker user group

sudo usermod -aG docker $USER

🌙Put a user into the docker user group separately

sudo usermod -aG docker username

🌙Update to activate docker user group

newgrp docker

🌙Verification does not require sudo to execute docker command

docker run hello-world

🌙If the above command does not work, restart the Docker service:

In some cases, you may need to restart the Docker service for the changes to take effect. You can use the following command:

sudo systemctl restart docker

Check group members: You can use the following command to check which users belong to the docker group:

getent group docker

🌕Method 2: All users can use docker (requires restart)

🌙Add all users who are docker

sudo usermod -aG docker $USER
reboot

🌙Restart the docker service

systemctl start docker
sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
service docker restart

🌙Test whether ordinary users can use docker

docker run hello-world

🌙Why do you need sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common

sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common This command is not a necessary step to directly allow ordinary users to use Docker. However, some packages in this command may be helpful in setting up or managing a Docker environment, depending on your goal and system configuration.

Let's take a look at the functions of these packages one by one:

apt-transport-https: This package allows apt (APT package management tool) to download packages through the HTTPS protocol. This is necessary to install packages from repository using HTTPS. If your Docker installation source or any Docker-related repository is provided via HTTPS, it is necessary to install apt-transport-https.

ca-certificates: This package contains Mozilla's CA (Certificate Authority) certificate bundle to verify the legitimacy of SSL/TLS certificates. This is essential to ensure secure communication over HTTPS.

curl: curl is a command line tool used to transfer data from a server. It supports a variety of protocols, including HTTP, HTTPS, FTP, etc. While Docker itself does not require curl to run, curl can be very useful when managing and configuring Docker (for example, downloading Docker configuration files or scripts from a remote server).

software-properties-common: This package provides the add-apt-repository command, which allows users to add new APT repository. While this is often used to add third-party repositories, it has little direct relationship with Docker. However, if you plan to add Docker's official repository or any third-party Docker repository, installing software-properties-common will be useful.

🌕Method 3: Wenxin's method

On Ubuntu, by default, the Docker daemon (daemon) runs as root and only root users or users with sudo permissions can run Docker commands directly. However, you can grant Docker permissions to normal users by following the steps below without using sudo every time.

🌙Step 1: Create a docker user group

During the Docker installation process, a user group named docker is usually created. If the group does not exist, you can create it manually:

sudo groupadd docker

🌙Step 2: Add the user to the docker group

Add your normal user to the docker group. Assuming your username is your_username, you can use the following command:

Specify a person to use docker separately

sudo usermod -aG docker your_username

Specify that all users can use docker

sudo usermod -aG docker $USER

🌙Step 3: Log in again

In order for group membership to take effect, you need to log in again. You can log out and log in again, or restart the system. If you don't want to log in again, you can temporarily switch to the new group using the newgrp command:

newgrp docker

🌙Step 4: Verify permissions

After logging in again, you can verify that Docker permissions have been successfully obtained. Run the following command without using sudo:

docker run hello-world

If everything works fine, you should see Docker's Hello World message, which indicates that you can now run Docker commands without sudo permissions.

🌙Restart Docker service:

In some cases, you may need to restart the Docker service for the changes to take effect. You can use the following command:

sudo systemctl restart docker

Check group members: You can use the following command to check which users belong to the docker group:

getent group docker

Through these steps, you can allow ordinary users to access and run Docker commands without sudo permissions.

This is the end of this article about the three methods of docker configuring access to ordinary users. For more related docker configuring access to ordinary users, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!