Docker (II): common docker commands

Time:2021-9-18

Life is boundless, learning is boundless

1、 Help command

Docker version # displays the docker version information
Docker info # displays the docker system information, including the number of images and containers
Docker command -- help # help information

Help documentation:https://docs.docker.com/engin…

2、 Mirror command

Help documentation:https://docs.docker.com/engin…

Docker images view images on all local hosts

[[email protected] /]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED      SIZE
hello-world   latest    d1165f221234   3 days ago   13.3kB

#Explain
Repository source of the repository image
Tag mirror label
Image ID ID of the image
The creation time of the created image
Size the size of the mirror

#Optional
  -a. -- all # lists all mirrors
  -q. -- quiet # only display ID
  
[[email protected] /]# docker images -a
REPOSITORY    TAG       IMAGE ID       CREATED      SIZE
hello-world   latest    d1165f221234   3 days ago   13.3kB
[[email protected] /]# docker images -q
d1165f221234
[[email protected] /]# docker images -aq
d1165f221234
[[email protected] /]#

Docker search search image

[[email protected] /]# docker search mysql
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   10589     [OK]       
mariadb                           MariaDB Server is a high performing open sou…   3963      [OK]  

#Optional, filter by mirroring
  -f. -- filter = stars = 3000 # the images searched are those with stars greater than 3000 

[[email protected] /]# docker search mysql --filter=STARS=3000
NAME      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql     MySQL is a widely used, open-source relation…   10589     [OK]       
mariadb   MariaDB Server is a high performing open sou…   3963      [OK]       
[[email protected] /]# docker search mysql --filter=STARS=5000
NAME      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql     MySQL is a widely used, open-source relation…   10589     [OK]       
[[email protected] /]#

Docker pull Download Image

docker pull image_name

#Download a MySQL image. The latest image is downloaded by default
[[email protected] /]# docker pull mysql

#Download the image of the specified version
[[email protected] /]# docker pull mysql:5.7

#Take a look at all the downloaded images
[[email protected] /]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED       SIZE
hello-world   latest    d1165f221234   3 days ago    13.3kB
mysql         5.7       a70d36bc331a   7 weeks ago   449MB
mysql         latest    c8562eaf9d81   7 weeks ago   546MB

delete mirror

#Delete image docker RMI - F a70d36bc331a according to ID
[[email protected] /]# docker rmi -f a70d36bc331a
Untagged: mysql:5.7
Untagged: [email protected]:b3d1eff023f698cd433695c9506171f0d08a8f92a0c8063c1a4d9db9a55808df
Deleted: sha256:a70d36bc331a13d297f882d3d63137d24b804f29fa67158c40ad91d5050c39c5
Deleted: sha256:50c77bf7bcddd1f1d97789d80ac2404eec22c860c104e858620d2a2e321f0ef7
Deleted: sha256:14244329b83dfc8982398ee4104a548385652d2bffb957798ff86a419013efd6
Deleted: sha256:6d990477f90af28473eb601a9bca22253f6381e053c5a8edda0a4f027e124a3c
Deleted: sha256:ee0449796df204071589162fc16f8d65586312a40c68d1ba156c93c56f5e5ce8
[[email protected] /]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED       SIZE
hello-world   latest    d1165f221234   3 days ago    13.3kB
mysql         latest    c8562eaf9d81   7 weeks ago   546MB
[[email protected] /]# 

##Delete all mirrors
[[email protected] /]# docker rmi -f $(docker images -aq)
[[email protected] /]# docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE
[[email protected] /]# 

#Delete multiple mirrors
Docker RMI - f image ID image ID

3、 Container command

Note: a container can only be created with an image. For Linux, download a CentOS image

[[email protected] /]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
7a0437f04f83: Pull complete 
Digest: sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
[[email protected] /]# 

Create a new container and start

Docker run [optional parameter] image

#Parameter description
--Name = "name" the container name tomcat01 tomcat02 is used to distinguish containers
-D background operation
-It runs interactively, entering the container to view the content
-P specify container port - P 8080:8080

    -P IP: host port: container port
    -P host port: container port (common)
    -P container port
    Container port
    
-P randomly assigned port

#Test, start and enter the container
[[email protected] /]# docker run -it centos /bin/bash
[ [email protected] /]#Ls # view CentOS in container
bin  etc   lib      lost+found  mnt  proc  run   srv  tmp  var
dev  home  lib64  media       opt  root  sbin  sys  usr

#Return the host from the container
[[email protected] /]# exit
exit
[[email protected] /]# ls
bin   dev  home        lib    lost+found  mnt  patch  root  sbin  sys  usr  www
boot  etc  install.sh  lib64  media       opt  proc   run   srv   tmp  var

See which containers are currently running

#Docker PS displays a functioning container
-A # displays currently running containers + historically running containers
-n=? #  Displays recently created containers
-Q # only the number of the container is displayed
[[email protected] /]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[[email protected] /]# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED         STATUS                          PORTS     NAMES
685c34c813d5   centos         "/bin/bash"   3 minutes ago   Exited (0) About a minute ago             pensive_shaw
db8909cfa9cc   d1165f221234   "/hello"      4 hours ago     Exited (0) 4 hours ago                    adoring_shockley
[[email protected] /]# docker ps -n=1
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS                     PORTS     NAMES
685c34c813d5   centos    "/bin/bash"   7 minutes ago   Exited (0) 5 minutes ago             pensive_shaw
[[email protected] /]# docker ps -aq
685c34c813d5
db8909cfa9cc
[[email protected] /]#

Exit container

Exit # directly exits the container and stops
CTRL + P + Q # exits the container but the container does not stop

[[email protected] /]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[[email protected] /]# docker run -it centos /bin/bash
[[email protected] /]# [[email protected] /]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
589fa10f7998   centos    "/bin/bash"   31 seconds ago   Up 29 seconds             magical_noether
[[email protected] /]#

Start, stop and restart container commands

docker start container_ name/container_ ID # start container
docker stop container_ name/container_ ID # stops the currently running container
docker restart container_ name/container_ ID # restart container
docker kill container_ name/container_ ID # forces the current container to stop

After starting a container in the background, if you want to enter the container, you can use the attach command

docker attach container_name/container_id

Command to delete a container

docker rm container_ name/container_ ID # cannot delete a running container

Delete all stopped containers

docker rm -f $(docker ps -a -q)

View docker information of current system

docker info

Download an image from docker hub

docker pull centos:latest

Find nginx images on the locker hub

docker search nginx

4、 Other commonly used commands

Background startup container

#Docker run - D image name
[[email protected] /]# docker run -d centos
dd83c9017b8b6cb2ed07d1943e0db8421b7ad0864ab291e7d470a6af76a8a6a9
[[email protected] /]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[[email protected] /]# 

#Problem: docker PS found CentOS stopped
#Common pitfalls: when the docker container runs in the background, there must be a foreground process. When docker finds that there is no application, it will automatically stop
#Nginx, after the container is started, if it is found that the attack does not provide services, it will stop immediately, that is, there is no program

view log

Docker logs - F - t -- tail container

View the process information in the container PS

#Command docker top container ID
[[email protected] /]# docker top fbff5b668c3a
UID      PID       PPID      C       STIME       TTY           TIME        CMD     
root     4345      4324      0       16:59       pts/0         00:00:00    /bin/bash

View metadata in the image

#Docker inspect container ID
#Testing
[[email protected] /]#Docker inspect container ID

Enter the currently running container

#Command docker exec - it container ID / bin / Bash

#Testing
[[email protected] /]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
fbff5b668c3a   centos    "/bin/bash"   16 minutes ago   Up 16 minutes             blissful
[[email protected] /]# docker exec -it fbff5b668c3a /bin/bash
[[email protected] /]# ls
bin  etc   lib      lost+found  mnt  proc  run   srv  tmp  var
dev  home  lib64  media       opt  root  sbin  sys  usr
[[email protected] /]# ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 08:59 pts/0    00:00:00 /bin/bash
root        16     0  0 09:16 pts/1    00:00:00 /bin/bash
root        32    16  0 09:17 pts/1    00:00:00 ps -ef

#Method 2: docker attach container ID
[[email protected] /]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
fbff5b668c3a   centos    "/bin/bash"   20 minutes ago   Up 20 minutes             blissful_tu
[[email protected] /]# docker attach fbff5b668c3a
[[email protected] /]# ls
bin  etc   lib      lost+found  mnt  proc  run   srv  tmp  var
dev  home  lib64  media       opt  root  sbin  sys  usr
[[email protected] /]#

Copy files from the container to the host

#Command docker CP container ID: the host path of the destination path in the container
#Enter container
[[email protected] /]# docker attach fbff5b668c3a      
[[email protected] /]# ls
bin  etc   lib      lost+found  mnt  proc  run   srv  tmp  var
dev  home  lib64  media       opt  root  sbin  sys  usr
[[email protected] /]# cd home/
 #Create a new test.java file under the home path in the container
[[email protected] home]# touch test.java     
[[email protected] home]# ls
test.java
[[email protected] home]# exit
exit
[[email protected] /]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
#Copy the test.java file in the container to the Linux host
[[email protected] /]# docker cp fbff5b668c3a:/home/test.java /home    
[[email protected] /]# cd home/
#Successfully copied to / home directory
[[email protected] home]# ls
f2  f3  jiangwang  test.java  www

Recommended Today

Blog Garden Background page dynamic effects

1. To set animation, you must first apply for permission 1.1 first enter [my blog park] and enter [settings] in [management] 1.2 find [blog sidebar announcement] and click [apply for JS permission] 1.3 write the content of application JS permission (examples are as follows) Dear blog administrator: Can you open JS permission for me? I […]