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

Linux file/directory permissions and ownership management

1. Overview of file permissions and ownership 1. ...

Solution to MySQL error code 1862 your password has expired

The blogger hasn't used MySQL for a month or ...

MySQL query_cache_type parameter and usage details

The purpose of setting up MySQL query cache is: C...

Example of fork and mutex lock process in Linux multithreading

Table of contents Question: 1. First attempt 2. R...

Implementation of MySQL asc and desc data sorting

Data sorting asc, desc 1. Single field sorting or...

Implementation of debugging code through nginx reverse proxy

background Now the company's projects are dev...

Summary of some reasons why crontab scheduled tasks are not executed

Preface I recently encountered some problems at w...

How to find and delete duplicate rows in MySQL

Table of contents 1. How to find duplicate rows 2...

Implementation of Vue large file upload and breakpoint resumable upload

Table of contents 2 solutions for file upload Bas...

JavaScript data visualization: ECharts map making

Table of contents Overview Precautions 1. Usage 2...

Nginx learning how to build a file hotlink protection service example

Preface Everyone knows that many sites now charge...

Six methods for nginx optimization

1. Optimize Nginx concurrency [root@proxy ~]# ab ...