How to obtain root permissions in a docker container

How to obtain root permissions in a docker container

First, your container must be running

You can view the CONTAINER ID of the container through sudo docker container ls or sudo docker ps

Finally execute the command (7509371edd48 is the CONTAINER ID found above)

sudo docker exec -ti -u root 7509371edd48 bash

Supplement: Solve the problem that non-root users do not have permission to run docker commands

Problem description:

"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.26/images/json: dial unix
/var/run/docker.sock: connect: permission denied"

Reason (from the docker manual):

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.

The answer is obvious. Either use the root user, or create a user group called docker and add the non-root user you need to use docker to the group. If you don't know how to do it yet, keep reading.

Method 1:

Use sudo to obtain administrator privileges and run docker commands. This method has many limitations when executing docker commands through scripts.

Method 2:

When the docker daemon is started, the user group named docker is given the permission to read and write the Unix socket by default. Therefore, as long as the docker user group is created and the current user is added to the docker user group, the current user will have the permission to access the Unix socket and can execute docker-related commands.

sudo groupadd docker #Add docker user group sudo gpasswd -a $USER docker #Add the logged in user to the docker user group newgrp docker #Update the user group

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:
  • How to obtain a permanent free SSL certificate from Let''s Encrypt in Docker
  • Docker solution for logging in without root privileges
  • How to modify the root password of mysql in docker
  • How to change the root password in a container using Docker
  • docker cp copy files and enter the container
  • Docker uses root to enter the container
  • Solution to the Docker container not having permission to write to the host directory
  • How to add a certificate to docker

<<:  Share some uncommon but useful JS techniques

>>:  A brief discussion on HTML table tags

Recommend

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

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

How to create Apache image using Dockerfile

Table of contents 1. Docker Image 2. Create an in...

Notes on using the blockquote tag

<br />Semanticization cannot be explained in...

The most comprehensive explanation of the locking mechanism in MySQL

Table of contents Preface Global Lock Full databa...

Web page creation question: Image file path

This article is original by 123WORDPRESS.COM Ligh...

Modification of the default source sources.list file of ubuntu20.04 LTS system

If you accidentally modify the source.list conten...

JS implements jQuery's append function

Table of contents Show Me The Code Test the effec...

Implementation of JavaScript downloading linked images and uploading them

Since we are going to upload pictures, the first ...

3D tunnel effect implemented by CSS3

The effect achievedImplementation Code html <d...

Nginx dynamically forwards to upstream according to the path in the URL

In Nginx, there are some advanced scenarios where...

JavaScript+html to implement front-end page sliding verification (2)

This article example shares the specific code of ...

Command to view binlog file creation time in Linux

Table of contents background analyze method backg...

Centos7 installation of MySQL8 tutorial

MySQL 8 new features: My personal opinion on MySQ...

MySQL uses UNIQUE to implement non-duplicate data insertion

SQL UNIQUE constraint The UNIQUE constraint uniqu...