Docker commands are implemented so that ordinary users can execute them

Docker commands are implemented so that ordinary users can execute them

After installing docker, there will usually be a docker user group

Step 2: Add the current user to the docker group

sudo gpasswd -a ${USER} docker

Step 3: Restart Docker

sudo systemctl restart docker

Step 4: Grant read and write permissions

sudo chmod a+rw /var/run/docker.sock

Supplement: Non-root users do not have permission to use docker

No permission is prompted when running docker run

Official documentation for installing Docker on CentOS

The docker user group is automatically created when installing docker-ce, but you need to manually add users to the docker user group

$ sudo usermod -aG docker $USER

Or $ sudo usermod -aG docker {specify username}

But after adding the user to the docker user group, it still prompts no permission

For example, I want to run bash on Ubuntu 14.04:

[hsowan@localhost shell-workspace]$ docker run -it --rm ubuntu:14.04 bash
docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.38/containers/create: dial unix /var/run/docker.sock: connect: permission denied.
See 'docker run --help'.

Why? The reason is that although the user has joined the docker user group, he has not switched to the docker user group, so he still has no permissions.

If a user belongs to multiple user groups at the same time, the user can switch between user groups to have the permissions of other user groups.

$ newgrp docker

Now you can execute docker related commands under non-root users

In addition to adding user groups to users, is there any other way to allow non-root users to execute docker-related commands?

OK, let's check the permissions of /var/run/docker.sock

$ sudo ll /var/run/docker.sock

You will get the following result:

srw-rw----. 1 root docker 0 Sep 22 15:29 /var/run/docker.sock

So now directly modify the permissions of /var/run/docker.sock

$ sudo chmod 666 /var/run/docker.sock

Now you can run docker as a non-root user

$ docker run -it --rm ubuntu:14.04 bash
root@5c60abab6425:/# cat /etc/os-release 
NAME="Ubuntu"
VERSION="14.04.5 LTS, Trusty Tahr"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 14.04.5 LTS"
VERSION_ID="14.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
root@5c60abab6425:/# exit
exit

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM. If there are any mistakes or incomplete considerations, please feel free to correct me.

You may also be interested in:
  • Docker exec executes multiple commands
  • How to execute Linux shell commands in Docker
  • Docker executes a command in a container outside the container
  • How to execute Docker commands without sudo
  • Execute multiple commands after docker run
  • Solve the problem of errors when executing docker daemon commands

<<:  MySQL online log library migration example

>>:  Web front-end development course What are the web front-end development tools

Recommend

MySql5.7.21 installation points record notes

The downloaded version is the Zip decompression v...

How to install Maven automatically in Linux continuous integration

Unzip the Maven package tar xf apache-maven-3.5.4...

Node.js+express+socket realizes online real-time multi-person chat room

This article shares the specific code of Node.js+...

Docker uses a single image to map to multiple ports

need: The official website's resource server ...

Some tips for using less in Vue projects

Table of contents Preface 1. Style penetration 1....

MySQL 5.7.15 installation and configuration method graphic tutorial (windows)

Because I need to install MySQL, I record the ins...

The process of SSH service based on key authentication in Linux system

As we all know, SSH is currently the most reliabl...

Using react-virtualized to implement a long list of images with dynamic height

Table of contents Problems encountered during dev...

40+ Beautiful Web Form Design Examples

Web forms are the primary communication channel b...

Introduction to TypeScript interfaces

Table of contents 1. Interface definition 2. Attr...

Solution to MySQLSyntaxErrorException when connecting to MySQL using bitronix

Solution to MySQLSyntaxErrorException when connec...