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 brief discussion on the placement of script in HTML

I used to think that script could be placed anywh...

The meaning of status code in HTTP protocol

A status code that indicates a provisional respon...

Install and use Git and GitHub on Ubuntu Linux

Introduction to Git Git is an open source version...

Detailed explanation of Angular data binding and its implementation

Table of contents Preface What is data binding? T...

Why I recommend Nginx as a backend server proxy (reason analysis)

1. Introduction Our real servers should not be di...

Sample code for converting video using ffmpeg command line

Before starting the main text of this article, yo...

Basic usage of @Font-face and how to make it compatible with all browsers

@Font-face basic introduction: @font-face is a CSS...

Steps to set up and mount shared folders on Windows host and Docker container

Programs in Docker containers often need to acces...

Implementation steps for docker-compose to deploy etcd cluster

Table of contents Write docker-compose.yml Run do...

js development plug-in to achieve tab effect

This article example shares the specific code of ...