Linux series docker container virtualization

Centos7 series

Preparations before docker installation

Can access the public network

 systemctl stop firewalld
 systemctl disable firewalld
 systemctl stop NetworkManager
 systemctl disable NetworkManager
  • Permanently or temporarily shut down selinux
[root@bogon ~]# cat /etc/sysconfig/selinux
-------------------------------------------------- -------
SELINUX=disabled #Permanently closed
--------------------------
setenforce 0 #Temporarily close
  • CentOS-Base.repo should be placed under /etc/yum.repos.d/
  • Need to make local yum source
  • Install docker-CE community version

1. Use yum to clear the old docker version

yum - remove docker

2. Install dependencies

yum -y install yum-utils device-mapper-persistent-data lvm2

3. Add the docker-ce version of the yum source configuration file

curl https://download.docker.com/linux/centos/docker-ce.repo \-o /etc/yum.repos.d/docker-ce.repo
or
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

4. Yum starts to install docker-ce community version

yum -y install docker-ce docker-ce-cli containerd.io

5. Initialization operation

systemctl start docker
systemctl enable docker

6. Verification version

[root@bogon ~]# docker version
Client: Docker Engine - Community
 Version: 20.10.14
 API version: 1.41
 Go version: go1.16.15
 Git commit: a224086
 Built: Thu Mar 24 01:49:57 2022
 OS/Arch: linux/amd64
 Context:default
 Experimental: true

7. Add docker domestic image source (the default foreign image source, the download is very slow, you need to change it back to the domestic one)

 cat /etc/docker/daemon.json # Configure image accelerator
{<!-- -->
  "registry-mirrors":["https://registry.docker-cn.com"],
  "max-concurrent-downloads":1
}
or
{<!-- -->
   "registry-mirrors": ["https://m2lv5yea.mirror.aliyuncs.com"]
}
or
{<!-- -->
  "storage-driver": "overlay2",
  "insecure-registries": ["registry.access.redhat.com","quay.io"],
  "registry-mirrors": ["https://60699eq2.mirror.aliyuncs.com","https://registry.docker-cn.com"],
  "exec-opts": ["native.cgroupdriver=systemd"],
  "live-restore": true,
  "log-opts": {<!-- -->
  "max-size": "100m"
   },
  "data-root":"/data/docker"
}

systemctl daemon-reload
systemctl restart docker

8. Commonly used docker commands

  • docker search centos #Find the image source. Privately uploaded images must be uploaded in the form of author name/image name.
  • docker pull hello-world #Download image Image name: version number
    docker pull centos:7
  • docker imagesc #View all local images. The image has not been unpacked yet, and the container is unpacked.
  • docker build -t Mr.wu/centos7:v1 .
    Mr.wu: author name centos7: image name v1: version number
    . Represents the location of the build image configuration file Dockerfile
    -t represents the specified image name
    1) Create the storage directory for Dockerfile
    Example: mkdir -p /db/dockerfile/library/centos/7
    cd /db/dockerfile/library/centos/7
    2) Create docker.sh script #To start a container, it must encapsulate at least one process that will never exit; without a process, the container cannot start, so an infinite loop script is required.
cat docker.sh
#!/bin/bash
while true
do #docker public network mirror warehouse dockerhup
   echo "Hello.world"
   sleep 2
done

3) Create a Dockerfile configuration file, the name must be Dockerfile
cat Dockerfile (Dockerfile is equivalent to the configuration file of images)
FROM centos #This image is based on the centos operating system. The image of the centos operating system must be available locally. If there is no local operating system, it will be automatically downloaded from the public network image warehouse.
LABEL MAINTATNER=“Mr.wu.com” #Author’s name
RUN ln -sfv /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ln -s soft link f: forced override v: information output enabled mirroring time zone is Shanghai time zone
COPY docker.sh /home/test/
RUN chmod +x /home/test/docker.sh
CMD [“/home/test/docker.sh”]
Start building the image and execute docker build -t Mr.wu/centos7:v1.
docker run -dit -name container name ImageID/mirror name /bin/bash
Create a container and run it. After the image is started, it is called a container. -d means running in the background -i means you can use command interaction in the container -t to create a pseudo terminal for logging in. So the -dit parameter must be added! You don’t have to add /bin/bash at the end to enable CMD execution; if you add /bin/bash, CMD in the Dockerfile will not be enabled!

  • docker run -dit –name nginx_test -p 80:80 nginx:latest
    #Map local port 80 to port 80 of the container before starting the image
    0.0.0.0:80 –> 80/tcp
    Port 80 representing any local IP is mapped to port 80 of the container using the tcp protocol
    curl 127.0.0.1 displays the web content of NGINX
  • docker ps #View the processes of all containers, including those that exited
  • docker ps -a #View the processes of running containers
  • docker logs container name/ID number #View container logs
  • docker attach container name/container ID number
    #Switch into the container from the local system. This command is not commonly used.
    exit exits the container, which means /bin/bash exits and there are no processes in the container.
  • docker stop container ID number/container name #Stop the process of the running container
  • docker start container ID number #Run (stopped) container
  • docker rm container ID number #Delete the stopped container, stop it first and then delete it
  • docker rmi image name/Image number
    Before deleting the image, first make sure that the image has not been started as a container, stop the container first, then delete the container and then delete the image.
    docker commit container name new image name
    Example: docker commit test1 Mr.wu/centos7:v2
    It means saving the changed container process as a new image (for example, switch to the container, execute a simple command mkdir -p /nsj/raptor4003, then exit, and submit it as a new image)
  • docker exec container name execution command
    #Issuing commands directly to the container on the local system without switching to the container
    docker exec -it test /bin/bash can cut into the container. After exit, the container process is still there. Better than attach
  • docker cp local system file container name:/directory/
    Used to copy files between the container process and the local system
    docker cp docker.sh test1:/nsj/
    docker exec test1 ls /nsj
    docker cp test1:/nsj/yum.log /tmp
  • docker create -it ImageID
    Create a container but do not run it directly. You can use start to run it.
  • docker diff container name
    #Compare the difference between the container and the original image, and what files or folders have been changed
    docker diff test1
    C /root/.bash_history # C stands for many things A stands for few things
    A /tmp/docker.sh
  • docker events #Monitor changes in containers at all times and monitor commands
  • docker export container ID number/container name > container.tar
    Export the process in the container (export container) into a .tar package, put it locally, and import it back to become a mirror.
  • docker import container.tar Mr.wu/centos7:3
    Example: docker import test1.tar Mr.wu/centos7:3
    #Create the exported .tar package into a new image
  • docker history ImageID number/mirror name
    #Check the historical modification record of an image
  • docker info
    #View docker running information of the current operating system
  • docker inspect ImageID number/mirror name
    #View detailed information and configuration files of the specified image
  • docker kill container ID number/container name
    #Force to stop one or more running container processes, which is faster than stop
  • docker save image name > image name.tar
    #Export a local image (export image) into a .tar package and put it locally
  • docker load #Used to reload the .tar package exported by save back into the image, which is exactly the same as the original image name and ImageID.
  • docker logs container ID number #Used to output logs within the container process
  • docker pause container ID number #Pause the container process
  • docker unpause container ID number #Resume the container process
  • docker port container name
    Output container port mapping and protocol, which can generally be replaced by docker ps -a
    #docker run -dit –name nginx_test -p 80:80 nginx:latest
    Example: Map local port 80 to port 80 of the container before starting the image
    0.0.0.0:80 –> 80/tcp
    Port 80 representing any local IP is mapped to port 80 of the container using the tcp protocol
    curl 127.0.0.1 displays the web content of NGINX
  • docker rename container old name container new name #rename the container process
  • docker restart container ID number #Restart the container process
  • docker stats container ID number
    – Used to output the resource usage of the container in real time, similar to top
  • docker tag Mr.wu/centos:3 Mr.wu/centos:4
    Similar to creating a soft link to a mirror, the ID numbers of both mirrors are the same. According to the ID numbers, both can be forcibly deleted together.
  • docker top container ID number #View the process information of the specified container
  • docker update –help
    #Adjust the startup configuration of one or more containers. You can configure CPU and memory parameters and start the image.
  • docker version #View docker version information
  • docker wait container ID number
    Capture the exit status of one or more containers and return the exit status code
  • login & amp; & amp; logout & amp; & amp; push
  • login: Log in to docker hup official public warehouse
  • logout: Exit docker hup official public warehouse
    push: used to push local images to docker hup official public image warehouse
  • docker hup official public image warehouse https://hup.docker.com/
    Docker hup cannot register an account in China

Install docker-compose

Version 1

curl -L “https://github.com/docker/compose/releases/download/v2.1.1/docker-compose-

(

u

n

a

m

e

?

s

)

?

(uname -s)-

(uname?s)?(uname -m)” -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
docker-compose –version

Version 2 (installed via pip)

yum -y install python3
wget https://bootstrap.pypa.io/pip/3.6/get-pip.py instead
or
wget https://bootstrap.pypa.io/get-pip.py
or
wget https://bootstrap.pypa.io/pip/3.6/get-pip.py
python3 get-pip.py
pip -V
pip install docker-compose
docker-compose version

Install and view the command runlike for docker to start the container

yum -y install python36
pip3 install runlike
runlike -p container name