Introduction to Docker foundation of container technology

Keywords: Linux Docker Nginx Ubuntu JSON

We have learned the basic usage of LXC and the simple use of LXC WEB Panel, which can be referred to by interested friends https://www.cnblogs.com/qiuhom-1874/p/12904188.html Today's essay is mainly about the basic knowledge of docker;

I. docker and LXC

First let's talk about the difference between docker and traditional LXC containers. The traditional LXC is to virtualize the resources of different containers of the kernel in the way of namespace, so that the resources of each container are isolated from each other. In the front we also mentioned that LXC is just a client tool of the container; the real implementation of the container is the kernel function; while docker and LXC are not essentially different, they are both the client tools of the container; LXC uses the kernel The function isolates resources between different containers, while docker is another encapsulation on LXC. LXC relies on a template when creating containers, while docker relies on image when creating containers;

It can be seen from the above figure that there are many processes running in LXC container, and docker is a container running a process and its sub processes; LXC is more like a system level container, while docker is more like a process level container or an application level container;

There is usually only one process and its sub processes in the docker container, usually the process number of the process is 1, which means that if the process number of 1 in the docker container goes down, then the container will go down; the image of docker is implemented in a way of "layered construction, joint mount"; the image of different functions is through certain The hierarchy is mounted to form a new image. When docker is started as a container, it will add a writable layer to the top of the image. This allows us to modify some data inside the container. Saving the modified data is only valid for the current container. If the same image is used to start as another container, the modified data in the subsequent container is The reason is that each layer of the image is read-only; only when the image is running as a container can there be a writable layer, and the writable layer only works for the container;

II. docker architecture

Docker is a C/S architecture. For clients, it is mainly docker commands and other tools that call docker API; for servers, it is mainly docker daemons; clients and servers communicate based on http or https protocol; unlike other traditional services, docker servers do not listen on a socket of TCP or UDP; it usually listens on a UNIX socket On socket (default is / var/run/docker.sock ), which also means that the client and the server must be on the same host; the process of running the docker container is as follows; the client sends the command to the docker server through API call or command typing through http or https protocol, and the docker server first checks whether there is a corresponding version of mirror in the local warehouse after receiving the command For example, if there is one, it will directly run as a container; if there is no one, it will go to the specified rigistry (the warehouse where the image warehouse is stored) to find the corresponding image warehouse, and then find the corresponding version of the image, download it to the local warehouse through the https protocol, and then start it as a container; if the default does not specify the address of rigistry, the default is to dockerhub.com Find the corresponding version image in the corresponding image warehouse. If you do not specify the version of the image, the latest version will be found by default. From the above process, you can see that docker runs as a container, and the core is the image. Without the image, it is impossible to run the container. We can understand the image as an application, just like we want to run an application on a computer In fact, the image of docker is to package the application and the libraries, files and environments that the application depends on;

III. installation of docker

1. Configure the yum warehouse. By default, there are docker packages in the extras Library of centos7, but the version is too low. If you want to use the newer version of docker, you can use the Alibaba cloud image warehouse. For the configuration method, please refer to https://developer.aliyun.com/mirror/docker-ce?spm=a2c6h.13651102.0.0.3e221b11mqdEEz

2. Install docker CE

[root@node1 yum.repos.d]# yum install -y docker-ce
Loaded plugins: fastestmirror
docker-ce-stable                                         | 3.5 kB     00:00     
docker-ce-stable/x86_64/primary_db                         |  42 kB   00:00     
Loading mirror speeds from cached hostfile
 * base: mirrors.cn99.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package docker-ce.x86_64 3:19.03.9-3.el7 will be installed
......Omit part of the content
Installed:
  docker-ce.x86_64 3:19.03.9-3.el7                                              

Dependency Installed:
  audit-libs-python.x86_64 0:2.8.5-4.el7                                        
  checkpolicy.x86_64 0:2.5-8.el7                                                
  container-selinux.noarch 2:2.119.1-1.c57a6f9.el7                              
  containerd.io.x86_64 0:1.2.13-3.2.el7                                         
  docker-ce-cli.x86_64 1:19.03.9-3.el7                                          
  libcgroup.x86_64 0:0.41-21.el7                                                
  libsemanage-python.x86_64 0:2.5-14.el7                                        
  policycoreutils-python.x86_64 0:2.5-34.el7                                    
  python-IPy.noarch 0:0.75-6.el7                                                
  setools-libs.x86_64 0:3.3.8-4.el7                                             

Dependency Updated:
  audit.x86_64 0:2.8.5-4.el7                                                    
  audit-libs.x86_64 0:2.8.5-4.el7                                               
  libselinux.x86_64 0:2.5-15.el7                                                
  libselinux-python.x86_64 0:2.5-15.el7                                         
  libselinux-utils.x86_64 0:2.5-15.el7                                          
  libsemanage.x86_64 0:2.5-14.el7                                               
  libsepol.x86_64 0:2.5-10.el7                                                  
  policycoreutils.x86_64 0:2.5-34.el7                                           
  selinux-policy.noarch 0:3.13.1-266.el7                                        
  selinux-policy-targeted.noarch 0:3.13.1-266.el7                               

Complete!
[root@node1 yum.repos.d]# 

3. Start docker

[root@node1 yum.repos.d]# systemctl start docker

4. Add docker accelerator

[root@node1 yum.repos.d]# cd /etc/docker/
[root@node1 docker]# cat >> daemon.json << EOF
> {
>   "registry-mirrors": ["https://cyr1uljt.mirror.aliyuncs.com","https://registry.docker-cn.com"]
> }
> EOF
[root@node1 docker]# cat daemon.json 
{
  "registry-mirrors": ["https://cyr1uljt.mirror.aliyuncs.com","https://registry.docker-cn.com"]
}
[root@node1 docker]# systemctl daemon-reload
[root@node1 docker]# systemctl restart docker
[root@node1 docker]

4. Verify the version of docker and accelerator address

[root@node1 docker]# docker version
Client: Docker Engine - Community
 Version:           19.03.9
 API version:       1.40
 Go version:        go1.13.10
 Git commit:        9d988398e7
 Built:             Fri May 15 00:25:27 2020
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.9
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.13.10
  Git commit:       9d988398e7
  Built:            Fri May 15 00:24:05 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.2.13
  GitCommit:        7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683
[root@node1 docker]# docker info
Client:
 Debug Mode: false

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 0
 Server Version: 19.03.9
 Storage Driver: overlay2
  Backing Filesystem: xfs
  Supports d_type: true
  Native Overlay Diff: true
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 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: runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc version: dc9208a3303feef5b3839f4323d9beb36df0a9dd
 init version: fec3683
 Security Options:
  seccomp
   Profile: default
 Kernel Version: 3.10.0-693.el7.x86_64
 Operating System: CentOS Linux 7 (Core)
 OSType: linux
 Architecture: x86_64
 CPUs: 4
 Total Memory: 3.686GiB
 Name: node1
 ID: T6OZ:HBP6:Q5UV:RVBY:RCHM:DX3A:4JWY:QY55:OUEX:NRMN:BMRZ:UGCU
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Registry Mirrors:
  https://cyr1uljt.mirror.aliyuncs.com/
  https://registry.docker-cn.com/
 Live Restore Enabled: false

[root@node1 docker]# 

Tip: you can see that the version of docker we installed is 19.03.9; the two accelerators added to it appear in registry mirrors; the installation and configuration of this docker are basically completed; next, let's talk about the use of docker;

IV. docker image

1. Get the image

We said that to run docker, we must have an image, which is one of the core of docker container. How can we download the image in the warehouse? The command docker image pull directly downloads images from the dockerhub warehouse to the local area. First, let's take a look at the help document of the command docker image pull

[root@node1 ~]# docker image pull --help

Usage:  docker image pull [OPTIONS] NAME[:TAG|@DIGEST]

Pull an image or a repository from a registry

Options:
  -a, --all-tags                Download all tagged images in the repository
      --disable-content-trust   Skip image verification (default true)
      --platform string         Set platform if server is multi-platform
                                capable
  -q, --quiet                   Suppress verbose output
[root@node1 ~]# 

Tip: from the help information above, we can see that this command must be followed by the name of an image, which means downloading the image with the specified name from the dockerhub warehouse to the local. The name of the image is name:tag If only the image name is written, the latest version of the image will be downloaded by default; where - a means to download all the images in the specified image warehouse Image; -- Disable content trust indicates that the verification of ignoring image is ignored by default; - q indicates quiet mode and does not output redundant download information;

Download nginx image

[root@node1 ~]# docker image pull nginx:stable-alpine
stable-alpine: Pulling from library/nginx
cbdbe7a5bc2a: Pull complete 
6ade829cd166: Pull complete 
Digest: sha256:2668e65e1a36a749aa8b3a5297eee45504a4efea423ec2affcbbf85e31a9a571
Status: Downloaded newer image for nginx:stable-alpine
docker.io/library/nginx:stable-alpine
[root@node1 ~]# 

Tip: the above command means downloading the image of nginx's stable Alpine version from the dockerhub warehouse to the local. docker image pull is the same as docker pull, the latter is the use of the old version. In order to be compatible with the old version of docker commands, the new version of docker retains the use of the old version. If you want to download images from the unofficial image warehouse, you can manually specify the address of other image warehouses. As follows

[root@node1 ~]# docker image pull hub.c.163.com/public/ubuntu:14.04       
14.04: Pulling from public/ubuntu
f4ab0b34ba6a: Pull complete 
2f0787dc0bfe: Pull complete 
54c427d77362: Pull complete 
51e87d5424ff: Pull complete 
5dfa314e7290: Pull complete 
a3ed95caeb02: Pull complete 
Digest: sha256:ffc2fc66f8e0bfa4b417b817054d3ebec130c8db44342b8fa394e25779633257
Status: Downloaded newer image for hub.c.163.com/public/ubuntu:14.04
hub.c.163.com/public/ubuntu:14.04
[root@node1 ~]# 

View the images of the local warehouse

[root@node1 ~]# docker image ls 
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
nginx                         stable-alpine       ab94f84cc474        3 weeks ago         21.3MB
hub.c.163.com/public/ubuntu   14.04               2fe5c4bba1f9        4 years ago         237MB
[root@node1 ~]# docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
nginx                         stable-alpine       ab94f84cc474        3 weeks ago         21.3MB
hub.c.163.com/public/ubuntu   14.04               2fe5c4bba1f9        4 years ago         237MB
[root@node1 ~]# 

Tip: docker image ls is to view the images in the local warehouse and list the existing local images. It is the same as docker images. From the above information, we can see that there are two local images

Label image

[root@node1 ~]# docker image tag --help

Usage:  docker image tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
[root@node1 ~]#

Tip: the docker image tag command is the same as docker tag

[root@node1 ~]# docker image ls
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
nginx                         stable-alpine       ab94f84cc474        3 weeks ago         21.3MB
hub.c.163.com/public/ubuntu   14.04               2fe5c4bba1f9        4 years ago         237MB
[root@node1 ~]# docker image tag hub.c.163.com/public/ubuntu:14.04 163.com/ubuntu:14.04
[root@node1 ~]# docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
nginx                         stable-alpine       ab94f84cc474        3 weeks ago         21.3MB
163.com/ubuntu                14.04               2fe5c4bba1f9        4 years ago         237MB
hub.c.163.com/public/ubuntu   14.04               2fe5c4bba1f9        4 years ago         237MB
[root@node1 ~]# docker tag 163.com/ubuntu:14.04 ub:14.04
[root@node1 ~]# docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
nginx                         stable-alpine       ab94f84cc474        3 weeks ago         21.3MB
163.com/ubuntu                14.04               2fe5c4bba1f9        4 years ago         237MB
ub                            14.04               2fe5c4bba1f9        4 years ago         237MB
hub.c.163.com/public/ubuntu   14.04               2fe5c4bba1f9        4 years ago         237MB
[root@node1 ~]# 

Tip: the essence of labeling is to alias the image. From the above information, we can see that the ID s of 163.com/ubuntu, ub and hub.c.163.com/ubuntu are the same, which means that the three images point to the same image;

View image details

[root@node1 ~]# docker image inspect nginx:stable-alpine
[
    {
        "Id": "sha256:ab94f84cc4744a5addf3c896206e1e666abde0eadc26990d640395cfacae24a5",
        "RepoTags": [
            "nginx:stable-alpine"
        ],
        "RepoDigests": [
            "nginx@sha256:2668e65e1a36a749aa8b3a5297eee45504a4efea423ec2affcbbf85e31a9a571"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2020-04-24T12:59:54.146056172Z",
        "Container": "5cdc5c83fb963292b8ae3ad7abae5069a88c76e51ae91dfb9f9d7fa604f088e6",
        "ContainerConfig": {
            "Hostname": "5cdc5c83fb96",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "NGINX_VERSION=1.18.0",
                "NJS_VERSION=0.4.0",
                "PKG_RELEASE=1"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"nginx\" \"-g\" \"daemon off;\"]"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:5d84a370ed482284aad8af6a5ea7bcca5a91c5bf7f8e061f2d336835cd724e6e",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
            },
            "StopSignal": "SIGTERM"
        },
        "DockerVersion": "18.09.7",
        "Author": "",
        "Config": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "NGINX_VERSION=1.18.0",
                "NJS_VERSION=0.4.0",
                "PKG_RELEASE=1"
            ],
            "Cmd": [
                "nginx",
                "-g",
                "daemon off;"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:5d84a370ed482284aad8af6a5ea7bcca5a91c5bf7f8e061f2d336835cd724e6e",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
            },
            "StopSignal": "SIGTERM"
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 21314634,
        "VirtualSize": 21314634,
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/e175dd426f8fc3b61248f51b13ebbcc0130de67cd0b604e6e5c2f4b5776b5568/diff",
                "MergedDir": "/var/lib/docker/overlay2/eda604afa15f728fe10851129414b80826b909d1741a969eb3587dcd50bfe0da/merged",
                "UpperDir": "/var/lib/docker/overlay2/eda604afa15f728fe10851129414b80826b909d1741a969eb3587dcd50bfe0da/diff",
                "WorkDir": "/var/lib/docker/overlay2/eda604afa15f728fe10851129414b80826b909d1741a969eb3587dcd50bfe0da/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:3e207b409db364b595ba862cdc12be96dcdad8e36c59a03b7b3b61c946a5741a",
                "sha256:34cf0e74977f5cabe21fd5465978f35ec87b57ed928745a0f2054bc7e1dcbcf9"
            ]
        },
        "Metadata": {
            "LastTagTime": "0001-01-01T00:00:00Z"
        }
    }
]
[root@node1 ~]#

Tip: using the docker image inspect ion command, you can get the details of the specified image, including the creator, architecture, summary information of each level, and so on. This command is equivalent to docker inspect: the above command returns a json format data, which is inconvenient for us to view the information of a field. If we want to obtain the information of a field, we can use the - f option to specify the field to view; as follows

[root@node1 ~]# docker image inspect -f "{{.Config.Cmd}}" nginx:stable-alpine
[nginx -g daemon off;]
[root@node1 ~]# docker image inspect -f "{{.RootFS}}" nginx:stable-alpine          
{layers [sha256:3e207b409db364b595ba862cdc12be96dcdad8e36c59a03b7b3b61c946a5741a sha256:34cf0e74977f5cabe21fd5465978f35ec87b57ed928745a0f2054bc7e1dcbcf9] }
[root@node1 ~]# docker image inspect -f "{{.RootFS.Type}}" nginx:stable-alpine
layers
[root@node1 ~]# 

Note: to specify a field with - f, you need to start with "." and enclose the field name with double braces

View image history

[root@node1 ~]# docker image history  nginx:stable-alpine           
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
ab94f84cc474        3 weeks ago         /bin/sh -c #(nop)  CMD ["nginx" "-g" "daemon...   0B                  
<missing>           3 weeks ago         /bin/sh -c #(nop)  STOPSIGNAL SIGTERM           0B                  
<missing>           3 weeks ago         /bin/sh -c #(nop)  EXPOSE 80                    0B                  
<missing>           3 weeks ago         /bin/sh -c set -x     && addgroup -g 101 -S ...   15.7MB              
<missing>           3 weeks ago         /bin/sh -c #(nop)  ENV PKG_RELEASE=1            0B                  
<missing>           3 weeks ago         /bin/sh -c #(nop)  ENV NJS_VERSION=0.4.0        0B                  
<missing>           3 weeks ago         /bin/sh -c #(nop)  ENV NGINX_VERSION=1.18.0     0B                  
<missing>           3 weeks ago         /bin/sh -c #(nop)  LABEL maintainer=NGINX Do...   0B                  
<missing>           4 weeks ago         /bin/sh -c #(nop)  CMD ["/bin/sh"]              0B                  
<missing>           4 weeks ago         /bin/sh -c #(nop) ADD file:b91adb67b670d3a6f...   5.61MB              
[root@node1 ~]# 

Tip: the above command indicates view nginx:stable-alpine History of this image. This command shows the operations of each layer image. The creation information of each layer image. This command is equivalent to docker history;

Search image

[root@node1 ~]# docker search nginx         
NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
nginx                              Official build of Nginx.                        13212               [OK]                
jwilder/nginx-proxy                Automated Nginx reverse proxy for docker con...   1806                                    [OK]
richarvey/nginx-php-fpm            Container running Nginx + PHP-FPM capable of...   775                                     [OK]
linuxserver/nginx                  An Nginx container, brought to you by LinuxS...   113                                     
bitnami/nginx                      Bitnami nginx Docker Image                      83                                      [OK]
tiangolo/nginx-rtmp                Docker image with Nginx using the nginx-rtmp...   73                                      [OK]
jc21/nginx-proxy-manager           Docker container for managing Nginx proxy ho...   61                                      
nginxdemos/hello                   NGINX webserver that serves a simple page co...   49                                      [OK]
jlesage/nginx-proxy-manager        Docker container for Nginx Proxy Manager        43                                      [OK]
nginx/nginx-ingress                NGINX Ingress Controller for Kubernetes         31                                      
privatebin/nginx-fpm-alpine        PrivateBin running on an Nginx, php-fpm & Al...   25                                      [OK]
schmunk42/nginx-redirect           A very simple container to redirect HTTP tra...   18                                      [OK]
nginxinc/nginx-unprivileged        Unprivileged NGINX Dockerfiles                  16                                      
centos/nginx-112-centos7           Platform for running nginx 1.12 or building ...   13                                      
centos/nginx-18-centos7            Platform for running nginx 1.8 or building n...   13                                      
blacklabelops/nginx                Dockerized Nginx Reverse Proxy Server.          13                                      [OK]
raulr/nginx-wordpress              Nginx front-end for the official wordpress:f...   12                                      [OK]
nginx/nginx-prometheus-exporter    NGINX Prometheus Exporter                       12                                      
sophos/nginx-vts-exporter          Simple server that scrapes Nginx vts stats a...   7                                       [OK]
mailu/nginx                        Mailu nginx frontend                            6                                       [OK]
bitwarden/nginx                    The Bitwarden nginx web server acting as a r...   5                                       
bitnami/nginx-ingress-controller   Bitnami Docker Image for NGINX Ingress Contr...   5                                       [OK]
wodby/nginx                        Generic nginx                                   1                                       [OK]
ansibleplaybookbundle/nginx-apb    An APB to deploy NGINX                          1                                       [OK]
centos/nginx-110-centos7           Platform for running nginx 1.10 or building ...   0                                       
[root@node1 ~]# docker search  -s 500 nginx 
Flag --stars has been deprecated, use --filter=stars=3 instead
NAME                      DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
nginx                     Official build of Nginx.                        13212               [OK]                
jwilder/nginx-proxy       Automated Nginx reverse proxy for docker con...   1806                                    [OK]
richarvey/nginx-php-fpm   Container running Nginx + PHP-FPM capable of...   775                                     [OK]
[root@node1 ~]# docker search  -s 5000 nginx
Flag --stars has been deprecated, use --filter=stars=3 instead
NAME                DESCRIPTION                STARS               OFFICIAL            AUTOMATED
nginx               Official build of Nginx.   13212               [OK]                
[root@node1 ~]# 

Prompt: - s indicates to display the image of more than a specified number of stars

Delete image

[root@node1 ~]# docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
nginx                         stable-alpine       ab94f84cc474        3 weeks ago         21.3MB
ub                            14.04               2fe5c4bba1f9        4 years ago         237MB
hub.c.163.com/public/ubuntu   14.04               2fe5c4bba1f9        4 years ago         237MB
163.com/ubuntu                14.04               2fe5c4bba1f9        4 years ago         237MB
[root@node1 ~]# docker image rm 163.com/ubuntu:14.04
Untagged: 163.com/ubuntu:14.04
[root@node1 ~]# docker image rm ub:14.04            
Untagged: ub:14.04
[root@node1 ~]# docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
nginx                         stable-alpine       ab94f84cc474        3 weeks ago         21.3MB
hub.c.163.com/public/ubuntu   14.04               2fe5c4bba1f9        4 years ago         237MB
[root@node1 ~]# docker image rm hub.c.163.com/public/ubuntu:14.04
Untagged: hub.c.163.com/public/ubuntu:14.04
Untagged: hub.c.163.com/public/ubuntu@sha256:ffc2fc66f8e0bfa4b417b817054d3ebec130c8db44342b8fa394e25779633257
Deleted: sha256:2fe5c4bba1f935f179e83cd5354403d1231ffc9df9c1621967194410eaf8d942
Deleted: sha256:085e08b75e744804e23332efb202896749c4aff9433391a775607852d339c26e
Deleted: sha256:7f622f92c76a635c16f506bced3dc1641e9390a26ee9d8cb70f8616c3a21fbdf
Deleted: sha256:ae7b6867bb7b8f3a8235547e0634e7629b4008d807dc768064e760a91413a71f
Deleted: sha256:3eef796012f97f23ffdb11296f6b624a0db4b32eae986da589492b974e6eb596
Deleted: sha256:b268b9bd797e1d58a176fac2a338df41437e853beb5aaa8a4159227fc38c4b72
Deleted: sha256:89688d062a0607fb50d0955de8964659e66f1bb41164b2d2b473d1edd7d8af90
[root@node1 ~]# 

Tip: if the image has multiple labels, specifying the label name means deleting the label. The image will be completely deleted only if there is only one label for the image. The docker image rm command is the same as the docker rmi command. To delete an image, in addition to the labels you can specify, you can also specify the image ID to delete the image;

Runs the specified image as a container

[root@node1 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               stable-alpine       ab94f84cc474        3 weeks ago         21.3MB
[root@node1 ~]# docker run --name n1 -d nginx:stable-alpine
1b404faab5ba4af597e38a998ad21bca61fe99583234b68882931d766c34ebf3
[root@node1 ~]# docker ps
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS               NAMES
1b404faab5ba        nginx:stable-alpine   "nginx -g 'daemon of..."   6 seconds ago       Up 4 seconds        80/tcp              n1
[root@node1 ~]# 

Tip: to run the specified image as a container, you need to use docker run; -- name indicates a name for the container, - d indicates that it does not occupy the existing terminal and runs in the background; the docker ps command is to view the running container of the current system; this command is the same as docker container ls. If you want to view the running conditions of all containers of the current system, you can use docker run PS-A, the command will list the running state container and the stopping state container;

View the ip address of the container you just created

[root@node1 ~]# docker container inspect -f "{{.NetworkSettings.IPAddress}}"  n1 
172.17.0.2
[root@node1 ~]# 

Tip: the difference between image and container is that image is static, and container is called container when image is running. In other words, container is the instantiation product of image. To view the details of container, we need to use docker container inspect ion, which is the same as docker image inspect ion Similarly, if you don't use - f to filter the field information, it usually displays all the information in json format; at this time, we can use the browser to access the nginx service in the container we just started

[root@node1 ~]# elinks --dump http://172.17.0.2
                               Welcome to nginx!

   If you see this page, the nginx web server is successfully installed and
   working. Further configuration is required.

   For online documentation and support please refer to [1]nginx.org.
   Commercial support is available at [2]nginx.com.

   Thank you for using nginx.

References

   Visible links
   1. http://nginx.org/
   2. http://nginx.com/
[root@node1 ~]# curl -I http://172.17.0.2
HTTP/1.1 200 OK
Server: nginx/1.18.0
Date: Fri, 22 May 2020 09:00:56 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 21 Apr 2020 15:15:39 GMT
Connection: keep-alive
ETag: "5e9f0e1b-264"
Accept-Ranges: bytes

[root@node1 ~]# 

Tip: the container launched by default will connect the network to the docker0 bridge, which is a nat bridge. To access the application services in the container, you need to use the host machine where the docker0 bridge is located to access (local machine). In addition, you can also build agents or use other containers on the docker0 bridge or expose the service ports in the container to the host machine;

Stop container

[root@node1 ~]# docker ps 
CONTAINER ID        IMAGE                 COMMAND                  CREATED              STATUS              PORTS               NAMES
444eb763efe0        nginx:stable-alpine   "nginx -g 'daemon of..."   About a minute ago   Up About a minute   80/tcp              n2
1b404faab5ba        nginx:stable-alpine   "nginx -g 'daemon of..."   20 minutes ago       Up 20 minutes       80/tcp              n1
[root@node1 ~]# docker container stop n1
n1
[root@node1 ~]# docker ps
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS               NAMES
444eb763efe0        nginx:stable-alpine   "nginx -g 'daemon of..."   2 minutes ago       Up 2 minutes        80/tcp              n2
[root@node1 ~]# docker ps -a
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS                     PORTS               NAMES
444eb763efe0        nginx:stable-alpine   "nginx -g 'daemon of..."   2 minutes ago       Up 2 minutes               80/tcp              n2
1b404faab5ba        nginx:stable-alpine   "nginx -g 'daemon of..."   21 minutes ago      Exited (0) 7 seconds ago                       n1
[root@node1 ~]# 

Start container

[root@node1 ~]# docker ps 
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS               NAMES
444eb763efe0        nginx:stable-alpine   "nginx -g 'daemon of..."   2 minutes ago       Up 2 minutes        80/tcp              n2
[root@node1 ~]# docker container start n1
n1
[root@node1 ~]# docker ps
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS               NAMES
444eb763efe0        nginx:stable-alpine   "nginx -g 'daemon of..."   2 minutes ago       Up 2 minutes        80/tcp              n2
1b404faab5ba        nginx:stable-alpine   "nginx -g 'daemon of..."   22 minutes ago      Up 2 seconds        80/tcp              n1
[root@node1 ~]# 

Delete container

[root@node1 ~]# docker container ls
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS               NAMES
444eb763efe0        nginx:stable-alpine   "nginx -g 'daemon of..."   3 minutes ago       Up 3 minutes        80/tcp              n2
1b404faab5ba        nginx:stable-alpine   "nginx -g 'daemon of..."   23 minutes ago      Up About a minute   80/tcp              n1
[root@node1 ~]# docker container rm n1
Error response from daemon: You cannot remove a running container 1b404faab5ba4af597e38a998ad21bca61fe99583234b68882931d766c34ebf3. Stop the container before attempting removal or force remove
[root@node1 ~]# docker container stop n1
n1
[root@node1 ~]# docker ps -a
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS                     PORTS               NAMES
444eb763efe0        nginx:stable-alpine   "nginx -g 'daemon of..."   4 minutes ago       Up 4 minutes               80/tcp              n2
1b404faab5ba        nginx:stable-alpine   "nginx -g 'daemon of..."   23 minutes ago      Exited (0) 5 seconds ago                       n1
[root@node1 ~]# docker container rm n1
n1
[root@node1 ~]# docker ps -a
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS               NAMES
444eb763efe0        nginx:stable-alpine   "nginx -g 'daemon of..."   4 minutes ago       Up 4 minutes        80/tcp              n2
[root@node1 ~]# docker container ls
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS               NAMES
444eb763efe0        nginx:stable-alpine   "nginx -g 'daemon of..."   5 minutes ago       Up 5 minutes        80/tcp              n2
[root@node1 ~]# 

Tip: the running container cannot be deleted directly. You must stop the container first. If you want to delete the container without stopping the container, you can use the - f option to specify. Of course, we usually do not recommend this;

Posted by drag0n on Fri, 22 May 2020 06:47:21 -0700