docker base command

1. docker command-mirror

1. View docker version

docker -v 
docker version 
decker info  #You can view the number of mirrors for all running containers, the version of the running containers, the CPU s that can be allocated, the total memory, and so on.docker's working directory/var/lib/decker

[root@ct ~]# docker version
Client: Docker Engine - Community
 Version:           20.10.8     #Edition
 API version:       1.41        #api version
 Go version:        go1.16.6    #go language version
 Git commit:        3967b7d
 Built:             Fri Jul 30 19:55:49 2021
 OS/Arch:           linux/amd64
 Context:           default     #Mapping Default    
 Experimental:      true        #Experimental function on

Server: Docker Engine - Community
 Engine:
  Version:          20.10.8
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.16.6
  Git commit:       75249d8   #Changes in the staging area to the id submitted to the local version Library
  Built:            Fri Jul 30 19:54:13 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.9
  GitCommit:        e25210fe30a0a703442421b0f60afac609f950a3
 runc:
  Version:          1.0.1
  GitCommit:        v1.0.1-0-g4144b63
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
[root@ct ~]#

2. Search Mirror

docker search nginx 
->Make your own mirror and upload it to docker hub upper
 Optimize: effectively reduce the size of the image, dockerfile Optimized Direction

You can also go directly to the official website:

https://hub.docker.com/ The number of the target version found in tag, the first official distribution of Searchinginx
Then grab the download docker pull nginx:1.21.0

3. Download Mirror

Default is from docker hub

docker pull Mirror Name
 for example:docker pull nginx  #Default latest without labeling, latest version

4. View Mirror List

docker images   #View Download Image Information under current docker
[root@ct ~]# docker images
REPOSITORY    TAG              IMAGE ID       CREATED       SIZE
#Mirror warehouse       #Label             #image id       #Establishment Time      #Size
redis         6.2-alpine3.14   f6f2296798e9   3 days ago     32.3MB
redis         v1               f6f2296798e9   3 days ago     32.3MB
busybox       latest           42b97d3c2ae9   10 days ago    1.24MB
nginx         1.21             dd34e67e3371   13 days ago    133MB    #
nginx         v1               dd34e67e3371   13 days ago    133MB 
#Based on the image id, it is found that it is the same image as above, but with different labels  
hello-world   latest           d1165f221234   5 months ago   13.3kB
centos        7                8652b9f0cb4c   9 months ago   204MB
[root@ct ~]# docker images  redis:v1        #Looking at a single image, REPOSITORY+TAG is a single image (uniqueness), and tags can be artificially specified
REPOSITORY   TAG       IMAGE ID       CREATED      SIZE
redis        v1        f6f2296798e9   3 days ago   32.3MB

5 Get mirror information

docker inspect  image ID (Or mirror: label)      

docker inspect redis:
[root@ct ~]# docker inspect nginx:1.21
[
    {
        "Id": "sha256:dd34e67e3371dc2d1328790c3157ee42dfcae74afffd86b297459ed87a98c0fb",
        "RepoTags": [
            "nginx:1.21",
            "nginx:v1"
        ],
        "RepoDigests": [
            "nginx@sha256:4d4d96ac750af48c6a551d757c1cbfc071692309b491b70b2b8976e102dd3fef"
        ],
        "Parent": "",
....................
[root@ct ~]# docker inspect dd34e67e3371
[
    {
        "Id": "sha256:dd34e67e3371dc2d1328790c3157ee42dfcae74afffd86b297459ed87a98c0fb",
        "RepoTags": [
            "nginx:1.21",  #There are two other names for the mirror
            "nginx:v1"      #Another mirror name
        ],
        "RepoDigests": [
            "nginx@sha256:4d4d96ac750af48c6a551d757c1cbfc071692309b491b70b2b8976e102dd3fef"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2021-08-17T11:46:11.370722453Z",
        "Container": "c8f4640bdac21c018ff68bfb015ab66f87de0ed066b73eb6607a832a8abf3581",
        "ContainerConfig": {
            "Hostname": "c8f4640bdac2",


6. Add mirror labels

docker tag Software Name:Old Label Software Name:new label 
[root@ct ~]# docker tag nginx:1.21 nginx:lnmp
[root@ct ~]# docker images
REPOSITORY    TAG              IMAGE ID       CREATED        SIZE
redis         6.2-alpine3.14   f6f2296798e9   3 days ago     32.3MB
redis         v1               f6f2296798e9   3 days ago     32.3MB
busybox       latest           42b97d3c2ae9   10 days ago    1.24MB
nginx         1.21             dd34e67e3371   13 days ago    133MB
nginx         lnmp             dd34e67e3371   13 days ago    133MB
#The old image will not disappear. The new image name will be different, but the id will not change.
nginx         v1               dd34e67e3371   13 days ago    133MB
hello-world   latest           d1165f221234   5 months ago   13.3kB
centos        7                8652b9f0cb4c   9 months ago   204MB

7. Delete Mirror

docker rmi    Mirror: Label
docker rmi  `docker images -q`  #-q List Mirror ID s, Bulk Delete
docker rmi -f Mirror: Label  #Force Delete

8. Mirror Export and Import

docker save -o  File Name Mirror Name   #Export Generation File
docker load < file name    #Reimport Mirror   

docker save -o centos-7 centos:7  #Export build file centos-7
docker load <centos-7      #Export file centos-7
[root@ct ~]# cd /opt/
[root@ct opt]# ls
containerd  mysql-5.7.20  mysql-boost-5.7.20.tar.gz
[root@ct opt]# docker save -o centos-7 centos:7   #Generate Mirror File
[root@ct opt]# ls
centos-7  containerd  mysql-5.7.20  mysql-boost-5.7.20.tar.gz

[root@ct opt]# docker rmi -f centos:7   #Remove the original image
Untagged: centos:7
Untagged: centos@sha256:0f4ec88e21daf75124b8a9e5ca03c37a5e937e0e108a255d890492430789b60e
Deleted: sha256:8652b9f0cb4c0599575e5a003f5906876e10c1ceb2ab9fe1786712dac14a50cf

[root@ct opt]# docker images
REPOSITORY    TAG              IMAGE ID       CREATED        SIZE
redis         6.2-alpine3.14   f6f2296798e9   3 days ago     32.3MB
busybox       latest           42b97d3c2ae9   10 days ago    1.24MB
nginx         1.21             dd34e67e3371   13 days ago    133MB
nginx         lnmp             dd34e67e3371   13 days ago    133MB
hello-world   latest           d1165f221234   5 months ago   13.3kB
#There is no centos mirror anymore
[root@ct opt]# docker load<centos-7
Loaded image: centos:7
[root@ct opt]# docker images
REPOSITORY    TAG              IMAGE ID       CREATED        SIZE
redis         6.2-alpine3.14   f6f2296798e9   3 days ago     32.3MB
busybox       latest           42b97d3c2ae9   10 days ago    1.24MB
nginx         1.21             dd34e67e3371   13 days ago    133MB
nginx         lnmp             dd34e67e3371   13 days ago    133MB
hello-world   latest           d1165f221234   5 months ago   13.3kB
centos        7                8652b9f0cb4c   9 months ago   204MB
#Regenerate


2. docker command - container operation

1. Query Container

docker ps -a   #Displays containers on the host and their status
docker ps -aq  #Show ip of container
[root@ct ~]# docker ps -a
CONTAINER ID   IMAGE                  COMMAND                  CREATED        STATUS                      PORTS      NAMES
57f77cf81738   centos:7               "/bin/bash"              30 hours ago   Exited (137) 25 hours ago              mystifying_buck
1ce04b9f9cad   f6f2296798e9           "docker-entrypoint.s..."   2 days ago     Up 2 days                   6379/tcp   nervous_dirac
914b1af0ecb0   redis:6.2-alpine3.14   "docker-entrypoint.s..."   2 days ago     Up 2 days                   6379/tcp   raide1
886cd3d8c81e   nginx:1.21             "/docker-entrypoint...."   2 days ago     Up 2 days                   80/tcp     socker1


CONTAINER ID: container ID
IMAGE: Mirror Version
CRATED:Time of creation
STATUS:Running time
PORTS:Exposed Ports
NAMES:Name (definable)

2. Create Containers

docker create -itd nginx:latest /bin/bash
-i Keep standard input for containers open
-t Assign a pseudo-terminal
-d Run as a daemon in the background

3. Start Container

docker start  container id
docker run centos:7 /usr/bin/bash -c ls /
Start Container(One-time Execution)

docker run -itd centos:7 /bin/bash/
#Continue in the background

4. Stop Container

docker stop  container ID
#Status code 0: normal exit, non-zero value indicates abnormal exit, 137 indicates active stop exit
[root@ct ~]# docker stop 57f77cf81738
57f77cf81738

5. Containers continue to run in the background

[root@ct ~]# docker run -d centos:7 /usr/bin/bash -c "while true;do echo hello;done"
418050aa42b7e7c182eb484cb9b80519c708c0638d65c9add6ebfc016ce00633
[root@docker ~]# watch -n 2 docker ps -a    #Execute every two seconds
[root@docker ~]# docker rm `docker ps -aq`    #Running will not be deleted

6. Enter Containers

Use run
docker run -it nginx:latest /bin/bash         #One-time, exit closes
exec(Container must be open)
docker exec -it  container ID /bin/bash

PS:
docker run -it A foreground process is created, but it is entered exit Terminate the process after.
docker attach  Will connect through stdin´╝îConnect to the input and output streams in the container, where the input exit Post-termination process.
docker exec -it Will connect to the container, like SSH Similarly, it enters the container and operates through exit Exit the container without affecting its operation.

[root@ct ~]# docker ps -a
CONTAINER ID   IMAGE                  COMMAND                  CREATED         STATUS         PORTS      NAMES
e86cc40e5351   centos:7               "/bin/bash"              3 seconds ago   Up 2 seconds              great_golick
1ce04b9f9cad   f6f2296798e9           "docker-entrypoint.s..."   2 days ago      Up 2 days      6379/tcp   nervous_dirac
914b1af0ecb0   redis:6.2-alpine3.14   "docker-entrypoint.s..."   2 days ago      Up 2 days      6379/tcp   raide1
886cd3d8c81e   nginx:1.21             "/docker-entrypoint...."   2 days ago      Up 2 days      80/tcp     socker1
[root@ct ~]# docker exec -it e86cc40e5351   /bin/bash
[root@e86cc40e5351 /]# ls    #There are also virtual files in the container of centos
anaconda-post.log  dev  home  lib64  mnt  proc  run   srv  tmp  var
bin                etc  lib   media  opt  root  sbin  sys  usr
[root@e86cc40e5351 /]# exit     #Sign out
exit


7. Container Export and Import

docker export    container ID   >   file name       #export
docker import  Exported File Name (Container) Specifies Mirror Name    #Import container file, regenerate mirror
[root@ct ~]# docker ps -a
CONTAINER ID   IMAGE                  COMMAND                  CREATED         STATUS         PORTS      NAMES
e86cc40e5351   centos:7               "/bin/bash"              5 minutes ago   Up 5 minutes              great_golick
1ce04b9f9cad   f6f2296798e9           "docker-entrypoint.s..."   2 days ago      Up 2 days      6379/tcp   nervous_dirac
914b1af0ecb0   redis:6.2-alpine3.14   "docker-entrypoint.s..."   2 days ago      Up 2 days      6379/tcp   raide1
886cd3d8c81e   nginx:1.21             "/docker-entrypoint...."   2 days ago      Up 2 days      80/tcp     socker1
[root@ct ~]# cd /opt/

[root@ct opt]# docker export 886cd3d8c81e > nginx-1.21    #Generate Files
[root@ct opt]# ls
 nginx-1.21

[root@ct opt]# docker stop  886cd3d8c81e   #Stop Container and Delete Container
886cd3d8c81e
[root@ct opt]# docker rm  886cd3d8c81e
886cd3d8c81e
[root@ct opt]# docker rmi nginx:1.21    #delete mirror
Untagged: nginx:1.21
Deleted: sha256:0b4bf752f838f3b775546fd465d4afa64dd04ccb9e6ebca10ada8a10aeefef42
Deleted: sha256:109b539aca08b0ca21c16a38493491ee223a75e2b9738ac7145150155eec36db

[root@ct opt]# docker import nginx-1.21 nginx:1.21  #Re-injection
sha256:0b4bf752f838f3b775546fd465d4afa64dd04ccb9e6ebca10ada8a10aeefef42
[root@ct opt]# docker ps -a
CONTAINER ID   IMAGE                  COMMAND                  CREATED         STATUS         PORTS      NAMES
e86cc40e5351   centos:7               "/bin/bash"              8 minutes ago   Up 8 minutes              great_golick
1ce04b9f9cad   f6f2296798e9           "docker-entrypoint.s..."   2 days ago      Up 2 days      6379/tcp   nervous_dirac
914b1af0ecb0   redis:6.2-alpine3.14   "docker-entrypoint.s..."   2 days ago      Up 2 days      6379/tcp   raide1
[root@ct opt]# docker images    #Mirror reappears
REPOSITORY    TAG              IMAGE ID       CREATED         SIZE
nginx         1.21             8345d48257de   4 seconds ago   132MB
redis         6.2-alpine3.14   f6f2296798e9   4 days ago      32.3MB
busybox       latest           42b97d3c2ae9   11 days ago     1.24MB
nginx         lnmp             dd34e67e3371   2 weeks ago     133MB

8. Delete Containers

docker rm container ID
docker rm -f container ID    #Force Delete

docker ps -a | awk '{print "docker rm "$1}' | bash
#Bulk Delete Container

docker rm `docker ps -aq`
 #Bulk Delete Containers Not Running, -f Strongly Delete All

for i in 'docker ps -a | grep -i exit | awk '{print $1}'`; do docker rm -f $i; done
#Bulk Delete Containers in'exit'State

2. Network mode of docker

1. host mode

Containers will not virtual out their own network cards, configure their own IP, etc., but will use the host's IP and port
If the host mode is used when starting a container, the container will not get a separate Network Namespace but will share a Network Namespace with the host.Containers will not virtual out their own network cards, configure their own IP, etc., but use the host's IP and port.However, other aspects of the container, such as the file system, process list, and so on, are isolated from the host.

Containers using host mode can communicate with the outside world directly using the host's IP address. Service ports inside the container can also use the host's port without NAT. The most advantage of hosts is that network performance is better, but ports already used on docker host can no longer be used and network isolation is poor.

2 container:

The container created does not create its own network card, set IP, etc. but shares IP, port range with a specified container
This mode specifies that the newly created container and an existing container share a network namespace instead of sharing with the host. The newly created container does not create its own network card, configure its own IP, but shares IP, port range, and so on with a specified container.Similarly, the two containers are isolated except for network aspects, such as file systems, process lists.Processes from both containers can communicate through loo network card devices

3. None mode

This mode turns off the network functionality of the container
In this network mode, the container has only lo loopback network ports and no other network cards.None mode can be specified by the -network=none parameter when a container is created
This type of network cannot be connected to the network, but a closed network provides a good security of the container.

4. Bridge mode

This mode assigns, sets IP, etc. to each container and connects the container to a docker virtual bridge. The table configuration communicates with the host through the docker 0 bridge and the nat of iptables
When the Docker process starts, a virtual network bridge named docker0 is created on the host, and the Docker container started on the host connects to the virtual network bridge.Virtual bridges work like physical switches so that all containers on the host are connected to a two-tier network through the switches.
Assign an IP from the docker0 subnet to the container for use, and set the IP address of the docker0 as the default gateway for the container.Create a pair of virtual network card veth pair devices on the host. Docker will place one end of the veth pair device in the newly created container, named etho (the container's network card), and the other end in the host, named vethxxx, with a similar name, and add this network device to the docker0 bridge.You can view it through the brctl show command.
The bridge mode is the docker's default network mode, and does not write the -net parameter, it is the bridge mode.When using docker run-p, docker actually makes DNA T rules in iptables to implement port forwarding function.You can view it using jptables. -t nat-vnL.

Tags: Operation & Maintenance Docker

Posted on Sat, 04 Sep 2021 12:25:26 -0400 by rilana