初识Docker-概念、安装和使用

摘要:这里整理了下Docker的基本概念、安装(包括CentOS和MacOS)过程以及常用命令,记录下...

        Docker 是一种开源的应用容器引擎,它允许开发者打包他们的应用以及应用的依赖包到一个可移植的容器中,然后发布到任何支持Docker的平台上。

1. 初识Docker

1.1 什么是容器?

        容器就是将软件打包成标准化单元,以用于开发、交付和部署。

  • 容器镜像是轻量的、可执行的独立软件包 ,包含软件运行所需的所有内容:代码、运行时环境、系统工具、系统库和设置。
  • 容器化软件适用于基于 Linux 和 Windows 的应用,在任何环境中都能够始终如一地运行。
  • 容器赋予了软件独立性,使其免受外在环境差异(例如,开发和预演环境的差异)的影响,从而有助于减少团队间在相同基础设施上运行不同软件时的冲突。

Docker核心概念总结 | JavaGuide「Java学习 + 面试指南」一份涵盖大部分 Java 程序员所需要掌握的核心知识。准备 Java 面试,首选 JavaGuide!icon-default.png?t=N7T8https://javaguide.cn/tools/docker/docker-intro.html

1.2 为什么需要Docker

        Docker作为世界领先的软件容器平台,可以为应用的管理提供多种价值:

  1. 便于环境一致性管理:在开发、测试和生产环境中,应用程序通常需要在不同的操作系统和硬件平台上运行。Docker 可以将应用程序及其依赖项打包到一个独立的容器中,使得在不同环境中部署和运行应用程序变得更加一致和可靠。
  2. 轻量级和快速启动:与虚拟机相比,Docker 容器更加轻量级,因为它们共享主机操作系统的内核,而不是像虚拟机那样运行一个完整的操作系统。这使得 Docker 容器可以快速启动、停止和销毁,提高了开发、测试和部署的效率。
  3. 资源利用率高:由于 Docker 容器共享主机操作系统的内核,多个容器可以在同一台物理机上运行,而且它们之间的资源隔离也更加高效。这样可以更充分地利用硬件资源,降低了硬件成本和管理成本。
  4. 弹性和可伸缩性:Docker 容器可以根据需求动态伸缩,快速部署新的容器实例或者调整容器的数量,以满足应用程序的需求。这种弹性和可伸缩性使得应用程序能够更好地适应变化的工作负载和流量。
  5. 简化开发和持续集成/持续部署(CI/CD)流程:Docker 容器提供了一种标准化的打包和部署方式,可以与各种自动化工具和持续集成/持续部署(CI/CD)流程集成,简化了开发团队的工作流程,加快了软件交付速度。

        Docker思想:集装箱、标准化( 运输方式 、存储方式 、API 接口)、隔离

1.3 虚拟机VS Docker

        传统的虚拟机,可以虚拟出硬件,运行一个完整的操作系统,在这个操作系统上安装和运行所需的软件;

        容器内的应用可以直接运行在宿主主机内核中,容器没有自己的内核,也不用虚拟硬件(更轻便)。

        虚拟机和容器都是是相互隔离的,前者彻底隔离整个运行环境,后者则是隔离不同的应用。

        虚拟机是物理硬件层的抽象,容器则是应用层的抽象;前者占用的内存空间大启动慢,后者占用空间少启动快。具体对比参考下表

Docker入门教程(详细)_docker教程-CSDN博客文章浏览阅读4.8w次,点赞163次,收藏1k次。个人理解:开发了一个项目 可以在本机运行 但是如果版本更新 可能就会导致服务不可使用 后期我们维护起来就很繁琐 因为对于每一个机器 我们都得进行环境的部署呀 配置呀什么的比如: 我在window系统下开发了一个项目(jar+redis+ES+Kafka+...) 但是我现在准备部署到服务器上去 因为不能夸平台 所以我还得重新去按照配置环境什么的 但是docker 就可以让我们 直接将项目打包然后部署上线了 不用一个个单独去维护。Docker图形化界面管理工具!提供一个后台面板供我们操作!_docker教程https://blog.csdn.net/qq_54729417/article/details/127913536

特性容器虚拟机
启动秒级分钟级
硬盘使用一般为MB一般为GB
性能接近原生弱于原生
系统支持量单机支持上千个容器一般几十个

为什么要用 Docker | Docker — 从入门到实践icon-default.png?t=N7T8https://yeasy.gitbook.io/docker_practice/introduction/why

1.4 Docker的基本概念

        Docker的三个基本概念是镜像、容器和仓库

        镜像(Image):Docker将应用程序及其所需依赖、函数库、环境、配置等文件打包在一起,称为镜像。镜像本质上是一个特殊文件系统,即包含容器运行时所需的程序、库、资源、配置等文件外,还包含了一些为运行时准备的一些配置参数(如匿名卷、环境变量、用户等)

        它好比一个模板,可以通过这个模板创建容器服务。镜像类似于Java类,而容器是实例;一个镜像可以有多个容器。镜像采用分层存储的架构,逐层构建后将不再改变。

        容器(Container):镜像中的应用程序运行后形成的进程就是容器,只是Docker会给容器进程做隔离,对外不可见容器是镜像运行时的实体其实质是进程。容器也使用了分层存储,存储层的生存周期和容易一样,会随着容器消亡而丢失(因此不应向存储层写入数据,所有的文件写入操作,都应该使用数据卷(Volume)、或者绑定宿主目录

        仓库(Repository):仓库就是集中存放镜像(Image)的地方,仓库又可以分为公有仓库和私有仓库。中央仓库集中存储,hub.docker.com(官方),hub.daocloud.io

https://hub.docker.com/icon-default.png?t=N7T8https://hub.docker.com/

2. Docker的安装

2.1 MacOS和 Window安装Docker

        MacOS和 Window操作系统安装很简单,通过官网获取Docker安装包即可

Get Docker | Docker DocsDownload and install Docker on the platform of your choice, including Mac, Linux, or Windows.icon-default.png?t=N7T8https://docs.docker.com/get-docker/

        Docker安装,由于本机为MACOS使用M1芯片,且系统版本低于12(11.6)导致下载的多个版本无法使用。最终使用支持M1芯片的第一个版本4.9.1版本DockerDesk。

chenzh12@chenzh12deiMac ~ % docker version
Client:
 Cloud integration: v1.0.25
 Version:           20.10.16
 API version:       1.41
 Go version:        go1.17.10
 Git commit:        aa7e414
 Built:             Thu May 12 09:20:34 2022
 OS/Arch:           darwin/arm64
 Context:           default
 Experimental:      true

Server: Docker Desktop 4.9.1 (81317)
 Engine:
  Version:          20.10.16
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.17.10
  Git commit:       f756502
  Built:            Thu May 12 09:14:19 2022
  OS/Arch:          linux/arm64
  Experimental:     false
 containerd:
  Version:          1.6.4
  GitCommit:        212e8b6fa2f44b9c21b2798135fc6fb7c53efc16
 runc:
  Version:          1.1.1
  GitCommit:        v1.1.1-0-g52de29d
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

2.2 Linux安装Docker

此处使用阿里云的服务器,可参考

https://help.aliyun.com/zh/ecs/use-cases/deploy-and-use-docker-on-alibaba-cloud-linux-2-instances?spm=5176.21213303.J_qCOwPWspKEuWcmp8qiZNQ.23.51442f3dwb8NYo&scm=20140722.S_help@@文档@@51853.S_hot.ID_help@@文档@@51853-RL_docker安装-LOC_llm-OR_ser-V_3-RE_new1-P0_4#aa11e8210adyticon-default.png?t=N7T8https://help.aliyun.com/zh/ecs/use-cases/deploy-and-use-docker-on-alibaba-cloud-linux-2-instances?spm=5176.21213303.J_qCOwPWspKEuWcmp8qiZNQ.23.51442f3dwb8NYo&scm=20140722.S_help@@%E6%96%87%E6%A1%A3@@51853.S_hot.ID_help@@%E6%96%87%E6%A1%A3@@51853-RL_docker%E5%AE%89%E8%A3%85-LOC_llm-OR_ser-V_3-RE_new1-P0_4#aa11e8210adyt

使用腾讯云可参考

云服务器 搭建 Docker-最佳实践-文档中心-腾讯云操作场景icon-default.png?t=N7T8https://cloud.tencent.com/document/product/213/46000

  • 首先,查看CentOS 版本为7.9.2009
[root@iZuf6eqvrwmawrgxqo5kw7Z ~]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)

        注:安装Docker需要root账号登录才可以

  • 安装前需要配置国内yum源,避免安装速度过慢
## 指令解析
## 配置国内yum源
## wget:从网络获取资源
## 资源放置的地址: /etc/yum.repos.d/CentOS-Base.repo 表示将下载的文件保存为名为 CentOS-Base.repo 的文件,并存放在 /etc/yum.repos.d/ 目录下,这个文件是 CentOS 7 的软件源配置文件
## -O 选项表示将下载的文件保存为指定的文件
## 要下载的文件的URL地址:http://mirrors.aliyun.com/repo/Centos-7.repo** 
[root@iZuf6eqvrwmawrgxqo5kw7Z ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
--2024-04-11 11:29:32--  http://mirrors.aliyun.com/repo/Centos-7.repo
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 61.179.79.232, 61.179.79.237, 61.170.79.238, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|61.179.79.232|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2523 (2.5K) [application/octet-stream]
Saving to: ‘/etc/yum.repos.d/CentOS-Base.repo’

100%[===========================================================================================================================>] 2,523       --.-K/s   in 0.001s  

2024-04-11 11:29:32 (4.14 MB/s) - ‘/etc/yum.repos.d/CentOS-Base.repo’ saved [2523/2523]

## 清除软件包
[root@iZuf6eqvrwmawrgxqo5kw7Z ~]# yum clean all
Loaded plugins: fastestmirror
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
Cleaning repos: base epel extras mysql-connectors-community mysql-tools-community mysql57-community nginx-stable updates
Cleaning up list of fastest mirrors

## 读取新的软件包
[root@iZuf6eqvrwmawrgxqo5kw7Z ~]# yum makecache
Loaded plugins: fastestmirror
Determining fastest mirrors
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
base                                                                                                                                          | 3.6 kB  00:00:00     
epel                                                                                                                                          | 4.7 kB  00:00:00     
extras                                                                                                                                        | 2.9 kB  00:00:00     
mysql-connectors-community                                                                                                                    | 2.6 kB  00:00:00     
mysql-tools-community                                                                                                                         | 2.6 kB  00:00:00     
mysql57-community                                                                                                                             | 2.6 kB  00:00:00     
nginx-stable                                                                                                                                  | 2.9 kB  00:00:00     
updates                                                                                                                                       | 2.9 kB  00:00:00     
(1/28): base/7/x86_64/group_gz                                                                                                                | 153 kB  00:00:00     
(2/28): base/7/x86_64/primary_db                                                                                                              | 6.1 MB  00:00:00     
(3/28): base/7/x86_64/other_db                                                                                                                | 2.6 MB  00:00:00     
(4/28): base/7/x86_64/filelists_db                                                                                                            | 7.2 MB  00:00:00     
(5/28): epel/x86_64/group_gz                                                                                                                  | 100 kB  00:00:00     
(6/28): epel/x86_64/updateinfo                                                                                                                | 1.0 MB  00:00:00     
(7/28): epel/x86_64/prestodelta                                                                                                               |  644 B  00:00:00     
(8/28): epel/x86_64/filelists_db                                                                                                              |  12 MB  00:00:00     
(9/28): epel/x86_64/other_db                                                                                                                  | 3.4 MB  00:00:00     
(10/28): epel/x86_64/primary_db                                                                                                               | 7.0 MB  00:00:00     
(11/28): extras/7/x86_64/filelists_db                                                                                                         | 305 kB  00:00:00     
(12/28): extras/7/x86_64/primary_db                                                                                                           | 253 kB  00:00:00     
(13/28): extras/7/x86_64/other_db                                                                                                             | 154 kB  00:00:00     
(14/28): mysql-connectors-community/x86_64/filelists_db                                                                                       |  51 kB  00:00:01     
(15/28): mysql-connectors-community/x86_64/primary_db                                                                                         | 109 kB  00:00:01     
(16/28): mysql-connectors-community/x86_64/other_db                                                                                           |  29 kB  00:00:00     
(17/28): mysql-tools-community/x86_64/primary_db                                                                                              |  98 kB  00:00:01     
(18/28): mysql-tools-community/x86_64/other_db                                                                                                |  19 kB  00:00:00     
(19/28): mysql-tools-community/x86_64/filelists_db                                                                                            | 680 kB  00:00:02     
(20/28): mysql57-community/x86_64/primary_db                                                                                                  | 361 kB  00:00:03     
(21/28): mysql57-community/x86_64/filelists_db                                                                                                | 2.1 MB  00:00:03     
(22/28): mysql57-community/x86_64/other_db                                                                                                    |  95 kB  00:00:00     
(23/28): nginx-stable/x86_64/filelists_db                                                                                                     | 112 kB  00:00:00     
(24/28): nginx-stable/x86_64/primary_db                                                                                                       |  87 kB  00:00:00     
(25/28): nginx-stable/x86_64/other_db                                                                                                         |  53 kB  00:00:00     
(26/28): updates/7/x86_64/filelists_db                                                                                                        |  14 MB  00:00:00     
(27/28): updates/7/x86_64/other_db                                                                                                            | 1.5 MB  00:00:00     
(28/28): updates/7/x86_64/primary_db                                                                                                          |  26 MB  00:00:00     
Metadata Cache Created
  • 卸载旧版Docker 相关的软件包的,执行如下指令已经装则删除,未安装也没有影响
## 指令解析
## yum: yum是一个在CentOS及其衍生发行版中用于管理软件包的命令行工具。它可以用来安装、更新、卸载软件包等操作。
## remove: 这是yum命令的一个选项,表示要移除指定的软件包。
## docker, docker-client, docker-client-latest, docker-common, docker-latest, docker-latest-logrotate, docker-logrotate, docker-engine: 这些是要移除的Docker相关软件包的名称。每个软件包名称都在一行上,以空格和反斜杠\分隔,这表示命令在多行上进行了换行,但实际上是一条命令的一部分
[root@iZuf6eqvrwmawrgxqo5kw7Z ~]# yum remove docker \
>                   docker-client \
>                   docker-client-latest \
>                   docker-common \
>                   docker-latest \
>                   docker-latest-logrotate \
>                   docker-logrotate \
>                   docker-engine
Loaded plugins: fastestmirror
No Match for argument: docker
No Match for argument: docker-client
No Match for argument: docker-client-latest
No Match for argument: docker-common
No Match for argument: docker-latest
No Match for argument: docker-latest-logrotate
No Match for argument: docker-logrotate
No Match for argument: docker-engine
No Packages marked for removal
  • 检查系统中可用的软件包更新
## yum会连接到软件源,检查当前系统中安装的软件包是否有可用的更新版本,并列出这些更新的软件包信息
[root@iZuf6eqvrwmawrgxqo5kw7Z ~]# yum check-update
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com

kernel.x86_64                                                  3.10.0-1160.114.2.el7                                                     updates
kernel-devel.x86_64                                            3.10.0-1160.114.2.el7                                                     updates
kernel-headers.x86_64                                          3.10.0-1160.114.2.el7                                                     updates
kernel-tools.x86_64                                            3.10.0-1160.114.2.el7                                                     updates
kernel-tools-libs.x86_64                                       3.10.0-1160.114.2.el7                                                     updates
libX11.x86_64                                                  1.6.7-5.el7_9                                                             updates
libX11-common.noarch                                           1.6.7-5.el7_9                                                             updates
lm_sensors-libs.x86_64                                         3.4.0-8.20160601gitf9185e5.el7_9.1                                        updates
python-perf.x86_64                                             3.10.0-1160.114.2.el7                                                     updates

**## 用于更新系统中所有可用的软件包到最新版本**
[root@iZuf6eqvrwmawrgxqo5kw7Z ~]# yum update
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package kernel.x86_64 0:3.10.0-1160.114.2.el7 will be installed
---> Package kernel-devel.x86_64 0:3.10.0-1160.114.2.el7 will be installed
---> Package kernel-headers.x86_64 0:3.10.0-1160.108.1.el7 will be updated
---> Package kernel-headers.x86_64 0:3.10.0-1160.114.2.el7 will be an update
---> Package kernel-tools.x86_64 0:3.10.0-1160.108.1.el7 will be updated
---> Package kernel-tools.x86_64 0:3.10.0-1160.114.2.el7 will be an update
---> Package kernel-tools-libs.x86_64 0:3.10.0-1160.108.1.el7 will be updated
---> Package kernel-tools-libs.x86_64 0:3.10.0-1160.114.2.el7 will be an update
---> Package libX11.x86_64 0:1.6.7-4.el7_9 will be updated
---> Package libX11.x86_64 0:1.6.7-5.el7_9 will be an update
---> Package libX11-common.noarch 0:1.6.7-4.el7_9 will be updated
---> Package libX11-common.noarch 0:1.6.7-5.el7_9 will be an update
---> Package lm_sensors-libs.x86_64 0:3.4.0-8.20160601gitf9185e5.el7 will be updated
---> Package lm_sensors-libs.x86_64 0:3.4.0-8.20160601gitf9185e5.el7_9.1 will be an update
---> Package python-perf.x86_64 0:3.10.0-1160.108.1.el7 will be updated
---> Package python-perf.x86_64 0:3.10.0-1160.114.2.el7 will be an update
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================================================================================
 Package                            Arch                    Version                                              Repository                Size
================================================================================================================================================
Installing:
 kernel                             x86_64                  3.10.0-1160.114.2.el7                                updates                   52 M
 kernel-devel                       x86_64                  3.10.0-1160.114.2.el7                                updates                   18 M
Updating:
 kernel-headers                     x86_64                  3.10.0-1160.114.2.el7                                updates                  9.1 M
 kernel-tools                       x86_64                  3.10.0-1160.114.2.el7                                updates                  8.2 M
 kernel-tools-libs                  x86_64                  3.10.0-1160.114.2.el7                                updates                  8.1 M
 libX11                             x86_64                  1.6.7-5.el7_9                                        updates                  607 k
 libX11-common                      noarch                  1.6.7-5.el7_9                                        updates                  165 k
 lm_sensors-libs                    x86_64                  3.4.0-8.20160601gitf9185e5.el7_9.1                   updates                   42 k
 python-perf                        x86_64                  3.10.0-1160.114.2.el7                                updates                  8.2 M

Transaction Summary
================================================================================================================================================
Install  2 Packages
Upgrade  7 Packages

Total download size: 104 M
Is this ok [y/d/N]: y
Downloading packages:
No Presto metadata available for updates
(1/9): kernel-devel-3.10.0-1160.114.2.el7.x86_64.rpm                                                                     |  18 MB  00:00:00     
(2/9): kernel-3.10.0-1160.114.2.el7.x86_64.rpm                                                                           |  52 MB  00:00:00     
(3/9): kernel-headers-3.10.0-1160.114.2.el7.x86_64.rpm                                                                   | 9.1 MB  00:00:00     
(4/9): kernel-tools-3.10.0-1160.114.2.el7.x86_64.rpm                                                                     | 8.2 MB  00:00:00     
(5/9): libX11-1.6.7-5.el7_9.x86_64.rpm                                                                                   | 607 kB  00:00:00     
(6/9): libX11-common-1.6.7-5.el7_9.noarch.rpm                                                                            | 165 kB  00:00:00     
(7/9): kernel-tools-libs-3.10.0-1160.114.2.el7.x86_64.rpm                                                                | 8.1 MB  00:00:00     
(8/9): lm_sensors-libs-3.4.0-8.20160601gitf9185e5.el7_9.1.x86_64.rpm                                                     |  42 kB  00:00:00     
(9/9): python-perf-3.10.0-1160.114.2.el7.x86_64.rpm                                                                      | 8.2 MB  00:00:00     
------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                            81 MB/s | 104 MB  00:00:01     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Updating   : kernel-tools-libs-3.10.0-1160.114.2.el7.x86_64                                                                              1/16 
  Updating   : libX11-common-1.6.7-5.el7_9.noarch                                                                                          2/16 
  Updating   : libX11-1.6.7-5.el7_9.x86_64                                                                                                 3/16 
  Updating   : kernel-tools-3.10.0-1160.114.2.el7.x86_64                                                                                   4/16 
  Installing : kernel-3.10.0-1160.114.2.el7.x86_64                                                                                         5/16 
  Updating   : lm_sensors-libs-3.4.0-8.20160601gitf9185e5.el7_9.1.x86_64                                                                   6/16 
  Updating   : kernel-headers-3.10.0-1160.114.2.el7.x86_64                                                                                 7/16 
  Updating   : python-perf-3.10.0-1160.114.2.el7.x86_64                                                                                    8/16 
  Installing : kernel-devel-3.10.0-1160.114.2.el7.x86_64                                                                                   9/16 
  Cleanup    : libX11-1.6.7-4.el7_9.x86_64                                                                                                10/16 
  Cleanup    : kernel-tools-3.10.0-1160.108.1.el7.x86_64                                                                                  11/16 
  Cleanup    : libX11-common-1.6.7-4.el7_9.noarch                                                                                         12/16 
  Cleanup    : kernel-headers-3.10.0-1160.108.1.el7.x86_64                                                                                13/16 
  Cleanup    : kernel-tools-libs-3.10.0-1160.108.1.el7.x86_64                                                                             14/16 
  Cleanup    : lm_sensors-libs-3.4.0-8.20160601gitf9185e5.el7.x86_64                                                                      15/16 
  Cleanup    : python-perf-3.10.0-1160.108.1.el7.x86_64                                                                                   16/16 
  Verifying  : libX11-common-1.6.7-5.el7_9.noarch                                                                                          1/16 
  Verifying  : kernel-tools-3.10.0-1160.114.2.el7.x86_64                                                                                   2/16 
  Verifying  : kernel-devel-3.10.0-1160.114.2.el7.x86_64                                                                                   3/16 
  Verifying  : kernel-tools-libs-3.10.0-1160.114.2.el7.x86_64                                                                              4/16 
  Verifying  : python-perf-3.10.0-1160.114.2.el7.x86_64                                                                                    5/16 
  Verifying  : kernel-headers-3.10.0-1160.114.2.el7.x86_64                                                                                 6/16 
  Verifying  : libX11-1.6.7-5.el7_9.x86_64                                                                                                 7/16 
  Verifying  : lm_sensors-libs-3.4.0-8.20160601gitf9185e5.el7_9.1.x86_64                                                                   8/16 
  Verifying  : kernel-3.10.0-1160.114.2.el7.x86_64                                                                                         9/16 
  Verifying  : kernel-tools-libs-3.10.0-1160.108.1.el7.x86_64                                                                             10/16 
  Verifying  : kernel-headers-3.10.0-1160.108.1.el7.x86_64                                                                                11/16 
  Verifying  : kernel-tools-3.10.0-1160.108.1.el7.x86_64                                                                                  12/16 
  Verifying  : python-perf-3.10.0-1160.108.1.el7.x86_64                                                                                   13/16 
  Verifying  : libX11-1.6.7-4.el7_9.x86_64                                                                                                14/16 
  Verifying  : lm_sensors-libs-3.4.0-8.20160601gitf9185e5.el7.x86_64                                                                      15/16 
  Verifying  : libX11-common-1.6.7-4.el7_9.noarch                                                                                         16/16 

Installed:
  kernel.x86_64 0:3.10.0-1160.114.2.el7                               kernel-devel.x86_64 0:3.10.0-1160.114.2.el7                              

Updated:
  kernel-headers.x86_64 0:3.10.0-1160.114.2.el7                     kernel-tools.x86_64 0:3.10.0-1160.114.2.el7                                 
  kernel-tools-libs.x86_64 0:3.10.0-1160.114.2.el7                  libX11.x86_64 0:1.6.7-5.el7_9                                               
  libX11-common.noarch 0:1.6.7-5.el7_9                              lm_sensors-libs.x86_64 0:3.4.0-8.20160601gitf9185e5.el7_9.1                 
  python-perf.x86_64 0:3.10.0-1160.114.2.el7                       

Complete!
[root@iZuf6eqvrwmawrgxqo5kw7Z ~]# yum update  ## 没有可更新的软件包了
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
No packages marked for update
  • 安装Docker前所需安装包
## 指令解析,如下三个软件包
## yum-utils:这是一个包含了一些实用工具的软件包,可以帮助你更好地管理和使用 YUM 软件包管理器;
## device-mapper-persistent-data:这是用于在Linux上管理设备映射的工具,它提供了一些持久化的数据存储功能,可以帮助你更好地管理存储设备和卷;
## lvm2:这是逻辑卷管理器(Logical Volume Manager,LVM)的第二个版本,它是一个用于管理逻辑卷和物理卷的工具,提供了一些高级的存储管理功能,例如动态调整卷的大小、创建快照等**
[root@iZuf6eqvrwmawrgxqo5kw7Z ~]# yum install -y yum-utils \
>   device-mapper-persistent-data \
>   lvm2
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Package yum-utils-1.1.31-54.el7_8.noarch already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package device-mapper-persistent-data.x86_64 0:0.8.5-3.el7_9.2 will be installed
---> Package lvm2.x86_64 7:2.02.187-6.el7_9.5 will be installed
--> Processing Dependency: lvm2-libs = 7:2.02.187-6.el7_9.5 for package: 7:lvm2-2.02.187-6.el7_9.5.x86_64
--> Processing Dependency: liblvm2app.so.2.2(Base)(64bit) for package: 7:lvm2-2.02.187-6.el7_9.5.x86_64
--> Processing Dependency: libdevmapper-event.so.1.02(Base)(64bit) for package: 7:lvm2-2.02.187-6.el7_9.5.x86_64
--> Processing Dependency: liblvm2app.so.2.2()(64bit) for package: 7:lvm2-2.02.187-6.el7_9.5.x86_64
--> Processing Dependency: libdevmapper-event.so.1.02()(64bit) for package: 7:lvm2-2.02.187-6.el7_9.5.x86_64
--> Running transaction check
---> Package device-mapper-event-libs.x86_64 7:1.02.170-6.el7_9.5 will be installed
---> Package lvm2-libs.x86_64 7:2.02.187-6.el7_9.5 will be installed
--> Processing Dependency: device-mapper-event = 7:1.02.170-6.el7_9.5 for package: 7:lvm2-libs-2.02.187-6.el7_9.5.x86_64
--> Running transaction check
---> Package device-mapper-event.x86_64 7:1.02.170-6.el7_9.5 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================================================================================
 Package                                        Arch                    Version                                  Repository                Size
================================================================================================================================================
Installing:
 device-mapper-persistent-data                  x86_64                  0.8.5-3.el7_9.2                          updates                  423 k
 lvm2                                           x86_64                  7:2.02.187-6.el7_9.5                     updates                  1.3 M
Installing for dependencies:
 device-mapper-event                            x86_64                  7:1.02.170-6.el7_9.5                     updates                  192 k
 device-mapper-event-libs                       x86_64                  7:1.02.170-6.el7_9.5                     updates                  192 k
 lvm2-libs                                      x86_64                  7:2.02.187-6.el7_9.5                     updates                  1.1 M

Transaction Summary
================================================================================================================================================
Install  2 Packages (+3 Dependent packages)

Total download size: 3.2 M
Installed size: 8.0 M
Downloading packages:
(1/5): device-mapper-event-1.02.170-6.el7_9.5.x86_64.rpm                                                                 | 192 kB  00:00:00     
(2/5): device-mapper-event-libs-1.02.170-6.el7_9.5.x86_64.rpm                                                            | 192 kB  00:00:00     
(3/5): device-mapper-persistent-data-0.8.5-3.el7_9.2.x86_64.rpm                                                          | 423 kB  00:00:00     
(4/5): lvm2-2.02.187-6.el7_9.5.x86_64.rpm                                                                                | 1.3 MB  00:00:00     
(5/5): lvm2-libs-2.02.187-6.el7_9.5.x86_64.rpm                                                                           | 1.1 MB  00:00:00     
------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                            15 MB/s | 3.2 MB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : 7:device-mapper-event-libs-1.02.170-6.el7_9.5.x86_64                                                                         1/5 
  Installing : 7:device-mapper-event-1.02.170-6.el7_9.5.x86_64                                                                              2/5 
  Installing : 7:lvm2-libs-2.02.187-6.el7_9.5.x86_64                                                                                        3/5 
  Installing : device-mapper-persistent-data-0.8.5-3.el7_9.2.x86_64                                                                         4/5 
  Installing : 7:lvm2-2.02.187-6.el7_9.5.x86_64                                                                                             5/5 
  Verifying  : 7:device-mapper-event-1.02.170-6.el7_9.5.x86_64                                                                              1/5 
  Verifying  : device-mapper-persistent-data-0.8.5-3.el7_9.2.x86_64                                                                         2/5 
  Verifying  : 7:lvm2-2.02.187-6.el7_9.5.x86_64                                                                                             3/5 
  Verifying  : 7:device-mapper-event-libs-1.02.170-6.el7_9.5.x86_64                                                                         4/5 
  Verifying  : 7:lvm2-libs-2.02.187-6.el7_9.5.x86_64                                                                                        5/5 

Installed:
  device-mapper-persistent-data.x86_64 0:0.8.5-3.el7_9.2                            lvm2.x86_64 7:2.02.187-6.el7_9.5                           

Dependency Installed:
  device-mapper-event.x86_64 7:1.02.170-6.el7_9.5  device-mapper-event-libs.x86_64 7:1.02.170-6.el7_9.5  lvm2-libs.x86_64 7:2.02.187-6.el7_9.5 

Complete!
  • 设置稳定的存储库,以便于安装 Docker CE (Community Edition)
## 指令解析
## sudo: 这个关键词表示以超级用户权限执行后续的命令,因为添加仓库需要管理员权限。
## yum-config-manager: 这是一个yum工具,用于管理yum的配置,包括仓库(repository)的管理。
## --add-repo: 这是yum-config-manager命令的一个选项,表示要添加一个新的仓库。
## http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo: 这是要添加的新仓库的URL。在这个URL中,Docker CE的CentOS版本的软件包仓库被定义。
[root@iZuf6eqvrwmawrgxqo5kw7Z ~]# sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Loaded plugins: fastestmirror
adding repo from: http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
grabbing file http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo

  • 列出 Docker CE 的所有可用版本,并按照版本号进行逆向排序
## 指令解析
## yum list docker-ce --showduplicates: 这个命令用于列出所有可用的 Docker CE 软件包,并显示所有的版本,包括重复的版本。
## |: 这是一个管道操作符,它将命令的输出传递给下一个命令。
## sort -r: 这个命令将对输入进行排序,-r 选项表示按照逆序(降序)进行排序,即从最新版本到最旧版本。
[root@iZuf6eqvrwmawrgxqo5kw7Z ~]# yum list docker-ce --showduplicates | sort -r
https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/stable/repodata/65c4f66e2808d328890505c3c2f13bb35a96f457d1c21a6346191c4dc07e6080-updateinfo.xml.gz: [Errno 12] Timeout on https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/stable/repodata/65c4f66e2808d328890505c3c2f13bb35a96f457d1c21a6346191c4dc07e6080-updateinfo.xml.gz: (28, 'Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds')
Trying other mirror.
 * updates: mirrors.aliyun.com
Loading mirror speeds from cached hostfile
Loaded plugins: fastestmirror
 * extras: mirrors.aliyun.com
docker-ce.x86_64            3:26.0.0-1.el7                      docker-ce-stable
docker-ce.x86_64            3:25.0.5-1.el7                      docker-ce-stable
docker-ce.x86_64            3:25.0.4-1.el7                      docker-ce-stable
docker-ce.x86_64            3:25.0.3-1.el7                      docker-ce-stable
docker-ce.x86_64            3:25.0.2-1.el7                      docker-ce-stable
docker-ce.x86_64            3:25.0.1-1.el7                      docker-ce-stable
docker-ce.x86_64            3:25.0.0-1.el7                      docker-ce-stable
docker-ce.x86_64            3:24.0.9-1.el7                      docker-ce-stable
docker-ce.x86_64            3:24.0.8-1.el7                      docker-ce-stable
docker-ce.x86_64            3:24.0.7-1.el7                      docker-ce-stable
docker-ce.x86_64            3:24.0.6-1.el7                      docker-ce-stable
docker-ce.x86_64            3:24.0.5-1.el7                      docker-ce-stable
docker-ce.x86_64            3:24.0.4-1.el7                      docker-ce-stable
docker-ce.x86_64            3:24.0.3-1.el7                      docker-ce-stable
docker-ce.x86_64            3:24.0.2-1.el7                      docker-ce-stable
docker-ce.x86_64            3:24.0.1-1.el7                      docker-ce-stable
docker-ce.x86_64            3:24.0.0-1.el7                      docker-ce-stable
docker-ce.x86_64            3:23.0.6-1.el7                      docker-ce-stable
docker-ce.x86_64            3:23.0.5-1.el7                      docker-ce-stable
docker-ce.x86_64            3:23.0.4-1.el7                      docker-ce-stable
docker-ce.x86_64            3:23.0.3-1.el7                      docker-ce-stable
docker-ce.x86_64            3:23.0.2-1.el7                      docker-ce-stable
docker-ce.x86_64            3:23.0.1-1.el7                      docker-ce-stable
docker-ce.x86_64            3:23.0.0-1.el7                      docker-ce-stable
docker-ce.x86_64            3:20.10.9-3.el7                     docker-ce-stable
docker-ce.x86_64            3:20.10.8-3.el7                     docker-ce-stable
docker-ce.x86_64            3:20.10.7-3.el7                     docker-ce-stable
docker-ce.x86_64            3:20.10.6-3.el7                     docker-ce-stable
docker-ce.x86_64            3:20.10.5-3.el7                     docker-ce-stable
docker-ce.x86_64            3:20.10.4-3.el7                     docker-ce-stable
docker-ce.x86_64            3:20.10.3-3.el7                     docker-ce-stable
docker-ce.x86_64            3:20.10.24-3.el7                    docker-ce-stable
docker-ce.x86_64            3:20.10.2-3.el7                     docker-ce-stable
docker-ce.x86_64            3:20.10.23-3.el7                    docker-ce-stable
docker-ce.x86_64            3:20.10.22-3.el7                    docker-ce-stable
docker-ce.x86_64            3:20.10.21-3.el7                    docker-ce-stable
docker-ce.x86_64            3:20.10.20-3.el7                    docker-ce-stable
docker-ce.x86_64            3:20.10.19-3.el7                    docker-ce-stable
docker-ce.x86_64            3:20.10.18-3.el7                    docker-ce-stable
docker-ce.x86_64            3:20.10.17-3.el7                    docker-ce-stable
docker-ce.x86_64            3:20.10.16-3.el7                    docker-ce-stable
docker-ce.x86_64            3:20.10.15-3.el7                    docker-ce-stable
docker-ce.x86_64            3:20.10.14-3.el7                    docker-ce-stable
docker-ce.x86_64            3:20.10.1-3.el7                     docker-ce-stable
docker-ce.x86_64            3:20.10.13-3.el7                    docker-ce-stable
docker-ce.x86_64            3:20.10.12-3.el7                    docker-ce-stable
docker-ce.x86_64            3:20.10.11-3.el7                    docker-ce-stable
docker-ce.x86_64            3:20.10.10-3.el7                    docker-ce-stable
docker-ce.x86_64            3:20.10.0-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.9-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.8-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.7-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.6-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.5-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.4-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.3-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.2-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.15-3.el7                    docker-ce-stable
docker-ce.x86_64            3:19.03.14-3.el7                    docker-ce-stable
docker-ce.x86_64            3:19.03.1-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.13-3.el7                    docker-ce-stable
docker-ce.x86_64            3:19.03.12-3.el7                    docker-ce-stable
docker-ce.x86_64            3:19.03.11-3.el7                    docker-ce-stable
docker-ce.x86_64            3:19.03.10-3.el7                    docker-ce-stable
docker-ce.x86_64            3:19.03.0-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.9-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.8-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.7-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.6-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.5-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.4-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.3-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.2-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.1-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.0-3.el7                     docker-ce-stable
docker-ce.x86_64            18.06.3.ce-3.el7                    docker-ce-stable
docker-ce.x86_64            18.06.2.ce-3.el7                    docker-ce-stable
docker-ce.x86_64            18.06.1.ce-3.el7                    docker-ce-stable
docker-ce.x86_64            18.06.0.ce-3.el7                    docker-ce-stable
docker-ce.x86_64            18.03.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            18.03.0.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.12.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.12.0.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.09.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.09.0.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.06.2.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.06.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.06.0.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.03.3.ce-1.el7                    docker-ce-stable
docker-ce.x86_64            17.03.2.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.03.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.03.0.ce-1.el7.centos             docker-ce-stable
 * base: mirrors.aliyun.com
Available Packages
  • 安装指定版本的的Docker,此处为18.09.0 版本
## docker-ce-18.09.0:这是Docker CE的18.09.0版本,是Docker社区版的一个特定版本。Docker CE是Docker社区版,提供了Docker引擎以及一些附加工具和库,用于构建、运行和管理容器化应用程序。
## docker-ce-cli-18.09.0:这是Docker CE的 18.09.0版本的命令行客户端工具,用于与Docker引擎进行交互,例如运行容器、构建镜像等。
## containerd.io:这是一个用于管理容器运行时的工具,它是Docker引擎的一部分,用于控制容器的生命周期,包括运行、暂停、停止等操作。
[root@iZuf6eqvrwmawrgxqo5kw7Z ~]# yum install docker-ce-18.09.0 docker-ce-cli-18.09.0 containerd.io
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package containerd.io.x86_64 0:1.6.28-3.2.el7 will be installed
--> Processing Dependency: container-selinux >= 2:2.74 for package: containerd.io-1.6.28-3.2.el7.x86_64
---> Package docker-ce.x86_64 3:18.09.0-3.el7 will be installed
---> Package docker-ce-cli.x86_64 1:18.09.0-3.el7 will be installed
--> Processing Dependency: libltdl.so.7()(64bit) for package: 1:docker-ce-cli-18.09.0-3.el7.x86_64
--> Running transaction check
---> Package container-selinux.noarch 2:2.119.2-1.911c772.el7_8 will be installed
--> Processing Dependency: policycoreutils-python for package: 2:container-selinux-2.119.2-1.911c772.el7_8.noarch
---> Package libtool-ltdl.x86_64 0:2.4.2-22.el7_3 will be installed
--> Running transaction check
---> Package policycoreutils-python.x86_64 0:2.5-34.el7 will be installed
--> Processing Dependency: setools-libs >= 3.3.8-4 for package: policycoreutils-python-2.5-34.el7.x86_64
--> Processing Dependency: libsemanage-python >= 2.5-14 for package: policycoreutils-python-2.5-34.el7.x86_64
--> Processing Dependency: audit-libs-python >= 2.1.3-4 for package: policycoreutils-python-2.5-34.el7.x86_64
--> Processing Dependency: python-IPy for package: policycoreutils-python-2.5-34.el7.x86_64
--> Processing Dependency: libqpol.so.1(VERS_1.4)(64bit) for package: policycoreutils-python-2.5-34.el7.x86_64
--> Processing Dependency: libqpol.so.1(VERS_1.2)(64bit) for package: policycoreutils-python-2.5-34.el7.x86_64
--> Processing Dependency: libcgroup for package: policycoreutils-python-2.5-34.el7.x86_64
--> Processing Dependency: libapol.so.4(VERS_4.0)(64bit) for package: policycoreutils-python-2.5-34.el7.x86_64
--> Processing Dependency: checkpolicy for package: policycoreutils-python-2.5-34.el7.x86_64
--> Processing Dependency: libqpol.so.1()(64bit) for package: policycoreutils-python-2.5-34.el7.x86_64
--> Processing Dependency: libapol.so.4()(64bit) for package: policycoreutils-python-2.5-34.el7.x86_64
--> Running transaction check
---> Package audit-libs-python.x86_64 0:2.8.5-4.el7 will be installed
---> Package checkpolicy.x86_64 0:2.5-8.el7 will be installed
---> Package libcgroup.x86_64 0:0.41-21.el7 will be installed
---> Package libsemanage-python.x86_64 0:2.5-14.el7 will be installed
---> Package python-IPy.noarch 0:0.75-6.el7 will be installed
---> Package setools-libs.x86_64 0:3.3.8-4.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================================================================================
 Package                                Arch                   Version                                   Repository                        Size
================================================================================================================================================
Installing:
 containerd.io                          x86_64                 1.6.28-3.2.el7                            docker-ce-stable                  35 M
 docker-ce                              x86_64                 3:18.09.0-3.el7                           docker-ce-stable                  19 M
 docker-ce-cli                          x86_64                 1:18.09.0-3.el7                           docker-ce-stable                  14 M
Installing for dependencies:
 audit-libs-python                      x86_64                 2.8.5-4.el7                               base                              76 k
 checkpolicy                            x86_64                 2.5-8.el7                                 base                             295 k
 container-selinux                      noarch                 2:2.119.2-1.911c772.el7_8                 extras                            40 k
 libcgroup                              x86_64                 0.41-21.el7                               base                              66 k
 libsemanage-python                     x86_64                 2.5-14.el7                                base                             113 k
 libtool-ltdl                           x86_64                 2.4.2-22.el7_3                            base                              49 k
 policycoreutils-python                 x86_64                 2.5-34.el7                                base                             457 k
 python-IPy                             noarch                 0.75-6.el7                                base                              32 k
 setools-libs                           x86_64                 3.3.8-4.el7                               base                             620 k

Transaction Summary
================================================================================================================================================
Install  3 Packages (+9 Dependent packages)

Total download size: 69 M
Installed size: 268 M
Is this ok [y/d/N]: y
Downloading packages:
(1/12): audit-libs-python-2.8.5-4.el7.x86_64.rpm                                                                         |  76 kB  00:00:00     
(2/12): checkpolicy-2.5-8.el7.x86_64.rpm                                                                                 | 295 kB  00:00:00     
(3/12): container-selinux-2.119.2-1.911c772.el7_8.noarch.rpm                                                             |  40 kB  00:00:00     
warning: /var/cache/yum/x86_64/7/docker-ce-stable/packages/docker-ce-18.09.0-3.el7.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 621e9f35: NOKEY
Public key for docker-ce-18.09.0-3.el7.x86_64.rpm is not installed
(4/12): docker-ce-18.09.0-3.el7.x86_64.rpm                                                                               |  19 MB  00:00:03     
(5/12): libcgroup-0.41-21.el7.x86_64.rpm                                                                                 |  66 kB  00:00:00     
(6/12): libtool-ltdl-2.4.2-22.el7_3.x86_64.rpm                                                                           |  49 kB  00:00:00     
(7/12): policycoreutils-python-2.5-34.el7.x86_64.rpm                                                                     | 457 kB  00:00:00     
(8/12): python-IPy-0.75-6.el7.noarch.rpm                                                                                 |  32 kB  00:00:00     
(9/12): libsemanage-python-2.5-14.el7.x86_64.rpm                                                                         | 113 kB  00:00:00     
(10/12): setools-libs-3.3.8-4.el7.x86_64.rpm                                                                             | 620 kB  00:00:00     
(11/12): containerd.io-1.6.28-3.2.el7.x86_64.rpm                                                                         |  35 MB  00:00:04     
(12/12): docker-ce-cli-18.09.0-3.el7.x86_64.rpm                                                                          |  14 MB  00:00:01     
------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                            13 MB/s |  69 MB  00:00:05     
Retrieving key from https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
Importing GPG key 0x621E9F35:
 Userid     : "Docker Release (CE rpm) <docker@docker.com>"
 Fingerprint: 060a 61c5 1b55 8a7f 742b 77aa c52f eb6b 621e 9f35
 From       : https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
Is this ok [y/N]: y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : setools-libs-3.3.8-4.el7.x86_64                                                                                             1/12 
  Installing : libcgroup-0.41-21.el7.x86_64                                                                                                2/12 
  Installing : audit-libs-python-2.8.5-4.el7.x86_64                                                                                        3/12 
  Installing : checkpolicy-2.5-8.el7.x86_64                                                                                                4/12 
  Installing : libtool-ltdl-2.4.2-22.el7_3.x86_64                                                                                          5/12 
  Installing : python-IPy-0.75-6.el7.noarch                                                                                                6/12 
  Installing : libsemanage-python-2.5-14.el7.x86_64                                                                                        7/12 
  Installing : policycoreutils-python-2.5-34.el7.x86_64                                                                                    8/12 
  Installing : 2:container-selinux-2.119.2-1.911c772.el7_8.noarch                                                                          9/12 
setsebool:  SELinux is disabled.
  Installing : containerd.io-1.6.28-3.2.el7.x86_64                                                                                        10/12 
  Installing : 1:docker-ce-cli-18.09.0-3.el7.x86_64                                                                                       11/12 
  Installing : 3:docker-ce-18.09.0-3.el7.x86_64                                                                                           12/12 
  Verifying  : 3:docker-ce-18.09.0-3.el7.x86_64                                                                                            1/12 
  Verifying  : libsemanage-python-2.5-14.el7.x86_64                                                                                        2/12 
  Verifying  : 2:container-selinux-2.119.2-1.911c772.el7_8.noarch                                                                          3/12 
  Verifying  : 1:docker-ce-cli-18.09.0-3.el7.x86_64                                                                                        4/12 
  Verifying  : python-IPy-0.75-6.el7.noarch                                                                                                5/12 
  Verifying  : libtool-ltdl-2.4.2-22.el7_3.x86_64                                                                                          6/12 
  Verifying  : checkpolicy-2.5-8.el7.x86_64                                                                                                7/12 
  Verifying  : containerd.io-1.6.28-3.2.el7.x86_64                                                                                         8/12 
  Verifying  : policycoreutils-python-2.5-34.el7.x86_64                                                                                    9/12 
  Verifying  : audit-libs-python-2.8.5-4.el7.x86_64                                                                                       10/12 
  Verifying  : libcgroup-0.41-21.el7.x86_64                                                                                               11/12 
  Verifying  : setools-libs-3.3.8-4.el7.x86_64                                                                                            12/12 

Installed:
  containerd.io.x86_64 0:1.6.28-3.2.el7            docker-ce.x86_64 3:18.09.0-3.el7            docker-ce-cli.x86_64 1:18.09.0-3.el7           

Dependency Installed:
  audit-libs-python.x86_64 0:2.8.5-4.el7        checkpolicy.x86_64 0:2.5-8.el7            container-selinux.noarch 2:2.119.2-1.911c772.el7_8   
  libcgroup.x86_64 0:0.41-21.el7                libsemanage-python.x86_64 0:2.5-14.el7    libtool-ltdl.x86_64 0:2.4.2-22.el7_3                 
  policycoreutils-python.x86_64 0:2.5-34.el7    python-IPy.noarch 0:0.75-6.el7            setools-libs.x86_64 0:3.3.8-4.el7                    

Complete!
  • 启动Docker并查看版本信息
## Docker是服务器----客户端架构。命令行运行docker命令的时候,需要本机有Docker服务。用下面的命令启动
[root@iZuf6eqvrwmawrgxqo5kw7Z ~]# systemctl start docker
[root@iZuf6eqvrwmawrgxqo5kw7Z ~]# docker version
Client:
 Version:           18.09.0
 API version:       1.39
 Go version:        go1.10.4
 Git commit:        4d60db4
 Built:             Wed Nov  7 00:48:22 2018
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
2024-04-11 15:12:02 重新连接成功!
Last login: Thu Apr 11 15:12:02 2024 from 118.31.243.168

Welcome to Alibaba Cloud Elastic Compute Service !

[root@iZuf6eqvrwmawrgxqo5kw7Z ~]# 

如果Docker安装完成后下载镜像非常慢,可使用以下方式换源

2.3 MacOS Docker 换源

Docker设置→Docker Engine配置文件中增加

"registry-mirrors":[
  "http://hub-mirror.163.com",
  "http://docker.mirrors.ustc.edu.cn"
]

2.4 CentOS换源

[root@iZuf6eqvrwmawrgxqo5kw7Z ~]# vim /etc/docker/daemon.json
[root@iZuf6eqvrwmawrgxqo5kw7Z ~]# cat /etc/docker/daemon.json
{
  "registry-mirrors":[
    "http://hub-mirror.163.com",
    "http://docker.mirrors.ustc.edu.cn"
  ]
}

3. Docker命令

3.1 帮助命令

        docker 命令 —help

chenzh12@chenzh12deiMac ~ % docker run --help

Usage:  docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Run a command in a new container

Options:
      --add-host list                  Add a custom host-to-IP mapping (host:ip)
  -a, --attach list                    Attach to STDIN, STDOUT or STDERR
      --blkio-weight uint16            Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)
      --blkio-weight-device list       Block IO weight (relative device weight) (default [])
      --cap-add list                   Add Linux capabilities
      --cap-drop list                  Drop Linux capabilities
      --cgroup-parent string           Optional parent cgroup for the container
      --cgroupns string                Cgroup namespace to use (host|private)
                                       'host':    Run the container in the Docker host's cgroup namespace
                                       'private': Run the container in its own private cgroup namespace
                                       '':        Use the cgroup namespace as configured by the
                                                  default-cgroupns-mode option on the daemon (default)

        完整的help帮助命令如下:

chenzh12@chenzh12deiMac ~ % docker --help

Usage:  docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Options:
      --config string      Location of client config files (default "/Users/chenzh12/.docker")
  -c, --context string     Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default
                           context set with "docker context use")
  -D, --debug              Enable debug mode
  -H, --host list          Daemon socket(s) to connect to
  -l, --log-level string   Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
      --tls                Use TLS; implied by --tlsverify
      --tlscacert string   Trust certs signed only by this CA (default "/Users/chenzh12/.docker/ca.pem")
      --tlscert string     Path to TLS certificate file (default "/Users/chenzh12/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default "/Users/chenzh12/.docker/key.pem")
      --tlsverify          Use TLS and verify the remote
  -v, --version            Print version information and quit

Management Commands:
  builder     Manage builds
  buildx*     Docker Buildx (Docker Inc., v0.8.2)
  compose*    Docker Compose (Docker Inc., v2.6.0)
  config      Manage Docker configs
  **container   Manage containers**
  context     Manage contexts
  image       Manage images
  manifest    Manage Docker image manifests and manifest lists
  network     Manage networks
  node        Manage Swarm nodes
  plugin      Manage plugins
  sbom*       View the packaged-based Software Bill Of Materials (SBOM) for an image (Anchore Inc., 0.6.0)
  scan*       Docker Scan (Docker Inc., v0.17.0)
  secret      Manage Docker secrets
  service     Manage services
  stack       Manage Docker stacks
  swarm       Manage Swarm
  system      Manage Docker
  trust       Manage trust on Docker images
  volume      Manage volumes

Commands:
  attach      Attach local standard input, output, and error streams to a running container
  build       Build an image from a Dockerfile
  commit      Create a new image from a container's changes
  cp          Copy files/folders between a container and the local filesystem
  create      Create a new container
  diff        Inspect changes to files or directories on a container's filesystem
  events      Get real time events from the server
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive
  history     Show the history of an image
  images      List images
  import      Import the contents from a tarball to create a filesystem image
  info        Display system-wide information
  inspect     Return low-level information on Docker objects
  kill        Kill one or more running containers
  load        Load an image from a tar archive or STDIN
  login       Log in to a Docker registry
  logout      Log out from a Docker registry
  logs        Fetch the logs of a container
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  ps          List containers
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Remove one or more images
  run         Run a command in a new container
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  search      Search the Docker Hub for images
  start       Start one or more stopped containers
  stats       Display a live stream of container(s) resource usage statistics
  stop        Stop one or more running containers
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  top         Display the running processes of a container
  unpause     Unpause all processes within one or more containers
  update      Update configuration of one or more containers
  version     Show the Docker version information
  wait        Block until one or more containers stop, then print their exit codes

Run 'docker COMMAND --help' for more information on a command.

To get more help with docker, check out our guides at https://docs.docker.com/go/guides/

        常用的有:

docker version # 查看docker版本
docker images # 查看所有已下载镜像,等价于:docker image ls 命令
docker container ls # 查看所有容器
docker ps #查看正在运行的容器
docker image prune # 清理临时的、没有被使用的镜像文件。-a, --all: 删除所有没有用的镜像,而不仅仅是临时文件;

3.2 镜像命令

        docker images [OPTIONS] [REPOSITORY[:TAG]]

        用于列出本地Docker镜像库中所有镜像的详细信息。[REPOSITORY[:TAG]]用于指定镜像的名称或者TAG。

chenzh12@chenzh12deiMac ~ % docker images
REPOSITORY               TAG       IMAGE ID       CREATED         SIZE
redis                    latest    720b987633ae   12 days ago     158MB
nginx                    latest    81be38025439   12 days ago     192MB
ubuntu                   latest    e343402cadef   5 weeks ago     69.2MB
hello-world              latest    b038788ddb22   6 months ago    9.14kB
docker/getting-started   latest    289dc403af49   10 months ago   46.5MB

        docker pull [OPTIONS] NAME[:TAG]

        用于从Docker Hub或其他Docker镜像仓库中拉取(下载)镜像。

[OPTIONS]是拉取的一些参数;:TAG可指定镜像的版本,默认是lastest(打开Docker Hub官方网站(https://hub.docker.com/))。

chenzh12@chenzh12deiMac ~ % docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
31ce7ceb6d44: Already exists 
f1359798dfe4: Pull complete 
4de1e0313830: Pull complete 
7745719004b6: Pull complete 
0f17732d34d5: Pull complete 
0eb0ed12e64c: Pull complete 
5836f8c1cebc: Pull complete 
Digest: sha256:86e53c4c16a6a276b204b0fd3a8143d86547c967dc8258b3d47c3a21bb68d3c6
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest

        docker search NAME [OPTIONS]

        命令的输出将显示与搜索关键字匹配的所有镜像名称和作者等信息。

# 搜索镜像stars数大于100的
chenzh12@chenzh12deiMac ~ % docker search ubuntu --filter=STARs=100
NAME                DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
ubuntu              Ubuntu is a Debian-based Linux operating sys…   16588     [OK]       
websphere-liberty   WebSphere Liberty multi-architecture images …   297       [OK]       
ubuntu-upstart      DEPRECATED, as is Upstart (find other proces…   115       [OK]       
neurodebian         NeuroDebian provides neuroscience research s…   105       [OK]       
ubuntu/nginx        Nginx, a high-performance reverse proxy & we…   103                        [OK]       

        docker rmi [OPTIONS] IMAGE [IMAGE...]

        docker rmi 命令用于删除本地 Docker 镜像。

chenzh12@chenzh12deiMac ~ % docker rmi my-web-app
Untagged: my-web-app:latest
Deleted: sha256:53021a3db33ba25c1eac711c7e44d0f122212fdc9cc7a79c10129d051795fba4
chenzh12@chenzh12deiMac ~ % docker rmi 6ab070ca2c74
Deleted: sha256:6ab070ca2c7426f8bdc9f92ec8c92762c48d5334bb81d303673cb779e7a449a8

3.3 容器命令

        docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

        docker run用于启动一个新的容器。

        OPTIONS是可选参数,用于配置容器的行为(OPTIONS为-d,可配置后台运行并打印容器的ID);IMAGE是要运行的镜像名称;COMMAND是在容器中要执行的命令;ARG是传递给命令的参数。

常见OPTIONS有:

-d:后台运行容器,并返回容器 ID。
-i:保持标准输入打开,允许你向容器中发送输入。
-t:分配一个伪终端 (pseudo-TTY),通常与 -i 一起使用,以便在容器中使用交互式 shell。
--name:为容器指定一个名称。
-e:设置环境变量。
--rm:容器退出时自动删除容器。
-v 或 --volume:挂载宿主机的目录或者数据卷到容器内部。
-p 或 --publish:将容器的端口映射到宿主机的端口。
--network:指定容器所属的网络。
--restart:设置容器退出时的重启策略。
-u:设置容器内命令执行的用户。

chenzh12@chenzh12deiMac ~ % docker run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 **1. The Docker client contacted the Docker daemon.    # 执行过程
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (arm64v8)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.**

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/
**-d 后台启动容器(守护进程模式运行容器)**
**-p 参数用于将容器的端口映射到宿主机的端口,建立容器内外部连接的桥梁。例如,如果要将容器的端口 80 映射到宿主机的端口 8080,
可以使用以下命令**:
chenzh12@chenzh12deiMac ~ % docker run -d -p 8080:80 nginx
022ad5b2b490423111f0998203f3d4d401bf79c01f770bbf6a3fac6eff42a528
**## -p 宿主机上端口:容器内部使用端口**
chenzh12@chenzh12deiMac ~ % docker run -d -p 8888:6379 redis
bccba0737463a7f105188b6729ae655eab1b501ed23c267b3bad7b14b57bb2ec
chenzh12@chenzh12deiMac ~ % redis-cli -p 8888               
127.0.0.1:8888> keys *
(empty array)
127.0.0.1:8888> set name czh
OK
127.0.0.1:8888> keys *  ## 容器内部Redis
1) "name"
127.0.0.1:8888> 

**-P 参数用于将容器的所有端口映射到宿主机的随机端口。例如,如果要将容器的所有端口映射到宿主机的随机端口,
可以使用以下命令:**
chenzh12@chenzh12deiMac ~ % docker run -d -P nginx        
8a868c975332be9e364b3c270f9eaf61aec3d29f76bf8d076b38a939723b03f7
chenzh12@chenzh12deiMac ~ % docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                   NAMES
8a868c975332   nginx     "/docker-entrypoint.…"   5 seconds ago   Up 4 seconds   0.0.0.0:55001->80/tcp   gracious_nobel     
chenzh12@chenzh12deiMac ~ % netstat -na|grep 55001        
tcp46      0      0  *.55001                *.*                    LISTEN
chenzh12@chenzh12deiMac ~ % docker stop 8a
8a

        docker ps [OPTIONS]

        用于列出所有正在运行的容器。OPTIONS是可选参数,用于配置查询结果的显示格式。

chenzh12@chenzh12deiMac ~ % docker ps
CONTAINER ID   IMAGE                         COMMAND                  CREATED          STATUS          PORTS     NAMES
1ebe6fde63ae   hub.c.163.com/library/nginx   "nginx -g 'daemon of…"   44 seconds ago   Up 43 seconds   80/tcp    boring_robinson

        docker container ls 命令用于列出正在运行的容器。它的作用类似于 docker ps 命令,因为在 Docker 1.13 之后,docker ps 命令的功能已经合并到了 docker container ls 中,成为了 docker container ls 的别名

        netstat -na|grep [PATTERN]

        用于显示当前运行中的所有网络连接及其详细信息。其中,-n 参数表示以协议名称(而不是端口号)显示连接信息;-a 参数表示以所有连接(包括 TCP、UDP、ICMP 等)显示连接信息;| 参数表示将命令的输出结果以管道方式传递给 grep 命令;grep 参数表示要搜索的文本模式。

chenzh12@chenzh12deiMac ~ % netstat -na|grep 8080
tcp46      0      0  *.8080                 *.*                    LISTEN 
chenzh12@chenzh12deiMac ~ % docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                  NAMES
022ad5b2b490   nginx     "/docker-entrypoint.…"   8 seconds ago   Up 7 seconds   0.0.0.0:8080->80/tcp   flamboyant_tu    
chenzh12@chenzh12deiMac ~ % docker stop 02
02

        docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

        用于在容器中执行命令。OPTIONS是可选参数,用于配置执行命令的方式(多个OPTIONS可以共用一个横杠,写在一起);CONTAINER是要执行命令的容器ID或名称;COMMAND是要执行的命令;ARG是传递给命令的参数。

chenzh12@chenzh12deiMac ~ % docker exec -it f0a bash
root@f0a5523de51a:/# ls
bin  boot  dev  etc  home  lib  lib32  lib64  libx32  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@f0a5523de51a:/# pwd
/
root@f0a5523de51a:/# ls
bin  boot  dev  etc  home  lib  lib32  lib64  libx32  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@f0a5523de51a:/# which nginx
/usr/sbin/nginx
root@f0a5523de51a:/# ps -ef  #ps -ef是一个Linux命令,用于列出当前系统中所有进程的详细信息
bash: ps: command not found
root@f0a5523de51a:/# exit
exit

        docker stop [OPTIONS] [CONTAINER...]

        用于停止一个或多个正在运行的容器。OPTIONS是可选参数,用于配置停止容器的选项;CONTAINER是要停止的容器ID或名称,可以指定多个。

chenzh12@chenzh12deiMac ~ % docker stop f
f     # f是ID第一位数字,可以写多位数字,当容器多的时候

        例如,拉取并启动Nginx并运行

**# 拉取镜像**
chenzh12@chenzh12deiMac ~ % docker pull hub.c.163.com/library/nginx:latest 
latest: Pulling from library/nginx
5de4b4d551f8: Pull complete 
d4b36a5e9443: Pull complete 
0af1f0713557: Pull complete 
Digest: sha256:f84932f738583e0169f94af9b2d5201be2dbacc1578de73b09a6dfaaa07801d6
Status: Downloaded newer image for hub.c.163.com/library/nginx:latest
hub.c.163.com/library/nginx:latest
**# 查看本地镜像**
chenzh12@chenzh12deiMac ~ % docker images                                 
REPOSITORY                    TAG       IMAGE ID       CREATED         SIZE
redis                         latest    720b987633ae   12 days ago     158MB
nginx                         latest    81be38025439   12 days ago     192MB
ubuntu                        latest    e343402cadef   5 weeks ago     69.2MB
hello-world                   latest    b038788ddb22   6 months ago    9.14kB
docker/getting-started        latest    289dc403af49   10 months ago   46.5MB
hub.c.163.com/library/nginx   latest    46102226f2fd   6 years ago     109MB
**# 前台运行镜像hub.c.163.com/library/nginx(此处遇到镜像于平台不匹配的问题)**
chenzh12@chenzh12deiMac ~ % docker run hub.c.163.com/library/nginx
WARNING: The requested image's platform (linux/amd64) does not match the detected 
host platform (linux/arm64/v8) and no specific platform was requested
2023/11/14 08:30:12 [emerg] 8#8: io_setup() failed (38: Function not implemented)
**# 查看本地正在运行的容器**
chenzh12@chenzh12deiMac ~ % docker ps
CONTAINER ID   IMAGE                         COMMAND                  CREATED          STATUS          PORTS     NAMES
1ebe6fde63ae   hub.c.163.com/library/nginx   "nginx -g 'daemon of…"   44 seconds ago   Up 43 seconds   80/tcp    boring_robinson
**# 后台运行并返回ID**
chenzh12@chenzh12deiMac ~ % docker run **-d** hub.c.163.com/library/nginx:latest
WARNING: The requested image's platform (linux/amd64) does not match the detected host platform (linux/arm64/v8) and no specific platform was requested
f0a5523de51ac92ddd362fdf031fd97668c4dc418b3b77cbf8839e3c225e47ee
**# 容器中执行命令**
chenzh12@chenzh12deiMac ~ % docker exec -it f0a bash
root@f0a5523de51a:/# ls
bin  boot  dev  etc  home  lib  lib32  lib64  libx32  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@f0a5523de51a:/# pwd
/
root@f0a5523de51a:/# ls
bin  boot  dev  etc  home  lib  lib32  lib64  libx32  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@f0a5523de51a:/# which nginx
/usr/sbin/nginx
root@f0a5523de51a:/# ps -ef  #ps -ef是一个Linux命令,用于列出当前系统中所有进程的详细信息
bash: ps: command not found
root@f0a5523de51a:/# exit
exit
chenzh12@chenzh12deiMac ~ % docker stop f
f
chenzh12@chenzh12deiMac ~ % docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

        docker rm [OPTIONS] CONTAINER [CONTAINER...]

        用于删除本地 Docker 容器。CONTAINER 是必选的,表示要删除的容器 ID。

chenzh12@chenzh12deiMac ~ % docker rm ubuntu      
Error: No such container: ubuntu

        docker logs [OPTIONS] CONTAINER

        用于查看 Docker 容器的日志输出。如果要将日志输出保存到文件,可以使用 -f 选项将日志输出实时输出到文件,或者使用 -t 选项将日志输出按时间保存到文件。

# -tf 显示日志
# --tail number   显示的日志条数
chenzh12@chenzh12deiMac ~ % docker logs -tf --tail 4 e895f01c1dfe
2023-11-16T08:12:00.383588127Z 2023/11/16 08:12:00 [notice] 1#1: start worker process 29
2023-11-16T08:12:00.383594252Z 2023/11/16 08:12:00 [notice] 1#1: start worker process 30
2023-11-16T08:12:00.383790252Z 2023/11/16 08:12:00 [notice] 1#1: start worker process 31
2023-11-16T08:12:00.383797294Z 2023/11/16 08:12:00 [notice] 1#1: start worker process 32

        docker inspect [OPTIONS] CONTAINER [CONTAINER...] 

        命令用于查看 Docker 容器的详细信息,CONTAINER 是必选的,表示要查看详细信息的容器 ID

chenzh12@chenzh12deiMac ~ % docker inspect e895f01c1dfe
[
    {
        "Id": "e895f01c1dfea3ff380efb1bd4c9e0587e803c4b3237ee0f7ec8505d6d695a39",
        "Created": "2023-11-16T08:11:59.960820043Z",
        "Path": "/docker-entrypoint.sh",
        "Args": [
            "nginx",
            "-g",
            "daemon off;"
        ],

        docker cp [OPTIONS] CONTAINER:PATH SOURCE DESTINATION

        用于复制 Docker 容器中的文件或目录。CONTAINER 是必选的,表示要从中复制文件的容器 ID。PATH 是必选的,表示要复制文件或目录的容器内路径。SOURCE 是源文件或目录的路径,DESTINATION 是目标文件或目录的路径。

docker container kill [OPTIONS] CONTAINER [CONTAINER...] 

        docker container kill 命令用于终止运行中的容器。它会向容器发送一个 SIGKILL 信号,强制终止容器的运行。其中:

        OPTIONS:可以包括一些额外的选项,比如 -s 用于指定发送的信号,默认为 SIGKILL。
        CONTAINER:要终止的容器的名称或者 ID。

## a33b0d57b29d为容器ID
chenzh12@chenzh12deiMac ~ % docker container kill a33b0d57b29d
a33b0d57b29d

总结:常见命令原理如下图

Docker常用命令原理图_doker image指令图-CSDN博客文章浏览阅读7.9k次,点赞11次,收藏97次。docker常用命令原理图概览:(一)image layer(镜像层)镜像可以看成是由多个镜像层叠加起来的一个文件系统,镜像层也可以简单理解为一个基本的镜像,而每个镜像层之间通过指针的形式进行叠加。根据上图,镜像层的主要组成部分包括镜像层id,镜像层指针【指向父层】,元数据【layer metadata】包含了docker构建和运行的信息还有父层的层次信息。只读层和读写_doker image指令图https://blog.csdn.net/huwh_/article/details/71308119

4.Dockerfile

        dockerfile 就是用来构建 docker 镜像的构建文本文件,文本内容包含了一条条构建镜像所需的指令和说明。dockerfile仅仅是用来制作镜像的源码文件,是构建容器过程中的指令,docker能够读取dockerfile的指定进行自动构建容器,基于dockerfile制作镜像,每一个指令都会创建一个镜像层,即镜像都是多层叠加而成,因此,层越多,效率越低,创建镜像,层越少越好。

  • 创建目录保存Dockerfile
chenzh12@chenzh12deiMac ~ % pwd
/Users/chenzh12
chenzh12@chenzh12deiMac ~ % mkdir Dockerfile
chenzh12@chenzh12deiMac ~ % cd ./Dockerfile
chenzh12@chenzh12deiMac Dockerfile % ls
chenzh12@chenzh12deiMac Dockerfile % pwd
/Users/chenzh12/Dockerfile
chenzh12@chenzh12deiMac Dockerfile % vim Dockerfile
  • Dockerfile文件中输入的内容如下
# 基础镜像为Ubuntu。基础镜像是构建新镜像的起点,它包含了操作系统和一些基本的软件包
FROM ubuntu
# 定义了容器启动后要执行的默认命令
CMD echo "imooc-java"

**更详细的Dockerfile示例:**
# 使用官方的 OpenJDK 11 作为基础镜像
FROM openjdk:11

# 设置工作目录
WORKDIR /app

# 复制应用程序的 JAR 文件到容器中
COPY target/my-application.jar /app/my-application.jar

# 添加非root用户来运行应用程序(增加安全性)
RUN addgroup --system myuser && adduser --system --ingroup myuser myuser
USER myuser:myuser

# 暴露应用程序运行的端口(如果需要的话)
EXPOSE 8080

# 指定启动应用程序的命令
CMD ["java", "-jar", "my-application.jar"]

        注:CMD 指令只有在 Dockerfile 中出现一次时才会生效,如果有多个CMD指令,只有最后一个会生效

  • 运行命令构建镜像image
# 指令解析
# docker build: 这是 Docker 命令行工具用于构建 Docker 镜像的命令。
# -t myimage: -t 参数用于给镜像打上标签,myimage 是你给该镜像指定的名称。
# .: 这个点表示 Docker 将当前目录作为构建上下文。Docker 将查找 Dockerfile 文件并使用其中的指令来构建镜像
chenzh12@chenzh12deiMac Dockerfile % docker build -t myimage .
[+] Building 0.1s (5/5) FINISHED                                                                                                                  
 => [internal] load build definition from Dockerfile                                                                                         0.0s
 => => transferring dockerfile: 76B                                                                                                          0.0s
 => [internal] load .dockerignore                                                                                                            0.0s
 => => transferring context: 2B                                                                                                              0.0s
 => [internal] load metadata for docker.io/library/ubuntu:latest                                                                             0.0s
 => [1/1] FROM docker.io/library/ubuntu                                                                                                      0.0s
 => exporting to image                                                                                                                       0.0s
 => => exporting layers                                                                                                                      0.0s
 => => writing image sha256:5b364325b4dd90c16741c839dce8c184ba67bc5f6f0f8a5def465e6a7ea2fcb7                                                 0.0s
 => => naming to docker.io/library/myimage                                                                                                   0.0s

Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
chenzh12@chenzh12deiMac Dockerfile % docker images
REPOSITORY                    TAG           IMAGE ID       CREATED         SIZE
ubuntu                        latest        2b7cc08dcdbb   6 weeks ago     69.2MB
myimage                       latest        5b364325b4dd   6 weeks ago     69.2MB
redis                         latest        720b987633ae   5 months ago    158MB
nginx                         latest        81be38025439   5 months ago    192MB
zookeeper                     latest        464b85f63f3b   5 months ago    322MB
tomcat                        latest        d4183e74cd03   5 months ago    454MB
ubuntu                        <none>        e343402cadef   6 months ago    69.2MB
hello-world                   latest        b038788ddb22   11 months ago   9.14kB
docker/getting-started        latest        289dc403af49   15 months ago   46.5MB
nacos/nacos-server            v2.2.0-slim   18f5f2842ec8   15 months ago   300MB
wurstmeister/kafka            latest        db97697f6e28   22 months ago   457MB
kafkamanager/kafka-manager    latest        14a7c1e556f7   4 years ago     464MB
hub.c.163.com/library/nginx   latest        46102226f2fd   6 years ago     109MB
chenzh12@chenzh12deiMac Dockerfile % docker run myimage 
imooc-java

  • 10
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值