Docker&Kubernetes ❀ detailed explanation of basic commands of docker image and container related operations

Posted by Base on Fri, 04 Feb 2022 09:14:26 +0100

1. Docker information query

1.1 version information

[root@localhost ~]# docker version
Client: Docker Engine - Community			#Client version information
 Version:           20.10.5
 API version:       1.41
 Go version:        go1.13.15
 Git commit:        55c4c88
 Built:             Tue Mar  2 20:17:04 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community		    #Server version information
 Engine:
  Version:          20.10.5
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       363e9a8
  Built:            Tue Mar  2 20:15:27 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.3
  GitCommit:        269548fa27e0089a8b8278fc4fc781d7f65a939b
 runc:
  Version:          1.0.0-rc92
  GitCommit:        ff819c7e9184c13b7c2607fe6c30ae19403a7aff
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

1.2 details

[root@localhost ~]# docker info
Client:
 Context:    default
 Debug Mode: false
 Plugins:
  app: Docker App (Docker Inc., v0.9.1-beta3)
  buildx: Build with BuildKit (Docker Inc., v0.5.1-docker)

Server:
 Containers: 2                            #Number of containers;
  Running: 0
  Paused: 0
  Stopped: 2
 Images: 1                                #Number of images;
 Server Version: 20.10.5
 Storage Driver: overlay2
  Backing Filesystem: xfs
  Supports d_type: true
  Native Overlay Diff: true
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 1
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 269548fa27e0089a8b8278fc4fc781d7f65a939b
 runc version: ff819c7e9184c13b7c2607fe6c30ae19403a7aff
 init version: de40ad0
 Security Options:
  seccomp
   Profile: default
 Kernel Version: 4.18.0-193.el8.x86_64	    #Host kernel version;
 Operating System: CentOS Linux 8 (Core)	#Host operating system;
 OSType: linux				                #Host operating system type;
 Architecture: x86_64			            #Host architecture;
 CPUs: 4					                #Number of host cores;
 Total Memory: 3.664GiB			            #Host memory;
 Name: localhost.localdomain		        #Host name;
 ID: APEZ:LIL2:ZACW:QKAG:6JXY:IGVV:ZKML:KJKP:J7T6:USVO:D2G3:536H
 Docker Root Dir: /var/lib/docker		    #Docker root directory path;
 Debug Mode: false				            #Debug mode;
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Registry Mirrors:			            	#The registry mirror is used as Ali source;
  https://sta7qavr.mirror.aliyuncs.com/
 Live Restore Enabled: false

1.3 help command

[root@localhost ~]# docker --help
Usage:  docker [OPTIONS] COMMAND

[root@localhost ~]# man docker

2. Mirror command

2.1 local image viewing

[root@localhost ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
 Mirror warehouse source	  Mirror label	 image ID	       Creation time		  Mirror size
hello-world   latest    bf756fb1ae65   14 months ago   13.3kB
  • -a: List all local images, including the intermediate image layer;
  • -q: Only the image ID is displayed;
  • – digests: displays the image summary information;
  • – no TRUNC: display complete image information;

2.2 image search

[root@localhost ~]# docker search centos	
NAME                 DESCRIPTION                               STARS     OFFICIAL   AUTOMATED
 name				     Description information					               Number of likes	 Official recommendation	Auto install
centos                 The official build of CentOS.           6439      [OK]       

The mirror search command is equivalent to going to https://hub.docker.com/ Search the image name in the web page, and the returned information is consistent with the command result in Linux. Select the required image and click to obtain the corresponding detailed information;

Click the specific centos image. The web page has built-in image crawling command. Copy it to the Linux system for execution to complete image crawling. If you need to download a specific centos version, you need to check the specific centos versions supported by the current web page;

Pull down the web page to find the corresponding version, and then download and crawl. By default, the latest version is downloaded without selecting the version;

2.3 image pulling

[root@localhost ~]# docker pull centos			#Crawl the latest centos image;
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

[root@localhost ~]# docker pull centos:7.9.2009	#Crawl and download centos image of centos version 7.9.2009
#You need to check whether a specific version exists in the web page before you can download it, otherwise the download is invalid;

[root@localhost ~]# docker images
REPOSITORY    TAG        IMAGE ID       CREATED         SIZE
centos        latest     300e315adb2f   2 months ago    209MB
centos        7.9.2009   8652b9f0cb4c   3 months ago    204MB
hello-world   latest     bf756fb1ae65   14 months ago   13.3kB

2.4 image deletion

[root@localhost ~]# docker images
REPOSITORY    TAG        IMAGE ID       CREATED         SIZE
centos        latest     300e315adb2f   2 months ago    209MB
centos        7.9.2009   8652b9f0cb4c   3 months ago    204MB
hello-world   latest     bf756fb1ae65   14 months ago   13.3kB
[root@localhost ~]# docker rmi hello-world
Untagged: hello-world:latest
Untagged: hello-world@sha256:7e02330c713f93b1d3e4c5003350d0dbe215ca269dd1d84a4abc577908344b30
Deleted: sha256:bf756fb1ae65adf866bd8c456593cd24beb6a0a061dedf42b26a993176745f6b
Deleted: sha256:9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63

3. Container command

Due to the disadvantages of slow deployment of virtual machines, Linux has developed a new virtualization technology: Linux Containers. Linux container deployment simulates a complete operating system, but isolates the process. With a container, the resources required for software operation can be packaged into an isolated container. The container is different from the virtual machine, There is no need to bundle a complete operating system, just set the library resources required for software work, so the system becomes efficient and lightweight;

3.1 create and start container

[root@localhost ~]# docker run --help
Usage:  docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
  • – Name: Specifies the name of the container;
  • -d: Run the container in the background and return the container ID;
  • -i: Run the container in interactive mode;
  • -t: Assigning a pseudo input terminal to the container;
  • -P: Random port mapping;
  • -p: Specify port mapping;
    There are four formats for port mapping:
    • IP:HOST PORT:CONTAINER PORT
    • IP::CONTAINER PORT
    • HOST PORT:CONTAINER PORT
    • CONTAINER PORT
  • – restart=always: the container starts automatically. When the docker service is down or restarted, the container starts automatically;
  • -h: Set the container host name;
  • – dns 114.114.114.114: Specifies the DNS server of the container;
  • – DNS search: DNS search settings;
  • – add host [hostname: IP]: inject the mapping relationship between host name and IP address, and write this information into the / etc/hosts file;
  • – rm: automatically delete after the container stops;
[root@Redhat8 ~]# docker run -it --name centos-1 -d centos
abb0127a2e48f5db4c6f2ac591daa2c808f74d9cc6020c51b59357fdd74fe87f
[root@Redhat8 ~]# docker exec -it centos-1 /bin/bash
[root@abb0127a2e48 /]# exit
exit

3.2 list all containers in operation

[root@localhost ~]# docker run -it 300e315adb2f
[root@6b0a01d8c5e0 /]# 						#After entering the container, the command prefix echoes the container ID;
[root@localhost ~]# docker ps				#A container that displays the current running state;
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
 container ID		    image	       command	   Creation time	 running state 	port		  name
6b0a01d8c5e0   300e315adb2f   "/bin/bash"   About a minute ago   Up About a minute             pensive_brattain
  • -a: List all currently running containers, including running containers;
  • -l: List recently created containers;
  • -n: List the last n containers created;
  • -q: Display only container ID;
  • – no TRUNC: display complete information;

3.3 exit the container

[root@localhost ~]# docker run -it 300e315adb2f
[root@6b0a01d8c5e0 /]# exit						#The container stops exiting;
exit
[root@localhost ~]# docker ps -l
CONTAINER ID   IMAGE         COMMAND    CREATED                  STATUS                              PORTS     NAMES
364722e51f07   hello-world   "/hello"   Less than a second ago   Exited (0) Less than a second ago             silly_shockley

Ctrl+P+Q										#The container does not stop exiting;
[root@localhost ~]# docker run -it --name test_zxc centos
[root@7283bd6f0645 /]# (Press the keyboard here Ctrl+P+Q)[root@localhost ~]# 	
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
7283bd6f0645   centos    "/bin/bash"   30 seconds ago   Up 29 seconds             test_zxc

3.4 start / restart / stop / kill / delete / re login container

[root@Redhat8 ~]# docker 
attach     commit     context    diff       export     image      info       load       logs       node       port       push       rm         save       service    stats      system     trust      version
build      config     cp         events     help       images     inspect    login      manifest   pause      ps         rename     rmi        search     stack      stop       tag        unpause    volume
builder    container  create     exec       history    import     kill       logout     network    plugin     pull       restart    run        secret     start      swarm      top        update     wait

[root@localhost ~]# docker start/restart/stop/kill/rm/attach 364722e51f07 	#Multiple container ID s are separated by spaces;
364722e51f07
[root@localhost ~]# docker inspect 364722e51f07   			#View container basic information
[root@localhost ~]# docker logs 364722e51f07    			#View the container log;
[root@localhost ~]# docker stats 364722e51f07    			#View the system resources occupied by the container;
[root@localhost ~]# docker exec 364722e51f07      			#Container execution command
[root@localhost ~]# docker exec -it 364722e51f07 /bin/bash  #Login bash of the container;

Topics: Docker Kubernetes Container