docker basic management

Docker core concepts

1. What is Docker?

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

2. The difference between Docker and virtual machine

Characteristic Docker container virtual machine
Starting characteristics SEC level Minute class
Calculation capacity loss Almost no Loss about 50%
performance Near native weaker than
System support quantity (single machine) Thousands of Dozens of
Isolation Resource constraints Complete isolation

3. Using scenario of Docker

● packaged applications simplify deployment

● free migration from underlying hardware

● for example: the server moves from Tencent cloud to Alibaba cloud

4. Docker core concepts

image

container

Warehouse (public warehouse, private warehouse, storage image)

5. Two ways to install Docker in CentOS

● use CURL to obtain the installation script of Docker for installation

● use YUM warehouse to install Docker

Docker image operation

Basic operation of docker image

Container 1: 192.168.13.128
 Container 2: 192.168.13.129
[root@localhost ~]# docker inspect f7bb5701a33c  ##View image information
[root@localhost ~]# docker tag nginx:latest nginx:web  ##Add new label
[root@localhost ~]# docker images  ##View mirroring
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 mirrors
[root@localhost opt]# ls
containerd  nginx  rh
[root@localhost opt]# scp /opt/nginx root@192.168.13.129:/opt/  
##Remote replication to 129 server

Open another virtual machine with docker (192.168.13.129)

[root@localhost opt]# docker load < nginx   ##Loading mirroring
##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.13.128)

[root@localhost opt]# docker tag nginx:latest nginx:web  ##Modify label
[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 list 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 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

Complete steps:

[root@localhost opt]# docker search centos7   ##View and find centos7 image
[root@localhost opt]# docker pull paigeeworld/centos7  ##Download mirroring
[root@localhost opt]# docker images  ##View mirroring
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 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 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 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 container
c48649c8cee9
[root@localhost opt]# docker ps -a  ##The container is now 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"
##Execute continuously in the background, - d runs 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
##A mirror is generated without creating a container
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 
##Bulk 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 to set the 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 mirroring
[root@localhost ~]# docker run -v /var/www:/data1 --name web1 -it centos /bin/bash
##Share of associated 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 in 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 the data volume container web100
[root@3f64be49dadd /]# cat data1/111.txt 
111
[root@3f64be49dadd /]# cat data2/222.txt 
222

Tags: Linux Docker Nginx CentOS

Posted on Sun, 12 Jan 2020 06:12:16 -0800 by crisward