Detailed explanation of docker for cloud native

Table of contents

1. The concept of cloud native

1.1 Definition of Cloud Native

1.2 Cloud Native Elements

1.2.1 Microservices

1.2.2 DevOps

1.2.3 Continuous delivery

1.2.4 Containerization

2. Docker

2.1 Overview of Docker

2.1.1 Definition of Docker

2.1.2 Docker application scenarios

2.1.3 Docker architecture

2.2 Docker commands

2.2.1 Docker process related commands

2.2.2 Docker image related commands

2.2.3 Docker container related commands

2.2.4 Log process

2.2.5 Data volume container

2.2.6 dockerfile build docker image file

2.2.7 Docker Compose


 

1. The concept of cloud native

1.1 Definition of Cloud Native

As the name suggests, "cloud native" means "born and grown on the cloud".
Cloud native is an application program based on cloud computing technology and using technologies such as containers, microservices, DevOps and automation to achieve agile development and rapid deployment Architecture, this is cloud native.
Cloud native is an application designed for "cloud", so the technology partly relies on the three-tier concept of traditional cloud computing, infrastructure as a service (IaaS), platform as a service (PaaS) and software as a service (SaaS).

1.2 Cloud Native Elements

Cloud native elements are as follows:

526a73c8d1234514aa1cf0d5294a4310.png

 

1.2.1 Microservices

Microservices solve the low coupling + high cohesion we have been pursuing in software development. I remember that there was a problem with the interface of our system, which affected the user's front-end operations. Why do these two affect each other?!"

Microservices can solve this problem. The essence of microservices is to divide a large pie into several low-coupling small pies. For example, one small pie is responsible for receiving external data, and one small pie is responsible for responding to front-end operations. Small pies can further Splitting, for example, the pie responsible for receiving external data can continue to be divided into multiple pies responsible for receiving different types of data, so that if there is a problem with each pie, other pies can still provide services to the outside world.

1.2.2 DevOps

DevOps means that development and operation and maintenance are no longer two separate teams, but a team with me in you and you in me. We are now a team of development and operation and maintenance, but the knowledge and experience in operation and maintenance still need to be continuously improved.

1.2.3 Continuous delivery

Continuous delivery means that new features are released to users frequently without affecting users' use of services. It is very, very difficult to do this. We now have a version every two weeks, and every time it goes online, it will have different degrees of impact on different users.

1.2.4 Containerization

The advantage of containerization is that it is no longer necessary to care about the technology stack used by each service during operation and maintenance. Each service is encapsulated in a container without distinction, and can be managed and maintained without distinction. Now it is a more popular tool It is docker and k8s.

So you can also simply understand cloud native as: cloud native = microservices + DevOps + continuous delivery + containerization

 

2. Docker

2.1 Overview of Docker

2.1.1 Definition of Docker

Docker is an open source application container engine, based on the Go language and open source in compliance with the Apache2.0 protocol. Docker allows developers to package their applications and dependencies into a lightweight, portable container, which can then be distributed to any popular Linux machine, and can also be virtualized. Containers use a sandbox mechanism completely, and there will be no interfaces between them. More importantly, the performance overhead of containers is extremely low.

2.1.2 Docker application scenarios

  • Automated packaging and publishing of web applications.

  • Automated testing and continuous integration, release.

  • Deploy and tune database or other backend applications in a service-oriented environment.

  • Solve the problem of software cross-border migration

2.1.3 Docker architecture

e2d260e963f445bfa0d1b69accccbde1.png

 

mirror image

A Docker image (Image) is a read-only template. Images can be used to create Docker containers, and one image can create many containers.

container

Docker uses containers to run one or a group of applications independently. A container is a running instance created using an image. It can be started, started, stopped, deleted. Each container is an isolated and secure platform. A container is a simplified version of the Linux environment and the applications running in it.

Warehouse (Repository)

The warehouse (Repository) is a place where image files are stored centrally. There is a difference between a repository (Repository) and a warehouse registration server (Registry). There are often multiple warehouses stored on the warehouse registration server, and each warehouse contains multiple images, and each image has a different tag (tag). Warehouses are divided into public warehouses (Public) and private warehouses (Private). The largest public warehouse is Docker Hub (https://hub.docker.com/), which stores a large number of images for users to download. Domestic public warehouses include Alibaba Cloud, NetEase Cloud, etc.
 

2.2 Docker commands

2.2.1 Docker process related commands

#启动docker
systemctl start docker

#停止docker
systemctl stop docker

#查看docker状态
systemctl status docker

#重启docker
systemctl restart docker

#设置开机启动
systemctl enable docker

2.2.2 Docker image related commands

#查看镜像,查看本地所有的镜像
docker images

docker images 
    -q #查询所有镜像的ID
    -a #列出所有的镜像
    --help #命令帮助
#查询如下
#镜像的仓库源, 镜像的标签,镜像的id,     镜像的创建时间,   镜像的大小
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    feb5d9fea6a5   14 months ago   13.3kB


#搜索镜像:从网络中查询需要的镜像
docker search 镜像的名称

#拉取镜像:从Docker仓库下载镜像到本地,镜像名称格式为 名称:版本号, 如果不指定,则是最新版,如果不知道镜像的版本号,可以去hub.docker.com去查询
docker pull 镜像名称

#删除指定镜像
docker rmi 镜像id 

2.2.3 Docker container related commands

docker run parameter image name: version number

Parameter Description:
    * -i: Keep the container running, usually used together with -t. After adding the two parameters it, the container will automatically enter the container after it is created. After exiting the container, the container will automatically close.
    * -d: Run the container in daemon (background) mode, create a container to run in the background, you need to use docker exec to enter the container, after exiting, the container will not be closed.
    * --name: Name the created container
    * -p host port: container port
#查看正在运行容器
docker ps
#查看所有容器
docker ps -a

#创建并启动容器
docker run -d --name nginx1 -p 4200:80 nginx

#进入容器
docker exec -it ngnix1 /bin/bash

#停止容器
docker stop 容器名称或者容器id

#启动容器
docker start 容器名称或者容器id

#删除容器:如果是运行状态则删除失败,需要停止容器才能删除
docker rm 容器名称或者容器id


#查看容器信息
docker inspect 同期名称或者容器id

2.2.4 Log process

#查看最近日志
docker logs -f 容器名/容器ID

#最近30分钟的日志
docker logs

#查看某时间之后的日志
docker logs -t  --since="2022-08-08T18:08:08" 容器ID

#查看某时间段日志
docker logs  -t  --since="2022-08-08T18:08:08" --until "2022-08-08T20:08:08" 容器ID

2.2.5 Data volume container

  A volume is a directory or file that exists in one or more containers and is mounted to the container by docker, but does not belong to the joint file system, so it can bypass the Union File System to provide some features for persistent storage or shared data. The purpose of the design is data persistence, which is completely independent of the life cycle of the container, so Docker will not delete the data volume it mounts when the container is deleted. Data volumes can share or reuse data between containers and changes in the volume can take effect directly in real time, and the life cycle of the data volume lasts until no container uses it.

01f06ba2c51d45c1a778869e4cdc91fa.png
 

 

Configure data volume

docker run ... -v host directory (file): container directory (file)...

 Precautions

  1. directory must be an absolute path
  2. If the directory does not exist, it will be created automatically
  3. Can be attached to multiple data volumes
#mysql 同步数据
docker run -d -p 3310:3306 -v /www/server/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql1 mysql:5.5

2.2.6 dockerfile build docker image file

  • Dockerfile is a text file

  • Contains a line of instructions

  • Each instruction builds a layer, based on the base image, and finally builds a new image

  • For developers: can provide a completely consistent development environment for the development team

  • For testers: You can directly build the image built during development or build a new image through the Dockerfile file to start working

  • For operation and maintenance personnel: during deployment, seamless migration of applications can be achieved

56a24cda185e4ab28b350f3fcfd7615c.png

 Build your own centos image as shown below

bf136b69de9149758f744db7d9cb5fd9.png

 

Execute dockerfile to generate image

docker build -f the path of the dockerfile file -t Set the name and version number of the new image. # Note the last point, which represents the generated image path -f indicates the dockerfile path -t The
    name
    and version number of the new image of the device
The last . means The path to generate the image is the current path

2.2.7 Docker Compose

When we use Docker, we define the Dockerfile, and then use commands such as docker build and docker run to operate the container. However, the application system of the microservice architecture generally includes several microservices, and each microservice usually deploys multiple instances. If each microservice needs to be started and stopped manually, the efficiency will be low and the amount of maintenance will be large.

Compose is a tool for defining and running multi-container Docker applications. With Compose, you can use YML files to configure all the services your application needs. Then, with a single command, all services can be created and started from the YML file configuration.

The three steps used by Compose:

  • Use a Dockerfile to define your application's environment.
  • Use docker-compose.yml to define the services that make up your application so they can run together in isolation.
  • Finally, execute the docker-compose up command to get the entire application up and running.

Due to the large space required for the introduction of Docker Compose, the author will write a separate article on Docker Compose, here is a brief introduction.

 

6e70e97e04224516957cf95adadfe70d.gif

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_43649937/article/details/131023875