Basic management of Docker container

Docker introduction

  • It's a lightweight "virtual machine"
  • Open source tools for running applications in Linux containers

The difference between Docker and virtual machine

  • Virtual machine is a physical machine, using virtualization technology, virtual out of multiple operating systems, each operating system is isolated. Docker is an open source application container engine. You still need to install the operating system on the computer first, and then install the docker container manager. Virtual machines are virtualized at the hardware level, while dockers are virtualized at the operating system level; virtual machines are operating systems built by simulating hardware

Using scenario of Docker

  • Packaged applications simplify deployment
  • Free migration from underlying hardware
  • For example: the server moves from Tencent cloud to Alibaba cloud

Docker core concepts

  • image
  • container
  • Warehouse

Two ways to install Docker in CentOS

  • Using curl to obtain the installation script of docker for installation
  • Use yum warehouse to install docker

Installation of docker

Set image source and install docker

[root@localhost ~]# yum install -y \
> yum-utils \                      //Set source tool
> device-mapper-persistent-data \  //Mapping tool
> lvm2
[root@localhost ~]# Yum config Manager -- add repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo / / load alicloud image source
[root@localhost ~]# Yum install docker ce - Y / / install the docker container
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# setenforce 0
[root@localhost ~]# systemctl start docker / / open the docker container
[root@localhost ~]# systemctl enable docker / / set boot to start automatically
[root@localhost ~]# ps aux | grep docker / / check whether the docker process is enabled
[root@localhost ~]# docker version / / view version
[root@localhost ~]# docker search nginx / / search the public image
[root@localhost ~]# docker pull nginx / / download the image

Image acceleration

  • Log in to alicloud website - console - Product Service - container image service - image accelerator - choose centos

    [root@localhost ~]# TEE / etc / docker / daemon. JSON < - 'EOF' / / image acceleration
    > {
    >   "registry-mirrors": ["https://3a8s9zx5.mirror.aliyuncs.com"]
    > }
    > EOF
    {
    "registry-mirrors": ["https://3a8s9zx5.mirror.aliyuncs.com"]
    }
    [root@localhost ~]# Systemctl daemon reload / / reload the Daemons
    [root@localhost ~]# systemctl restart docker / / restart the docker service
    [root@localhost ~]# docker pull nginx / / download the nginx image
    [root@localhost ~]# docker images / / view the downloaded image information
    REPOSITORY     TAG        IMAGE ID       CREATE        SIZE
    nginx                   latest     f7bb5701a33c   4 days ago    126MB

    Basic operation of docker image

  • Container 1: 192.168.80.12
  • Container 2: 192.168.80.13
    [root@localhost ~]# docker inspect f7bb5701a33c / / view the image information
    [root@localhost ~]# docker tag nginx:latest nginx:web / / add a new tag
    [root@localhost ~]# docker images / / view the image
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    nginx               latest              f7bb5701a33c        4 days ago          126MB
    nginx               web                 f7bb5701a33c        4 days ago          126MB
    //Regenerate a mirror image, the original image will not disappear
    [root@localhost ~]# docker images | grep web / / view the image labeled web
    nginx               web                 f7bb5701a33c        4 days ago          126MB
    [root@localhost ~]# docker rmi nginx:web / / delete the image, or directly follow the ID number
    Untagged: nginx:web
    [root@localhost ~]# docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    nginx               latest              f7bb5701a33c        4 days ago          126MB
    [root@localhost ~]# cd /opt/
    [root@localhost opt]# Docker save - O nginx nginx: latest / / save the image
    [root@localhost opt]# ls
    containerd  nginx  rh
    [root@localhost opt]# scp /opt/nginx root@192.168.80.13:/opt / / / remote copy to 13 server

    Open another virtual machine with docker (192.168.80.13)

    [root@localhost opt]# Docker load < nginx / / load image
    //Or use docker load --input to save the filename
    [root@localhost opt]# docker images / / view image information
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    nginx               latest              f7bb5701a33c        4 days ago          126MB

    Back to the original virtual machine (192.168.80.12)

    [root@localhost opt]# docker tag nginx:latest nginx:web / / modify tag
    [root@localhost opt]# docker login / / log in to docker (you need to register)
    Username:             //User name
    Password:             //Password
    [root@localhost opt]# docker push nginx:web / / upload public warehouse

    Basic operation of docker container

    [root@localhost opt]# docker create -it nginx:latest /bin/bash / / create a container based on the image - i keep the container's standard input open, - t let docker assign a pseudo terminal
    36fdfb0925ba040c094d585d70a3481bd450c7d39e6636ceeb10b5c1b9743593
    [root@localhost opt]# Docker PS - A / / - a lists the most recently started containers
    CONTAINER ID        IMAGE         COMMAND             CREATED             STATUS              PORTS               NAMES
    36fdfb0925ba        nginx:latest        "/bin/bash"         3 seconds ago       Created                                 mystifying_dijkstra
    [root@localhost opt]# docker start 36fdfb0925ba / / open the container
    36fdfb0925ba
    [root@localhost opt]# docker ps -a / / check that the container status is on
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
    36fdfb0925ba        nginx:latest        "/bin/bash"         16 minutes ago      Up 6 seconds        80/tcp              mystifying_dijkstra

    Operation steps

    [root@localhost opt]# docker search centos7 / / view the centos7 image
    [root@localhost opt]# docker pull paigeeworld/centos7 / / download the image
    [root@localhost opt]# docker images / / view the image
    REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
    nginx                 latest              f7bb5701a33c        4 days ago          126MB
    nginx                 web                 f7bb5701a33c        4 days ago          126MB
    paigeeworld/centos7   latest              4cbe7aa905e7        5 years ago         382MB
    [root@localhost opt]# docker create -it paigeeworld/centos7 /bin/bash / / create container
    c48649c8cee9124cb456be4f93882e6dff16f88ba45051731138142d99293dfe
    [root@localhost opt]# docker ps -a / / view container
    CONTAINER ID        IMAGE                 COMMAND             CREATED             STATUS                     PORTS               NAMES
    c48649c8cee9        paigeeworld/centos7   "/bin/bash"         4 seconds ago       Created                                        relaxed_curran
    36fdfb0925ba        nginx:latest          "/bin/bash"         24 minutes ago      Exited (0) 3 minutes ago                       mystifying_dijkstra
    [root@localhost opt]# docker start c48649c8cee9 / / open the container
    c48649c8cee9
    [root@localhost opt]# docker ps -a / / view container
    CONTAINER ID        IMAGE                 COMMAND             CREATED             STATUS                     PORTS               NAMES
    c48649c8cee9        paigeeworld/centos7   "/bin/bash"         34 seconds ago      Up 7 seconds                                   relaxed_curran
    36fdfb0925ba        nginx:latest          "/bin/bash"         24 minutes ago      Exited (0) 3 minutes ago

    Basic operation of container

    [root@localhost opt]# Docker run paigeeworld / centos7 / usr / bin / bash - C LS / / docker run directly downloads the image, creates the container, opens it, enters the container, executes the command, and exits
    bin
    boot
    dev
    etc
    home
    [root@localhost opt]# docker ps -a / / view container
    CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS                      PORTS               NAMES
    c48649c8cee9        paigeeworld/centos7   "/bin/bash"              9 minutes ago       Up 9 minutes                                    relaxed_curran
    [root@localhost opt]# docker exec -it c48649c8cee9 /bin/bash / / enter the container (must be open)
    bash-4.2# ls /
    bin   dev  home  lib64       media  opt   root  sbin  sys  usr
    boot  etc  lib   lost+found  mnt    proc  run   srv   tmp  var
    bash-4.2# Exit / / exit the container
    exit
    [root@localhost opt]# docker ps -a / / at this time, the container is still open
    CONTAINER ID        IMAGE                 COMMAND                  CREATED              STATUS                          PORTS               NAMES
    c48649c8cee9        paigeeworld/centos7   "/bin/bash"              10 minutes ago       Up 10 minutes                                       relaxed_curran
    [root@localhost opt]# docker stop c48649c8cee9 / / close the container
    c48649c8cee9
    [root@localhost opt]# docker ps -a / / at this time, the container is in the exit state
    CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS                       PORTS               NAMES
    c48649c8cee9        paigeeworld/centos7   "/bin/bash"              11 minutes ago      Exited 
    [root@localhost opt]# docker run -d paigeeworld/centos7 /bin/bash -c "while true;do echo hello;done" / / run continuously in the background, - d run in the background
    398f3d27f36b7f59a2167a71e71f61064e4e9a0808dfa13404caec0280a0b9c2
    [root@localhost opt]# docker ps -a / / check that the container is always open
    CONTAINER ID        IMAGE                 COMMAND                  CREATED              STATUS                       PORTS               NAMES
    398f3d27f36b        paigeeworld/centos7   "/bin/bash -c 'while..."   9 seconds ago        Up 7 seconds   
    [root@localhost opt]# Docker export 36fdfb0925ba > nginx? C / / container export
    [root@localhost opt]# ls
    containerd  nginx  nginx_c  rh
    [root@localhost opt]# scp /opt/nginx_c root@192.168.13.129:/opt / / / remote replication to another virtual machine

    To another virtual machine

    [root@localhost opt]# ls
    containerd  nginx  nginx_c  rh
    [root@localhost opt]# Cat nginx | C | docker import - nginx: Web / / container import will generate images without creating containers
    sha256:1488d058197863aedd46d289eeb11dc39f19a2b855c3ecf383331a4d0bac568c
    [root@localhost opt]# docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    nginx               web                 1488d0581978        5 seconds ago       125MB
    [root@localhost opt]# docker ps -a
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
    [root@localhost opt]# Docker PS - a | awk '{print "docker RM" $1}' | bash / / batch delete container

    docker resource control

Limit cpu usage rate

  • Limit cpu usage with the -- cpu quota option
  • Implementation by modifying the configuration file cpu.cfs

Multitasking share cpu in proportion

  • Docker run -- CPU shares 1024 container A
  • Docker run -- CPU shares 1024 container B
  • Docker run -- CPU shares 2048 container C

Use the -- cpuse CPUs option to restrict cpu kernel usage

[root@localhost opt]# Docker run -- CPU quota 20000 nginx: latest / / set 20% limit
[root@localhost opt]# cd /sys/fs/cgroup/cpu/docker/
[root@localhost opt]# cat cpu.cfs_quota_us 
-1
[root@localhost opt]# Docker run - ITD -- name c1 -- cpu shares 512 paigeeworld / centos7 / / create container c1 and set weight, so that the cpu resources of c1 and c2 account for 33.3% and 66.7%
ec4ab03a7969eebe4746cfe67184bc2c6f9c97e81b22bc2ffab452820a78a0a7
[root@localhost opt]# docker run -itd --name c2 --cpu-shares 1024 paigeeworld/centos7
c688b014329c6a33b0d66947f4489a1a1cb6febc321090ecb4a82b68ae6df250
[root@localhost opt]# docker ps -a / / view container
CONTAINER ID        IMAGE                 COMMAND                  CREATED              STATUS                     PORTS               NAMES
c688b014329c        paigeeworld/centos7   "/bin/bash"              About a minute ago   Up About a minute                              c2
ec4ab03a7969        paigeeworld/centos7   "/bin/bash"              About a minute ago   Up About a minute                              c1
[root@localhost opt]# Docker run -- name C3 -- cpuset CPUs 0,1 paigeeworld / centos7 / / restrict the container to use the specified cpu
[root@localhost opt]# docker ps -a / / view container information
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS                     PORTS               NAMES
751409a81682        paigeeworld/centos7   "/bin/bash"              8 seconds ago       Exited (0) 7 seconds ago                       c3
[root@localhost opt]# docker run --name c5 -m 512m paigeeworld/centos7 / / memory usage limit
[root@localhost opt]# docker ps -a
CONTAINER ID        IMAGE                 COMMAND                  CREATED              STATUS                          PORTS               NAMES
8c3101668345        paigeeworld/centos7   "/bin/bash"              5 seconds ago        Exited (0) 4 seconds ago                            c5

Restrictions on blkio

--device-read-bps: Limit the amount of data for a device
--device-write-bps: Limit the amount of data written to a device
--device-read-iops: Limit the number of times to read a device
--device-write-iops: Limit the number of writes to a device
[root@localhost opt]# docker run -d --device-write-bps /dev/sda:30mb paigeeworld/centos7

Data management of docker

Data management operations

  • Easy to view the data generated in the container
  • Data sharing among multiple containers

Two management methods

  • Data volume
  • Data volume container

Data volume

  • Data volume is a special directory for container use

Data volume container

  • Data volume container is a common container

Data volume sharing (sharing between host and container)

[root@localhost ~]# docker pull centos / / Download Image
[root@localhost ~]# docker run -v /var/www:/data1 --name web1 -it centos /bin/bash / / associate the sharing of host and container
[root@2483bee94f1a /]# cd data1/
[root@2483bee94f1a data1]# echo "123" > test01.txt   
[root@2483bee94f1a data1]# exit 
exit
[root@localhost ~]# cat /var/www/test01.txt   
123

Data volume container sharing (container and container)

[root@localhost ~]# docker run --name web100 -v /data1 -v /data2 -it centos /bin/bash / / create a web100 container with two volumes of data1, 2
[root@ba6a328c068e /]# cd data1/
[root@ba6a328c068e data1]# Echo "111" > 111.txt / / write contents to the directory respectively
[root@ba6a328c068e data1]# cd ../data2/
[root@ba6a328c068e data2]# echo "222" > 222.txt
[root@ba6a328c068e data2]# exit
exit
[root@localhost ~]# Docker run - it -- volumes from web100 - it CentOS / bin / bash / / mount the new container to web100
[root@3f64be49dadd /]# cat data1/111.txt 
111
[root@3f64be49dadd /]# cat data2/222.txt 
222

Tags: Linux Docker Nginx CentOS yum

Posted on Sun, 09 Feb 2020 10:43:59 -0500 by DragonFire-N[R]