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

A nice html printing code supports page turning

ylbtech_html_print HTML print code, support page t...

CSS3+Bezier curve to achieve scalable input search box effect

Without further ado, here are the renderings. The...

Mysql some complex sql statements (query and delete duplicate rows)

1. Find duplicate rows SELECT * FROM blog_user_re...

MySQL 8.0.12 installation and configuration method graphic tutorial (windows10)

This article records the installation graphic tut...

Difference between var and let in JavaScript

Table of contents 1. Scopes are expressed in diff...

Using JS to determine the existence of elements in an array in ten minutes

Preface In front-end development, you often need ...

The marquee tag in HTML achieves seamless scrolling marquee effect

The <marquee> tag is a tag that appears in ...

Summary of the Differences between SQL and NoSQL

Main differences: 1. Type SQL databases are prima...

Detailed explanation on how to deploy H5 games to nginx server

On the road to self-learning game development, th...

CSS to achieve the small sharp corner effect of bubbles

Effect picture (the border color is too light, pu...

Detailed explanation of JavaScript closure issues

Closures are one of the traditional features of p...

Getting Started Guide to Converting Vue to React

Table of contents design Component Communication ...