Problem description
The following problems occurred when running docker ps command today:
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.38/containers/json: dial Unix/var/run/docker. The sock: connect: permission deniedCopy the code
Mysql > select root from user root;
If you want to use docker-related commands as a normal user, what should you do?
We went to Docker Mannual to find out the reason. The reason is as follows:
Manage Docker as a non-root user
The docker daemon binds to a Unix socket instead of a TCP port. By default that Unix socket is owned by the user root and other users can only access it using sudo. The docker daemon always runs as the root user.
If you don’t want to use sudo when you use the docker command, create a Unix group called docker and add users to it. When the docker daemon starts, it makes the ownership of the Unix socket read/writable by the docker group.
Copy the code
Docker processes use Unix sockets instead of TCP ports. By default, the Unix socket belongs to the root user and can be accessed by the root user.
How can we solve this problem?
Solution 1
Use sudo to get admin privileges and run the docker command. (I don’t recommend this method because I failed.)
Solution 2
When the Docker daemon is started, the user group named docker is granted the permission to read and write Unix sockets by default. Therefore, as long as you create a Docker user group and add the current user to the docker user group, The current user has access to Unix sockets and can execute docker-related commands.
We can use the following command to solve the problem:
Sudo groupadd docker # add docker USER group sudo gpasswd -a $USER docker # add docker USER group sudo gpasswd -a $USER docker # The docker command can be used with sudoCopy the code
Then we can solve the problem perfectly, with the following effect: