Docker basic usage

Posted by webweever on Tue, 11 Jan 2022 01:37:21 +0100

What is docker!

Docker's idea comes from the container. What problem does the container solve? In a large ship, the goods can be placed neatly. And all kinds of goods are standardized by containers, and containers will not affect each other. Then I don't need a ship for fruit and a ship for chemicals. As long as the goods are well sealed in containers, I can take them all away in a big ship

OCI!!

  • Led by the Linux foundation, it was founded in June 2015
  • It aims to develop an open industrial standard around container format and runtime
  • Contains two specifications
    • Runtime spec
    • Image spec

OCF!!

runC is a CLI tool for generating and running containers according to the OCI specification

  • The container is started as a child process of runC, and can be embedded in various other systems without running a daemon
  • runC is built on libcontainer. The same container technology supports the installation of numerous docker engines. Docker provides a site dedicated to container images: https://hub.docker.com

docker architecture!!

  • Generally, the client and server are on the same host
  • The client side uses various docker commands to manage containers
  • There can be multiple containers, images and local storage on the server side
  • The registry can provide images of different system types

Workflow

First, the client uses the docker command

  • After the server detects the command, first check whether there is an image in the local area
    -If there is no mirror to operate on, you can start from hub Pull image from docker to local
  • At this time, if the local image already has the image to be operated, the operation can be performed directly

How docker works

  • Docker is a C/S architecture, which is used to manage containers. The daemon of docker runs on the host and communicates through socket.

  • The server receives the instruction from the client through the socket, and will execute the command after receiving the instruction. docker establishes the boundary of the container by modifying the system call parameters of namespaces.

  • docker uses CGroups (control groups) to manage (restrict) the resource occupation of a container to avoid resource preemption between containers.

docker image and image warehouse

  • tag is the version number of the application
  • Latest is the latest version
  • Stable is a stable version
  • The image is static, while the container is dynamic. The container has its life cycle. The relationship between the image and the container is similar to that between the program and the process. The image is similar to the program file in the file system, while the container is similar to the state in which a program runs, that is, the process. Therefore, containers can be deleted. After a container is deleted, its image will not be deleted.

docker object

When you use docker, you can create, use, and manage images, containers, networks, storage volumes, plug-ins, and other objects.

  • image

    - A mirror is a read-only template used to create containers
    - Usually, the creation of one image is based on another system image, and some customized operations are carried out, such as installing and starting a service
    - You can create your own image or use the image created by others to pull it locally (it is recommended to use the official image)
    
  • container

    - A container is a running image
    - Can be in API Interface or on the command line to create, run, stop, move, and delete your container
    - You can connect containers to one or more networks, attach storage volumes to them, and even create a new mirror based on the current state
    

docker installation

[root@master ~]# cd /etc/yum.repos.d/
[root@master yum.repos.d]# vim docker-ce.repo
[root@master yum.repos.d]# cat docker-ce.repo
[docker-ce]
name=docker-ce
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/8/x86_64/stable/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
enabled=1

[root@master yum.repos.d]# dnf clean all
26 File deleted
[root@master yum.repos.d]# dnf -y install docker-ce
CentOS Stream 8 - AppStream                                                                                112 kB/s |  12 MB     01:48    
CentOS Stream 8 - BaseOS                                                                                    99 kB/s | 7.7 MB     01:19    
CentOS Stream 8 - Extras                                                                                   1.3 kB/s |  14 kB     00:10    
docker-ce                                                                                                  2.1 kB/s |  14 kB     00:06    
Salt repo for RHEL/CentOS 8 PY3                                                                             12 kB/s | 242 kB     00:20    

docker acceleration

The configuration file of docker CE is / etc/docker / daemon JSON, this file does not exist by default. We need to create and configure it manually. The / etc/docker directory is generated after docker is started, and the acceleration of docker is realized by configuring this file.

docker can be accelerated in many ways:

  • docker cn
  • Accelerator of China University of science and technology
  • Alicloud accelerator (you need to register an account through alicloud Developer Platform and use your own accelerator for free)
[root@master ~]# cd /etc/docker/
[root@master docker]# ls
key.json
[root@master docker]# vim deamon.json
[root@master docker]# cat deamon.json
{
          "registry-mirrors": ["https://5ryw5lau.mirror.aliyuncs.com"]
}
[root@master docker]# systemctl daemon-reload
[root@master docker]# systemctl restart docker

docker common operations

commandfunction
docker searchUsed to find images on docker hub
docker pullUsed to pull the specified image on the docker
docker imagesUsed to view the current system image information
docker createUsed to create containers
docker startUsed to start one or more containers
docker runWhen there is no local image, automatically pull the image and create and start the container
docker attachUsed to enter a specified container
docker psUsed to view information about containers in the system
docker logsUsed to view the logs of containers in the system
docker restartUsed to restart the specified container
docker stopUsed to stop the specified container
docker killThe process used to kill the specified container
docker rmUsed to delete containers
docker execUsed to enter a specified container and execute commands
docker infoUsed to view the details of the docker tool
docker inspectUsed to view the details of a specified object

docker search

[root@master docker]# docker search httpd
NAME                                    DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
httpd                                   The Apache HTTP Server Project                  3598      [OK]       
centos/httpd-24-centos7                 Platform for running Apache httpd 2.4 or bui...   40                   
centos/httpd                                                                            34                   [OK]
arm64v8/httpd                           The Apache HTTP Server Project                  7                    
polinux/httpd-php                       Apache with PHP in Docker (Supervisor, CentO...   5                    [OK]
solsson/httpd-openidc                   mod_auth_openidc on official httpd image, ve...   2                    [OK]
hypoport/httpd-cgi                      httpd-cgi                                       2                    [OK]
centos/httpd-24-centos8                                                                 1                    
clearlinux/httpd                        httpd HyperText Transfer Protocol (HTTP) ser...   1                    
inanimate/httpd-ssl                     A play container with httpd, ssl enabled, an...   1                    [OK]
dockerpinata/httpd                                                                      1                    
dariko/httpd-rproxy-ldap                Apache httpd reverse proxy with LDAP authent...   1                    [OK]
manageiq/httpd                          Container with httpd, built on CentOS for Ma...   1                    [OK]
jonathanheilmann/httpd-alpine-rewrite   httpd:alpine with enabled mod_rewrite           1                    [OK]
publici/httpd                           httpd:latest                                    1                    [OK]
lead4good/httpd-fpm                     httpd server which connects via fcgi proxy h...   1                    [OK]
interlutions/httpd                      httpd docker image with debian-based config ...   0                    [OK]
appertly/httpd                          Customized Apache HTTPD that uses a PHP-FPM ...   0                    [OK]
amd64/httpd                             The Apache HTTP Server Project                  0                    
manasip/httpd                                                                           0                    
manageiq/httpd_configmap_generator      Httpd Configmap Generator                       0                    [OK]
trollin/httpd                                                                           0                    
itsziget/httpd24                        Extended HTTPD Docker image based on the off...   0                    [OK]
ysli/httpd                              Httpd for DeepWeb                               0                    [OK]
e2eteam/httpd                                                                           0                    

docker pull

[root@master docker]# docker pull httpd
Using default tag: latest
latest: Pulling from library/httpd
33847f680f63: Pull complete 
d74938eee980: Pull complete 
963cfdce5a0c: Pull complete 
8d5a3cca778c: Pull complete 
e06a573b193b: Pull complete 
Digest: sha256:61e49dd08a51d6fc421ed257bd8eb461cf2d48269d9ab2b4ff5d4c69826c3c9c
Status: Downloaded newer image for httpd:latest
docker.io/library/httpd:latest

docker images

[root@master docker]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED      SIZE
httpd        latest    73b8cfec1155   5 days ago   138MB

docker create

[root@master docker]# docker create httpd
9e84110fa7fdf26df44d3655469115b5cbf8b77f7247413a83472ede7f44be75

docker ps

[root@master docker]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@master docker]# docker ps -a
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS    PORTS     NAMES
9e84110fa7fd   httpd     "httpd-foreground"   51 seconds ago   Created             gallant_swartz

docker start

[root@master docker]# docker start 9e84110fa7fd
9e84110fa7fd
[root@master docker]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED              STATUS          PORTS     NAMES
9e84110fa7fd   httpd     "httpd-foreground"   About a minute ago   Up 16 seconds   80/tcp    gallant_swartz

docker run

[root@master docker]# docker run httpd
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. Set the 'ServerName' directive globally to suppress this message
[Wed Jul 28 04:06:47.644621 2021] [mpm_event:notice] [pid 1:tid 140665671206016] AH00489: Apache/2.4.48 (Unix) configured -- resuming normal operations
[Wed Jul 28 04:06:47.644815 2021] [core:notice] [pid 1:tid 140665671206016] AH00094: Command line: 'httpd -D FOREGROUND'


[root@master ~]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS              PORTS     NAMES
85e481d357ba   httpd     "httpd-foreground"   26 seconds ago   Up 25 seconds       80/tcp    sad_jemison
9e84110fa7fd   httpd     "httpd-foreground"   3 minutes ago    Up About a minute   80/tcp    gallant_swartz


# When ctrl+c terminates, the container will also stop
[root@master ~]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED         STATUS              PORTS     NAMES
9e84110fa7fd   httpd     "httpd-foreground"   3 minutes ago   Up About a minute   80/tcp    gallant_swartz

docker attach

[root@master ~]# docker attach 9e84110fa7fd
^C[Wed Jul 28 04:08:46.947770 2021] [mpm_event:notice] [pid 1:tid 140190087959680] AH00491: caught SIGTERM, shutting down
[root@master ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

docker logs

[root@master docker]# docker start 9e84110fa7fd
9e84110fa7fd
[root@master docker]# docker logs 9e84110fa7fd
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
[Wed Jul 28 04:05:45.499920 2021] [mpm_event:notice] [pid 1:tid 140190087959680] AH00489: Apache/2.4.48 (Unix) configured -- resuming normal operations
[Wed Jul 28 04:05:45.500715 2021] [core:notice] [pid 1:tid 140190087959680] AH00094: Command line: 'httpd -D FOREGROUND'
[Wed Jul 28 04:08:46.947770 2021] [mpm_event:notice] [pid 1:tid 140190087959680] AH00491: caught SIGTERM, shutting down
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
[Wed Jul 28 04:10:02.414826 2021] [mpm_event:notice] [pid 1:tid 140048856097920] AH00489: Apache/2.4.48 (Unix) configured -- resuming normal operations
[Wed Jul 28 04:10:02.415084 2021] [core:notice] [pid 1:tid 140048856097920] AH00094: Command line: 'httpd -D FOREGROUND'

docker stop

[root@master docker]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED         STATUS              PORTS     NAMES
9e84110fa7fd   httpd     "httpd-foreground"   6 minutes ago   Up About a minute   80/tcp    gallant_swartz
[root@master docker]# docker stop 9e84110fa7fd
9e84110fa7fd
[root@master docker]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

docker rm

[root@master ~]# docker rm 9e84110fa7fd
9e84110fa7fd
[root@master ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND              CREATED         STATUS                     PORTS     NAMES
85e481d357ba   httpd     "httpd-foreground"   8 minutes ago   Exited (0) 7 minutes ago             sad_jemison

docker event state