Docker uses root to enter the container

Docker uses root to enter the container

First run the docker container

Run the command as root user

sudo docker exec -it -u root ec33c19230ca /bin/bash

Supplement: Enter the container command line and run in the background in Docker

In Docker, we generally have two ways to execute commands. One is to directly enter the command line of the container, execute in the terminal and view the results. The other is to execute in the background and not view the results in the terminal.

1. Enter the container command line

su root
docker run -i -t ubuntu:16.04 /bin/bash
#Or execute the following sentence docker run -it ubuntu:16.04 /bin/bash

The purpose of -i and -t is to help you enter interactive mode. Without this part, you will not be able to enter the container, let alone use the command line. /bin/bash means using the container's command line to enter commands. The execution results are as follows:

root@7d150a0fb029:/#

The root is followed by the container ID, indicating that you have entered the container and can enter commands. You can try entering some common commands:

root@7d150a0fb029:/# docker run -i -t ubuntu:16.04 /bin/bash

The result outputs the kernel version information of the image:

Linux version 4.13.0-36-generic (buildd@lgw01-amd64-033) (gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.9)) #40~16.04.1-Ubuntu SMP Fri Feb 16 23:25:58 UTC 2018

You can exit the container by exiting.

2. Background execution command

Enter the following code, -d means running in the background. You can view the function of the command directly through docker run --help. The following sentence means to add the code in quotation marks to the image program and run it in the background.

docker run -d ubuntu:16.04 /bin/sh -c "while true; do echo hello world; sleep 1; done"

The output will not show the result of the run, but will give the newly created container id. Here is an explanation: each time you enter the docker run command, a new container will be created and a new id will be generated.

The output is as follows:

c7188be9bd7b5c0aa91c5eeb72013996c89bfdc2181d1a5ff7f880af6f8aff99

Take a look at the running container:

root@cdl-XPS15R:/home/cdl# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c7188be9bd7b ubuntu:16.04 "/bin/sh -c 'while t..." 13 seconds ago Up 11 seconds keen_vaughan
1b6ba17f7df7 ubuntu:16.04 "/bin/bash" 2 minutes ago Exited (0) 2 minutes ago cocky_noether
7d150a0fb029 ubuntu:16.04 "/bin/bash" 4 minutes ago Exited (0) 2 minutes ago hopeful_banach

Here we can find that the first few digits of the ID of the first container are exactly the same as the first few digits of the container ID running in the background, but the length is much longer. In fact, they are the same, but only the first few digits are displayed for convenience when printing the output through the ps command.

If we want to enter the command line of the container running in the background without creating a new container, just enter the following command:

#exec is to enter an existing container, run is to create a new container docker exec -it c7188be9bd7b /bin/bash

The same effect will occur if you enter a longer id:

docker exec -it c7188be9bd7b5c0aa91c5eeb72013996c89bfdc2181d1a5ff7f880af6f8aff99 /bin/bash

The output results are:

root@c7188be9bd7b:/#

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
  • How to obtain root permissions in a docker container
  • docker cp copy files and enter the container
  • Solution to the Docker container not having permission to write to the host directory
  • How to add a certificate to docker

<<:  Summary of MySQL Undo Log and Redo Log

>>:  Use vue2+elementui for hover prompts

Recommend

Record the process of connecting to the local Linux virtual machine via SSH

Experimental environment: Physical machine Window...

Difference and principle analysis of Nginx forward and reverse proxy

1. The difference between forward proxy and rever...

How to use React forwardRef and what to note

Previously, react.forwardRef could not be applied...

Vue+element ui realizes anchor positioning

This article example shares the specific code of ...

Solution to span width not being determined in Firefox or IE

Copy code The code is as follows: <html xmlns=...

Vue integrates Tencent Map to implement API (with DEMO)

Table of contents Writing Background Project Desc...

General Guide to Linux/CentOS Server Security Configuration

Linux is an open system. Many ready-made programs...

JavaScript navigator.userAgent obtains browser information case explanation

The browser is probably the most familiar tool fo...

Detailed explanation of custom swiper component in JavaScript

Table of contents Effect display Component Settin...

Vue implements two routing permission control methods

Table of contents Method 1: Routing meta informat...

Solution to changing the data storage location of the database in MySQL 5.7

As the data stored in the MySQL database graduall...

Methods for defragmenting and reclaiming space in MySQL tables

Table of contents Causes of MySQL Table Fragmenta...