Introduction to docker principle and deployment

The first time I came into contact with docker was in 2018. At that time, I was also curious about docker technology, so I found some teaching videos and online materials for study and research, and got a general understanding of some principles, concepts and deployment operations. However, in the past two or three years, I have also experienced several projects, but I have not used docker in the actual development and production environment. On the contrary, it is required to master docker and even Kubernetes and other technologies at the company level, which is also some wonders! In this context, the theoretical study and actual deployment of docker will be carried out again, and the relevant brief principle concepts will be summarized, with the focus on installation, deployment and actual use of related command operations, etc., so that it can be used quickly in the future and continue to be more in-depth in the future. The accumulation of learning also provides a reference for latecomers. There are inevitably omissions in the article, and I hope readers can correct me, thank you very much!

1. Introduction to 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, and then publish it to any popular Linux machine, and it 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.
Docker has been divided into CE (Community Edition: Community Edition) and EE (Enterprise Edition: Enterprise Edition) since version 17.03.
Application scenarios of Docker

Web 应用的自动化打包和发布。
自动化测试和持续集成、发布。
在服务型环境中部署和调整数据库或其他的后台应用。
从头编译或者扩展现有的 OpenShift 或 Cloud Foundry 平台来搭建自己的 PaaS 环境。

Advantages of Docker

Docker 是一个用于开发,交付和运行应用程序的开放平台。
Docker 能够将应用程序与基础架构分开,从而可以快速交付软件。
借助 Docker,可以与管理应用程序相同的方式来管理基础架构。
通过利用 Docker 的方法来快速交付,测试和部署代码,可以大大减少编写代码和在生产环境中运行代码之间的延迟。

2. Docker architecture concept

Docker uses the core separation mechanism in Linux, such as Cgroups, and Linux's core Namespace (name space) to create independent containers. In a word, Docker is a lightweight virtualization technology that uses Namespace for resource isolation, Cgroup for resource limitation, and Union FS for container file system. The essence of the Docker container is still a special process that runs directly on the host. The file system seen is isolated, but the operating system kernel is shared with the host OS, so Docker is a lightweight virtualization technology.
insert image description hereinsert image description here

Docker consists of three basic concepts:

(1) mirror (Image)

Docker 镜像(Image),就相当于是一个 root 文件系统。
比如官方镜像 ubuntu:16.04 就包含了完整的一套 Ubuntu16.04 最小系统的 root 文件系统。

(2) Containers

镜像(Image)和容器(Container)的关系,就像是面向对象程序设计中的类和实例一样,
镜像是静态的定义,容器是镜像运行时的实体。容器可以被创建、启动、停止、删除、暂停等。

(3) Warehouse (Repository)

仓库可看成一个代码控制中心,用来保存镜像。

Docker uses a client-server (C/S) architecture pattern and uses remote APIs to manage and create Docker containers.
Docker containers are created from Docker images.
The relationship between containers and images is similar to objects and classes in object-oriented programming.

concept illustrate
Docker images A Docker image is a template for creating Docker containers, such as an Ubuntu system.
Docker container (Container) A container is an application or a group of applications that run independently, and is the entity of the image runtime.
Docker client (Client) The Docker client communicates with the Docker daemon through the command line or other tools using the Docker SDK (https://docs.docker.com/develop/sdk/).
Docker host (Host) A physical or virtual machine used to execute the Docker daemon and containers.
Docker Registry The Docker warehouse is used to store images, which can be understood as a code warehouse in code control. Docker Hub (https://hub.docker.com) provides a huge collection of images for use. A Docker Registry can contain multiple warehouses (Repository); each warehouse can contain multiple tags (Tag); each tag corresponds to a mirror image. Usually, a warehouse will contain images of different versions of the same software, and tags are often used to correspond to each version of the software. We can use the format of <warehouse name>:<label> to specify which version of this software is the mirror image. If no tag is given, latest will be used as the default tag.
Docker Machine Docker Machine is a command-line tool that simplifies Docker installation. Docker can be installed on corresponding platforms through a simple command line, such as VirtualBox, Digital Ocean, and Microsoft Azure.

3. docker installation and deployment

3.1 Installation environment

linux系统:CentOS Linux release 7.2.1511 (Core)
docker版本:Docker version 20.10.8

3.2 Installation steps

1. 安装辅助工具
yum install -y yum-utils device-mapper-persistent-data lvm2

2. 添加软件源信息,这里使用Docker CE版本。
从2017年3月开始docker在原来的基础上分为两个分支版本: Docker CE 和 Docker EE。Docker CE 即社区免费版,Docker EE 即企业版,强调安全,但需付费使用。
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

3. 更新yum,更新yum缓存
yum update
yum makecache fast

4. 移除旧版本
yum remove docker \

5. 安装docker-ce
yum -y install docker-ce

6. 查看docker版本
docker version

7. 启动Docker,然后加入开机启动
systemctl start docker
systemctl enable docker
systemctl status docker

4. docker use

4.1 Image management

1. 列出本地所有镜像
docker images

2. 查找镜像
docker search <IMAGE_ID/NAME>

3. 拉取镜像
docker pull  <IMAGE_ID/NAME> :tag
#tag:镜像的标签 如果不指定tag 那么默认用最新的版本

4. 上传镜像
docker push <IMAGE_ID/NAME>

5. 删除镜像
docker rmi <IMAGE_ID/NAME>

6. 例如:
查找nginx镜像:docker search nginx
拉取nginx镜像:docker pull nginx(:版本,默认为latest 最新版本)
删除nginx镜像:docker rmi nginx

4.2 Container Management

1. 启动运行镜像--容器
docker run -p 本机映射端口:镜像映射端口 -d  --name 启动镜像容器命名 -e 镜像启动参数  镜像名称:镜像版本号
参数:
-p   本机端口和容器启动端口映射,可以有多个,因为该镜像可能存在多个端口
-d   以守护进程方式后台运行
--name   容器名称
-e    镜像启动参数 
例如:docker run -p 8088:80 -d --name nginx-c nginx

2. 列举当前运行的容器
docker ps
-l:显示最后启动的容器
-a:同时显示停止的容器,默认只显示启动状态

3. 检查容器内部信息,查看容器详细信息(输出为Json)
docker inspect 容器名称
-f:查找特定信息,如 docker inspect  -f  '{
   
   { .NetworkSettings.IPAddress }}'

4. 停止某个容器
docker stop 容器名称

5. 启动某个容器
docker start 容器名称
docker restart 容器名称

6. 移除某个容器
docker rm 容器名称 (容器必须是停止状态)

7. 查看容器日志、运行进程及容器中的变化
docker logs <CONTAINER_ID> -f  :输出容器日志,-f 表示实时输出
docker top <CONTAINER_ID>:查看容器中运行的进程
docker diff <CONTAINER_ID>:查看容器中的变化

8. 进入容器中执行相关命令
(1)docker exec -ti <CONTAINER_ID> /bin/bash
-d:分离模式: 在后台运行
-i:即使没有附加也保持STDIN 打开
-t:分配一个伪终端
(2)docker attach <CONTAINER_ID> 连接到启动的容器
docker attach可以attach到一个已经运行的容器的stdin,然后进行命令执行的动作。但是需要注意的是:如果从这个stdin中exit,会导致容器的停止。


9. 复制容器内的文件到宿主机目录上
docker cp <CONTAINER_ID>:path hostpath

10. 挂载
docker run -d -p 8081:8081 --name nginx-c 
-v /usr/local/nginx/conf/nginx.conf:/etc/nginx/nginx.conf 
-v /usr/local/nginx/html:/usr/share/nginx/html 
nginx
挂载,docker中使用的某些配置文件,可以挂载使用宿主机中的文件
第一个“-v”,挂载配置文件路径;
第二个“-v”,挂载静态文件项目位置路径;

11. 从容器创建一个新的镜像
docker commit -a "author" -m "comment" <CONTAINER_ID> imagename:tag
-a :提交的镜像作者;
-c :使用Dockerfile指令来创建镜像;
-m :提交时的说明文字;
-p :在commit时,将容器暂停。

12. 其他
docker rm `docker ps -a -q`:删除所有容器
docker kill `docker ps -q`
docker rmi `docker images -q -a`

5. References

[1] https://www.docker.com/
[2] https://www.cnblogs.com/cac2020/p/11359412.html
[3] https://www.cnblogs.com/makelu/p/11018212.html

Guess you like

Origin blog.csdn.net/shy871/article/details/117598629