How to operate Docker as a non-root user on Linux system Ubuntu

  This article introduces the specific method of performing various Docker operations as a non-root user through configuration in the Ubuntu version of the Linux operating system .

  In the article Detailed process of configuring Docker on Ubuntu in Linux system (https://blog.csdn.net/zhebushibiaoshifu/article/details/132612560), we introduced the detailed configuration method of the open source containerization platform and tool set Docker ; after the configuration is completed, Docker can be used normally, but there is still a small problem - when we perform various operations of Docker in the Unix system, because Docker is bound to the Unix socket ( Socket ), and the socket belongs For the root user of the system, if non- root users need to access it, they can only do so through commands; this also leads to the fact that when we run Docker , we must do it as the root user (that is, through commands). This makes us need to enter the root user's password once when executing many Docker- related commands, which causes more trouble. So, can we lift this restriction?sudosudo

  The answer is yes, we can achieve the above requirements by creating a new Unix user group. We need to create a user group, name it docker, and put our current non- root user into this user group; after doing this, when Docker starts, it will create a Unix socket that can dockerbe accessed by members of the user group interface, so that we can use non- root users to perform subsequent operations.

  To complete the above work, the specific operations required are as follows. Among them, all the codes involved in this article can be executed in the terminal.

  First, execute the following code to create dockera user group named. Among them, groupaddis a Linux system command used to create a user group; dockerit is the name of the user group we will create next.

sudo groupadd docker

  Run the above code as shown below.

  Next, execute the following code to put our current non- root user into the user group just created. Among them, usermodis a Linux system command used to modify the user's attributes and group associations; -aGis usermodthe option of the command, which -ameans append ( Append ), -Gindicating the additional group to which the specified user belongs; dockeris the name of the user group to which the user is to be added; $USERIs an environment variable that represents the username of the current user.

sudo usermod -aG docker $USER

  Run the above code as shown below.

  Then, execute the following code to switch the effective group of the current session. Among them, newgrpis a Linux system command used to switch the effective group of the current session; dockeris the name of the target group we want to switch to.

newgrp docker

  Run the above code as shown below.

  Subsequently, our configuration work was completed. At this point, we can verify whether we have successfully completed the above configuration through the following code; this command will download a test image and run it in the container.

docker run hello-world

  Run the above code as shown below.

  If the interface shown above appears, it means that our aforementioned configuration has been successfully completed. In our last article , the detailed process of configuring Docker on Ubuntu on Linux system (https://blog.csdn.net/zhebushibiaoshifu/article/details/132612560), this line of code was also used to test whether Docker was configured successfully, and At that time, this sentence of code needed to have sudowords in front of it, as shown in the figure below; but after our above configuration, this is no longer needed sudo.

docker run hello-worldOf course, if you encounter an error after   running the above code, you can refer to Docker 's official website , which has instructions on this issue.

  At this point, you're done.

Welcome to follow: Crazy Learning GIS

Guess you like

Origin blog.csdn.net/zhebushibiaoshifu/article/details/132637472