[Docker] common Docker commands. Help command, mirror command, container command

Posted by ParkerPHP on Sun, 20 Feb 2022 14:16:59 +0100

Docker common commands

Docker Hub official website: https://hub.docker.com/

1. Help command

docker version # Display Docker version information.
docker info    # Displays Docker system information, including the number of images and containers
docker --help  # help

Among the three commands, docker --help is the most commonly used. When we have unclear commands, we can use this help to view them.

2. Mirror command

As the name suggests, the image command is the command for image operation.

docker images

# Lists the mirrors on the host
[root@jiangnan ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    feb5d9fea6a5   4 months ago   13.3kB

# explain
REPOSITORY    Mirrored warehouse source
TAG  		  Mirrored label
IMAGE ID 	  mirrored  ID
CREATED 	  Image creation time
SIZE 		  Mirror size

# The same warehouse source can have multiple tags, representing different versions of the warehouse source. We use REPOSITORY: TAG to define different images. If you do not define the label version of the image, docker will use the last image by default!

# Optional
-a:  List all local mirrors
-q:  Show only mirrors id
--digests:  Displays summary information for the mirror

docker search

# Search image
[root@jiangnan ~]# docker search mysql
NAME                             DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql/mysql-server               Optimized MySQL Server Docker images. Create...   905                  [OK]
# docker search the name of an image corresponds to the image in the DockerHub warehouse

# Optional
--filter=stars=50    # Lists the mirrors whose collections are not less than the specified value.

It's the same as our search on the official website.

docker pull

# Download Image
[root@jiangnan ~]# docker pull mysql
Using default tag: latest   # Do not write tag. The default is latest
latest: Pulling from library/mysql
72a69066d2fe: Pull complete    # Layered Download
1c04857f594f: Pull complete 
4d7cfa90e6ea: Pull complete 
e0431212d27d: Pull complete 
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709    #autograph
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest  # Real location

# Specified version download
[root@kuangshen ~]# docker pull mysql:5.7
....

# If we know the real location in advance, we can also download according to the real location, such as docker io/library/mysql:latest
[root@jiangnan ~]# docker pull docker.io/library/mysql:latest
latest: Pulling from library/mysql
72a69066d2fe: Pull complete 
93619dbc5b36: Pull complete 
4d7cfa90e6ea: Pull complete 
e0431212d27d: Pull complete 
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest

If we do not specify the latest version when downloading the image, press lastest to specify the default version.

docker rmi

# delete mirror
docker rmi -f image id				  # Delete single
docker rmi -f Image name:tag Image name:tag    # Delete multiple
docker rmi -f $(docker images -qa)    # Delete all recursive delete
[root@jiangnan ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
mysql         5.7       c20987f18b13   2 months ago   448MB
mysql         latest    3218b38490ce   2 months ago   516MB
hello-world   latest    feb5d9fea6a5   4 months ago   13.3kB
[root@jiangnan ~]# docker rmi -f $(docker images -qa) is equivalent to first obtaining the IDs of all images, and then passing it to the rmi command with the $sign for deletion.
Untagged: mysql:5.7
Untagged: mysql@sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Deleted: sha256:c20987f18b130f9d144c9828df630417e2a9523148930dc3963e9d0dab302a76
Deleted: sha256:5c062c3ac20f576d24454e74781511a5f96739f289edaadf2de934d06e910b92
Untagged: mysql:latest
Untagged: mysql@sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709
Deleted: sha256:3218b38490cec8d31976a40b92e09d61377359eab878db49f025e5d464367f3b
Deleted: sha256:ad6b69b549193f81b039a1d478bc896f6e460c77c1849a4374ab95f9a3d2cea2
Untagged: hello-world:latest
Untagged: hello-world@sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f
Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412
[root@jiangnan ~]# docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE
[root@jiangnan ~]# 

Note: after we execute docker rmi -f $(docker images -qa), all images are deleted.

3. Container command

Note: a container can only be created with an image. Here we use the image of centos to test, that is, a virtual centos!

First, pull a centos image

docker pull centos

Create a new container and start

docker run

# command
docker run [OPTIONS] IMAGE [COMMAND][ARG...]

# Description of common parameters
--name="Name"	# Give the container a name
-d 				# Run the container in the background mode and return the id of the container!
-i 				# Run the container in interactive mode by using with - t
-t				# Reassign a terminal to a container, usually with - i
-P				# Random port mapping (in uppercase)
-p 				# Specify the port mapping (lowercase), which can be written in four ways
		ip:hostPort:containerPort
		ip::containerPort
		hostPort:containerPort (Commonly used)
		containerPort

# Use centos to start the container in interactive mode, and execute the / bin/bash command in the container!
[root@jiangnan ~]# docker run -it centos /bin/bash
[root@a492122d4a65 /]# ls   # By this time, we have entered the interior of the container
bin  etc   lib	  lost+found  mnt  proc  run   srv  tmp  var
dev  home  lib64  media       opt  root  sbin  sys  usr
[root@a492122d4a65 /]#   

List all running containers

docker ps

# command
docker ps [OPTIONS]

# Description of common parameters
-a		 # List all currently running containers + historically running containers
-l 		 # Displays recently created containers
-n=?	 # Displays the last n containers created
-q 		 # In silent mode, only the container number is displayed.

Note: ctrl +P+Q is used here. Although the container is exited, it does not stop running.

Exit container

exit exits the container and stops

ctrl +P+Q exits the container without stopping

[root@jiangnan ~]# docker run -it centos /bin/bash
[root@a492122d4a65 /]# ls
bin  etc   lib	  lost+found  mnt  proc  run   srv  tmp  var
dev  home  lib64  media       opt  root  sbin  sys  usr
[root@a492122d4a65 /]# exit
exit
[root@jiangnan ~]# 

Start stop container

docker start (container id or Container name) 		# Start container
docker restart (container id or Container name) 	# Restart container
docker stop (container id or Container name) 		# Stop container
docker kill (container id or Container name) 		# Force stop container

Delete container

docker rm

docker rm container id 				   # Delete specified container
docker rm -f $(docker ps -a -q) # Delete all containers recursively delete
docker ps -a -q|xargs docker rm # Delete all container channel characters

Generally, only stopped containers can be deleted. Of course, they can also be forcibly deleted through the parameter - f.

4. Other common commands

Background startup container

docker run -d

# command
docker run -d Container name

# example
docker run -d centos # Start centos in background mode

Problem: after using the background startup, we use docker ps to check and find that the container has exited!

Explanation: when the docker container runs in the background, there must be a foreground process. If the commands run by the container are not those that have been suspended, they will exit automatically. For example, if you run the nginx service, but the docker foreground does not run the application, in this case, the container will commit suicide immediately after starting, because he thinks there is no program, so the best situation is to run and start your application in the way of foreground process.

view log

docker logs

# command
docker logs -f -t --tail Digital container id

# Example: we start centos and write a script to test it! Finally, check the log
[root@jiangnan ~]# docker run -d centos /bin/bash -c "while true;do echo jiangnan;sleep 1;done"

[root@jiangnan ~]# docker ps
CONTAINER ID   IMAGE
d7c02f12485e   centos

# -Tdisplay timestamp
# -f print the latest log
# --How many tail numbers are displayed!
[root@jiangnan ~]# docker logs -tf --tail 10 d7c02f12485e
2022-02-19T15:09:19.631411184Z jiangnan
2022-02-19T15:09:20.633827865Z jiangnan
2022-02-19T15:09:21.636266883Z jiangnan
2022-02-19T15:09:22.638810644Z jiangnan
2022-02-19T15:09:23.640823083Z jiangnan
2022-02-19T15:09:24.642835236Z jiangnan
2022-02-19T15:09:25.644915196Z jiangnan
2022-02-19T15:09:26.647109342Z jiangnan
2022-02-19T15:09:27.649322806Z jiangnan
2022-02-19T15:09:28.651438593Z jiangnan

View the process information running in the container

docker top

# command
docker top container id

[root@jiangnan ~]# docker top d7c02f12485e
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                16340               16321               0                   23:09               ?                   00:00:00            /bin/bash -c while true;do echo jiangnan;sleep 1;done
root                16785               16340               0                   23:15               ?                   00:00:00            /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
[root@jiangnan ~]# 

View metadata of container / image

docker inspect

# command
docker inspect container id

[root@jiangnan ~]# docker inspect d7c02f12485e
[
    {
        "Id": "d7c02f12485e6e28fc83388b8079c41aba55979a038a858a4c2ebe75e51944fe",
        "Created": "2022-02-19T15:09:10.338384531Z",
        "Path": "/bin/bash",
        "Args": [
            "-c",
            "while true;do echo jiangnan;sleep 1;done"
        ],
        "State": {
            "Status": "running",
            ...
]
[root@jiangnan ~]# 

Enter the running container

docker exec

docker attach container id

# Command 1
docker exec -it container id bashShell

# Command 2
docker attach container id

Difference: exec is to open a new terminal in the container and start a new process.
attach directly enters the terminal of the container startup command and will not start a new process.

When we started centos earlier, there was a script. Using exec to enter will start a new process and will not print logs. When you use attach to enter, you directly start printing the log without starting a new process.

Copy files from the container to the host

docker cp

# command
docker cp container id:In container path destination host path

# Execute in the container and create a file test
[root@d7c02f12485e /]# cd /home
[root@d7c02f12485e home]# ls
[root@d7c02f12485e home]# touch text.txt
[root@d7c02f12485e home]# ls
text.txt
[root@d7c02f12485e home]# exit
exit

# Exit the container and execute outside the container
[root@jiangnan ~]# docker cp d7c02f12485e:/home/text.txt /home
[root@jiangnan ~]# cd /home
[root@jiangnan home]# ls
aliyun-xxc  mygroup  text.txt
[root@jiangnan home]# 

It is found that there is test.com in the / home directory of the external host Txt file.

5. Summary

Common commands

attach		Attach to a running container # The attach connection under the current shell specifies the running image
build		Build an image from a Dockerfile # Customized image through Dockerfile
commit 		Create a new image from a container changes # Commit the current container as a new image
cp			Copy files/folders from the containers filesystem to the host path #Copy the specified file or directory from the container to the host
create		Create a new container # Create a new container, the same as run, but do not start the container
diff		Inspect changes on a container's filesystem # View docker container changes
events		Get real time events from the server # Get container real-time events from docker service
exec		Run a command in an existing container # Run the command on an existing container 
export 		Stream the contents of a container as a tar archive # Export the content stream of the container as a tar archive [corresponding to import]
history		Show the history of an image # Show a mirror formation history
images 		List images # Lists the current image of the system
import 		Create a new filesystem image from the contents of a tarball # Create a new file system image from the contents of the tar package [corresponding to export]
info 		Display system-wide information # Display system related information
inspect 	Return low-level information on a container # View container details
kill 		Kill a running container # kill specifies the docker container 
load 		Load an image from a tar archive # Load an image from a tar package [corresponding to save]
login 		Register or Login to the docker registry server # Register or log in to a docker source server
logout 		Log out from a Docker registry server # Exit from the current Docker registry
logs 		Fetch the logs of a container # Output current container log information
port 		Lookup the public-facing port which is NAT-ed to PRIVATE_PORT #View the internal source port of the container corresponding to the mapped port
pause 		Pause all processes within a container # Pause container
ps 			List containers # List containers
pull 		Pull an image or a repository from the docker registry server #Pull the specified image or library image from the docker image source server
push 		Push an image or a repository to the docker registry server #Push the specified image or library image to the docker source server
restart 	Restart a running container # Restart the running container
rm 			Remove one or more containers # Remove one or more containers
rmi 		Remove one or more images # Remove one or more images [no container can be deleted without using the image, otherwise the relevant container needs to be deleted before continuing or -f forced deletion]
run 		Run a command in a new container # Create a new container and run a command
save 		Save an image to a tar archive # Save an image as a tar package [corresponding to load]
search 		Search for an image on the Docker Hub # Search for images in docker hub
start 		Start a stopped containers # Start container
stop 		Stop a running containers # Stop container
tag 		Tag an image into a repository # Label the image in the source
top 		Lookup the running processes of a container # View the process information running in the container 
unpause 	Unpause a paused container # Unsuspend container
version 	Show the docker version information # View docker version number
wait 		Block until a container stops, then print its exit code # Intercept the exit status value when the container stops

Five hours, while testing, editing and sorting, point a praise and support it. I hope it will be useful to you!

The series of blogs are constantly updated.

Topics: Linux Docker Container