Docker installation docker and basic image container operation

1, Docker introduction

Docker is an open source application container engine, which is developed based on go language and follows Apache 2.0 0 protocol open source

It is an open source tool for running applications in Linux container

Is a lightweight "virtual machine"“

Docker's container technology can easily create a lightweight, portable and self-sufficient container for any application on one host.

2, The difference between Docker and virtual machine

characteristicDockervirtual machine
Starting speedSecond orderMinute level
Computing power lossAlmost fiveThe loss is about 50%
performanceNear primaryweaker than
System supportThousandsDozens
IsolationResource isolation / restrictionComplete isolation

3, Container core technology

docker is essentially a process of the host machine. docker realizes resource isolation through namespace, resource restriction through cgroup, and efficient file operation through copy on write technology (similar to the disk of virtual machine, such as allocating 500g instead of actually occupying 500g of physical disk).

4, Docker core concepts

● mirror image
Docker image is the basis of container creation. It is similar to the snapshot of virtual machine and can be understood as a read-only template for docker container engine.
Start a container through an image. An image is an executable package, which includes all the contents required to run the application, including code, runtime, libraries, environment variables, and configuration files.
Docker image is also a compressed package, but this compressed package is not only an executable file, but also an environment deployment script. It also contains a complete operating system. Because most images are built based on an operating system, it is easy to build the same local and remote environments, which is also the essence of docker image.

● containers
Docker's container is a running instance created from the image, which can be started, stopped and deleted. Each container created is isolated and invisible to each other to ensure the security of the platform.
The container can be regarded as a simple version of linux environment (including root user permission, image space, user space and network space) and applications running in it.

● warehouse
Docker warehouse is used to store images in a centralized manner. After creating your own image, you can use the push command to upload it to public or private warehouses. The next time you want to use this image on another machine, just get it from the warehouse.
The images, containers, logs and other contents of Docker are stored in / var/lib/docker by default

5, Install Docker

1. Turn off the firewall and file protection

[root@zqh ~]# systemctl stop firewalld
[root@zqh ~]# setenforce 0

2. Install dependent packages

[root@zqh ~]# yum install -y yum-utils device-mapper-persistent-data lvm2

yum-utils:Provided yum-config-manager Tools. device mapper:
yes Linux The general device mapping mechanism supporting logical volume management in the kernel provides a highly modular kernel architecture for the implementation of block device driver for storage resource management. device mapper Storage driver needs device-mapper-persistent-data and lvm2. 

Set alicloud image source

[root@zqh ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

Install docker CE and set it to start automatically

[root@zqh ~]# yum install -y docker-ce docker-ce docker-ce-cli containerd.io
[root@zqh ~]# systemctl start docker.service
[root@zqh ~]# systemctl enable docker.service

####
Installed Docker The system has two programs, Docker Server and ocker client. among Docker The server is a service process, which is responsible for managing all containers.
Docker The client plays the role of Docker The remote controller of the server can be used to control Docker Server process. In most cases Docker The server and client run on the same machine.

View docker information

View docker status, quantity information, container information and image information

6, Docker image operation

1. Search image

Format:

docker search keyword

2. Get image

Format:

docker pull Warehouse name[:label]

##If no label is specified when downloading the image, the latest version of the image in the warehouse will be downloaded by default, that is, the label selected is the latest label

3. View the image

docker imager

View mirror details

docker inspect image id Or name

4. Image acceleration

Log in to Alibaba cloud website

5. View image information

6. Modify the image name and label

Format:

docker tag Old name:Old label new name:new label

7. Delete image

The first method:

docker rmi Library name:label

The second method is to force deletion by id number

docker rmi image id number -f

##If the container - f is used, it will also be deleted

8. Export and import image to local

(1) Export image first

docker save -o Custom file name.tar Library name:label

[root@zqh ~]# docker save -o nginx-1.14.tar nginx:1.14

(2) Re import image

9. Export the image to its own library on the official website

First log in to the official website to register an account

Return to the server

[root@zqh ~]# docker login

[root@zqh ~]# docker tag nginx:1.14 zqhh/nginx:1.14  
##Change the published image to your account name

[root@zqh ~]# docker push zqhh/nginx:1.14 

Switch to the web interface to view

7, Docker container operation

1. Container creation

The newly created container is stopped by default and does not run any program. You need to start another process to start the container

Format:

docker create [option] image

Common options:
-i:Let the container open the standard input
-t:Give Way Docker Assign a pseudo terminal tty
-it:Work together to interact with the container and run an interactive session shell

[root@zqh ~]# docker create -it nginx:latest /bin/bash

2. Check all containers and their status

docker ps -a

3. Start the container

docker start container id

4. Stop the container

docker stop container id

or

docker kill container id

The difference is  docker stop I want to send an instruction to the program and close the program after the program finishes processing the process
 and docker kill Whether it is 3721 or not, it can be deleted directly

5. Create and start the container

You can directly execute the docker run command, which is equivalent to executing the docker create command first and then the docker start command.
heart
Note: the container is a terminal that exists together with the shell command running in it. The command runs, the container runs, the command ends, and the container exits.
By default, the docker container will take the first process inside the container, that is, the program with pid=1, as the basis for whether the docker container is running. If the process with pid=1 in the docker container hangs, the docker container will exit directly, that is, there must be a front process in the docker container, otherwise it is considered that the container has hung.

When using docker run to create containers, the standard running process of Docker in the background is:

(1)Checks whether the specified mirror exists locally. When the image does not exist, it will be downloaded from the public warehouse;

(2)Create and start a container using the image;

(3)Allocate a file system to the container and mount a read-write layer outside the read-only image layer;

(4)Bridge a virtual machine interface from the bridge interface configured by the host to the container;

(5)Assign an address in the address pool IP Address to container;

(6)Execute the application specified by the user. After execution, the container is terminated.

(1) Continuously run the container created by docker run in the background

[root@zqh ~]# docker run -d centos:7 /bin/bash -c "while true; do echo helloworld; done"

6. Modify container name

(1) Stop a container first

[root@zqh ~]# docker stop 4c9f41540e7f 

(2) Modify container name

[root@zqh ~]# docker run -itd --name zqh1 centos:7 /bin/bash

7. Enter the container

[root@zqh ~]# docker exec -it zqh1 bash

(1) How to directly enter the container after creating the container (one command)

[root@zqh ~]# docker run -it centos:7 bash

8. Realize file transfer before container and host

Host import file to container

docker cp File container id number:/Container directory/

The container exports the file to the host and renames it

docker cp container id number:/Container directory/File Host Directory/New file name

9. Export and import of containers

node1: 192.168.100.142

node2: 192.168.100.140

Users can migrate any Docker container from one machine to another. During migration, you can use docker export
The command exports the created container as a file, regardless of whether the container is running or stopped. The export file can be transferred to other machines, and the container migration can be realized through the corresponding import command.

Export format

docker export container id/name > file name

Import format

cat file name | docker import - Image name:label
##Images are generated after import, but containers are not created

Requirement: now you need to migrate the zqhh1 container in node1 to node2 host

The operation is as follows:

Operation on node1

[root@zqh ~]# docker export c4b00385104e > zqh.tar
[root@zqh ~]# scp zqh.tar 192.168.100.140:/opt

Operation on node2

[root@bogon opt]# cat zqh.tar | docker import - zqh:aa
[root@bogon opt]# docker run -itd zqh:aa bash
[root@bogon opt]# docker exec -it 8e5d07246112  bash
[root@8e5d07246112 /]# cd /opt/
[root@8e5d07246112 opt]# ls
aa.txt  passwd.txt

10. Delete container

docker rm container id

To delete a running container, you need to stop the container or add-f Force deletion

How to delete all containers at once

[root@zqh ~]# docker ps -a | awk 'NR>=2 {print $1}' | xargs docker rm -f

Keywords: Docker Kubernetes Container

Added by jerry_louise on Tue, 08 Mar 2022 16:01:08 +0200