Centos Docker 安装(100%成功)

背景

容器化部署是本文所在专栏的基础知识,也是业界最常用的工具服务部署方案,有效的隔离开很多系统配置,每个服务单独环境,资源相对独立,不相互影响,有容易迁移、扩展、备份 与 集群化、高可用 等特性。

专栏链接: https://blog.csdn.net/weixin_40331132/category_12827717.html

Docker简介

优秀的容器化技术之一,学习得当2~3天就可以上手。两周达成熟手,一个月可以开始软件运维。如果测试工程师掌握了一手漂亮运维技术,那将是非常优秀的靓仔。

1、Docker 安装

1.1、依赖安装

命令(源)

bash 复制代码
sudo yum install -y yum-utils device-mapper-persistent-data lvm2

执行(等待):

执行结果看到了Complete!

表示依赖下载完成

如果下载不成功,回复留言,有很多解决方案,这里写不下。

2.1、配置docker下载源

早期国内源很受欢迎,但经过这两年地球两大村之间技术封锁高压下,国内源也并不是那么好用了。下面给大家分享配置国内源与官方源的两条命令
国内源:

bash 复制代码
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

官方源:

bash 复制代码
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

本文这里为什么又把官方源给出来呢?(大家思考一下)

执行配置国内源命令

2.2、Docker 安装

2.2.1、更新yum缓存

很多时候yum安装东西需要更新缓存,保证安装的东西时又可靠又新又高效

bash 复制代码
sudo yum makecache fast

执行

上图为更新缓存成功

万一更新失败也没有关系,有另一条命令:

bash 复制代码
#手动更新YUM软件仓库缓存,一般是加 fast的更优,但这个稳定且强制
sudo  yum makecache

2.2.2、安装Docker-ce

个人用的就是docker-ce,docker拆分成ee版(商业版)与ce版本(社区版)
命令:

bash 复制代码
sudo yum install docker-ce

执行这条命令,遇到的确认都选择Y

bash 复制代码
[root@localhost ~]#
[root@localhost ~]# sudo yum install docker-ce
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 docker-ce.x86_64 3:26.1.4-1.el7 will be installed
--> Processing Dependency: container-selinux >= 2:2.74 for package: 3:docker-ce-26.1.4-1.el7.x86_64
--> Processing Dependency: containerd.io >= 1.6.24 for package: 3:docker-ce-26.1.4-1.el7.x86_64
--> Processing Dependency: libseccomp >= 2.3 for package: 3:docker-ce-26.1.4-1.el7.x86_64
--> Processing Dependency: docker-ce-cli for package: 3:docker-ce-26.1.4-1.el7.x86_64
--> Processing Dependency: docker-ce-rootless-extras for package: 3:docker-ce-26.1.4-1.el7.x86_64
--> Processing Dependency: libcgroup for package: 3:docker-ce-26.1.4-1.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 containerd.io.x86_64 0:1.6.33-3.1.el7 will be installed
---> Package docker-ce-cli.x86_64 1:26.1.4-1.el7 will be installed
--> Processing Dependency: docker-buildx-plugin for package: 1:docker-ce-cli-26.1.4-1.el7.x86_64
--> Processing Dependency: docker-compose-plugin for package: 1:docker-ce-cli-26.1.4-1.el7.x86_64
---> Package docker-ce-rootless-extras.x86_64 0:26.1.4-1.el7 will be installed
--> Processing Dependency: fuse-overlayfs >= 0.7 for package: docker-ce-rootless-extras-26.1.4-1.el7.x86_64
--> Processing Dependency: slirp4netns >= 0.4 for package: docker-ce-rootless-extras-26.1.4-1.el7.x86_64
---> Package libcgroup.x86_64 0:0.41-21.el7 will be installed
---> Package libseccomp.x86_64 0:2.3.1-4.el7 will be installed
--> Running transaction check
---> Package docker-buildx-plugin.x86_64 0:0.14.1-1.el7 will be installed
---> Package docker-compose-plugin.x86_64 0:2.27.1-1.el7 will be installed
---> Package fuse-overlayfs.x86_64 0:0.7.2-6.el7_8 will be installed
--> Processing Dependency: libfuse3.so.3(FUSE_3.2)(64bit) for package: fuse-overlayfs-0.7.2-6.el7_8.x86_64
--> Processing Dependency: libfuse3.so.3(FUSE_3.0)(64bit) for package: fuse-overlayfs-0.7.2-6.el7_8.x86_64
--> Processing Dependency: libfuse3.so.3()(64bit) for package: fuse-overlayfs-0.7.2-6.el7_8.x86_64
---> 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: 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
---> Package slirp4netns.x86_64 0:0.4.3-4.el7_8 will be installed
--> 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 fuse3-libs.x86_64 0:3.6.1-4.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:
 docker-ce                                              x86_64                              3:26.1.4-1.el7                                          docker-ce-stable                               27 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
 containerd.io                                          x86_64                              1.6.33-3.1.el7                                          docker-ce-stable                               35 M
 docker-buildx-plugin                                   x86_64                              0.14.1-1.el7                                            docker-ce-stable                               14 M
 docker-ce-cli                                          x86_64                              1:26.1.4-1.el7                                          docker-ce-stable                               15 M
 docker-ce-rootless-extras                              x86_64                              26.1.4-1.el7                                            docker-ce-stable                              9.4 M
 docker-compose-plugin                                  x86_64                              2.27.1-1.el7                                            docker-ce-stable                               13 M
 fuse-overlayfs                                         x86_64                              0.7.2-6.el7_8                                           extras                                         54 k
 fuse3-libs                                             x86_64                              3.6.1-4.el7                                             extras                                         82 k
 libcgroup                                              x86_64                              0.41-21.el7                                             base                                           66 k
 libseccomp                                             x86_64                              2.3.1-4.el7                                             base                                           56 k
 libsemanage-python                                     x86_64                              2.5-14.el7                                              base                                          113 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
 slirp4netns                                            x86_64                              0.4.3-4.el7_8                                           extras                                         81 k

Transaction Summary
========================================================================================================================================================================================================
Install  1 Package (+17 Dependent packages)

Total download size: 116 M
Installed size: 407 M
Is this ok [y/d/N]: y
Downloading packages:
(1/18): audit-libs-python-2.8.5-4.el7.x86_64.rpm                                                                                                                                 |  76 kB  00:00:00
(2/18): container-selinux-2.119.2-1.911c772.el7_8.noarch.rpm                                                                                                                     |  40 kB  00:00:00
(3/18): checkpolicy-2.5-8.el7.x86_64.rpm                                                                                                                                         | 295 kB  00:00:00
warning: /var/cache/yum/x86_64/7/docker-ce-stable/packages/docker-buildx-plugin-0.14.1-1.el7.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 621e9f35: NOKEY
Public key for docker-buildx-plugin-0.14.1-1.el7.x86_64.rpm is not installed
(4/18): docker-buildx-plugin-0.14.1-1.el7.x86_64.rpm                                                                                                                             |  14 MB  00:00:00
(5/18): docker-ce-26.1.4-1.el7.x86_64.rpm                                                                                                                                        |  27 MB  00:00:00
(6/18): containerd.io-1.6.33-3.1.el7.x86_64.rpm                                                                                                                                  |  35 MB  00:00:01
(7/18): docker-ce-rootless-extras-26.1.4-1.el7.x86_64.rpm                                                                                                                        | 9.4 MB  00:00:00
(8/18): fuse-overlayfs-0.7.2-6.el7_8.x86_64.rpm                                                                                                                                  |  54 kB  00:00:00
(9/18): libseccomp-2.3.1-4.el7.x86_64.rpm                                                                                                                                        |  56 kB  00:00:00
(10/18): libcgroup-0.41-21.el7.x86_64.rpm                                                                                                                                        |  66 kB  00:00:00
(11/18): libsemanage-python-2.5-14.el7.x86_64.rpm                                                                                                                                | 113 kB  00:00:00
(12/18): python-IPy-0.75-6.el7.noarch.rpm                                                                                                                                        |  32 kB  00:00:00
(13/18): policycoreutils-python-2.5-34.el7.x86_64.rpm                                                                                                                            | 457 kB  00:00:00
(14/18): setools-libs-3.3.8-4.el7.x86_64.rpm                                                                                                                                     | 620 kB  00:00:00
(15/18): slirp4netns-0.4.3-4.el7_8.x86_64.rpm                                                                                                                                    |  81 kB  00:00:00
(16/18): docker-ce-cli-26.1.4-1.el7.x86_64.rpm                                                                                                                                   |  15 MB  00:00:00
(17/18): fuse3-libs-3.6.1-4.el7.x86_64.rpm                                                                                                                                       |  82 kB  00:00:00
(18/18): docker-compose-plugin-2.27.1-1.el7.x86_64.rpm                                                                                                                           |  13 MB  00:00:00
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                                    54 MB/s | 116 MB  00:00:02
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 : libseccomp-2.3.1-4.el7.x86_64                                                                                                                                                       1/18
  Installing : libcgroup-0.41-21.el7.x86_64                                                                                                                                                        2/18
  Installing : slirp4netns-0.4.3-4.el7_8.x86_64                                                                                                                                                    3/18
  Installing : setools-libs-3.3.8-4.el7.x86_64                                                                                                                                                     4/18
  Installing : audit-libs-python-2.8.5-4.el7.x86_64                                                                                                                                                5/18
  Installing : libsemanage-python-2.5-14.el7.x86_64                                                                                                                                                6/18
  Installing : python-IPy-0.75-6.el7.noarch                                                                                                                                                        7/18
  Installing : docker-compose-plugin-2.27.1-1.el7.x86_64                                                                                                                                           8/18
  Installing : fuse3-libs-3.6.1-4.el7.x86_64                                                                                                                                                       9/18
  Installing : fuse-overlayfs-0.7.2-6.el7_8.x86_64                                                                                                                                                10/18
  Installing : checkpolicy-2.5-8.el7.x86_64                                                                                                                                                       11/18
  Installing : policycoreutils-python-2.5-34.el7.x86_64                                                                                                                                           12/18
  Installing : 2:container-selinux-2.119.2-1.911c772.el7_8.noarch                                                                                                                                 13/18
  Installing : containerd.io-1.6.33-3.1.el7.x86_64                                                                                                                                                14/18
  Installing : docker-buildx-plugin-0.14.1-1.el7.x86_64                                                                                                                                           15/18
  Installing : 1:docker-ce-cli-26.1.4-1.el7.x86_64                                                                                                                                                16/18
  Installing : docker-ce-rootless-extras-26.1.4-1.el7.x86_64                                                                                                                                      17/18
  Installing : 3:docker-ce-26.1.4-1.el7.x86_64                                                                                                                                                    18/18
  Verifying  : docker-buildx-plugin-0.14.1-1.el7.x86_64                                                                                                                                            1/18
  Verifying  : checkpolicy-2.5-8.el7.x86_64                                                                                                                                                        2/18
  Verifying  : fuse3-libs-3.6.1-4.el7.x86_64                                                                                                                                                       3/18
  Verifying  : docker-compose-plugin-2.27.1-1.el7.x86_64                                                                                                                                           4/18
  Verifying  : python-IPy-0.75-6.el7.noarch                                                                                                                                                        5/18
  Verifying  : fuse-overlayfs-0.7.2-6.el7_8.x86_64                                                                                                                                                 6/18
  Verifying  : libsemanage-python-2.5-14.el7.x86_64                                                                                                                                                7/18
  Verifying  : slirp4netns-0.4.3-4.el7_8.x86_64                                                                                                                                                    8/18
  Verifying  : 2:container-selinux-2.119.2-1.911c772.el7_8.noarch                                                                                                                                  9/18
  Verifying  : containerd.io-1.6.33-3.1.el7.x86_64                                                                                                                                                10/18
  Verifying  : libseccomp-2.3.1-4.el7.x86_64                                                                                                                                                      11/18
  Verifying  : 3:docker-ce-26.1.4-1.el7.x86_64                                                                                                                                                    12/18
  Verifying  : policycoreutils-python-2.5-34.el7.x86_64                                                                                                                                           13/18
  Verifying  : docker-ce-rootless-extras-26.1.4-1.el7.x86_64                                                                                                                                      14/18
  Verifying  : audit-libs-python-2.8.5-4.el7.x86_64                                                                                                                                               15/18
  Verifying  : setools-libs-3.3.8-4.el7.x86_64                                                                                                                                                    16/18
  Verifying  : 1:docker-ce-cli-26.1.4-1.el7.x86_64                                                                                                                                                17/18
  Verifying  : libcgroup-0.41-21.el7.x86_64                                                                                                                                                       18/18

Installed:
  docker-ce.x86_64 3:26.1.4-1.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     containerd.io.x86_64 0:1.6.33-3.1.el7
  docker-buildx-plugin.x86_64 0:0.14.1-1.el7     docker-ce-cli.x86_64 1:26.1.4-1.el7            docker-ce-rootless-extras.x86_64 0:26.1.4-1.el7        docker-compose-plugin.x86_64 0:2.27.1-1.el7
  fuse-overlayfs.x86_64 0:0.7.2-6.el7_8          fuse3-libs.x86_64 0:3.6.1-4.el7                libcgroup.x86_64 0:0.41-21.el7                         libseccomp.x86_64 0:2.3.1-4.el7
  libsemanage-python.x86_64 0:2.5-14.el7         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
  slirp4netns.x86_64 0:0.4.3-4.el7_8

Complete!
[root@localhost ~]#

遇到 两个 Y确认,

最后

瞧见着这Complete算成了

2.2.2、检查Docker 版本

命令

bash 复制代码
docker version

执行:

shell 复制代码
[root@localhost ~]# docker version
Client: Docker Engine - Community
 Version:           26.1.4
 API version:       1.45
 Go version:        go1.21.11
 Git commit:        5650f9b
 Built:             Wed Jun  5 11:32:04 2024
 OS/Arch:           linux/amd64
 Context:           default
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
[root@localhost ~]#

本文安装时版本为26.1.4

2.3、Docker 运行

1) 设置开启启动

命令

bash 复制代码
sudo systemctl enable docker

执行:

bash 复制代码
[root@localhost ~]# sudo systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@localhost ~]#

不立即启动服务,只设置"今后重启自动启动"。如果你想立即启动,还要执行下面的。

2)立即启动

命令:

bash 复制代码
sudo systemctl start docker

执行:

复制代码
[root@localhost ~]# sudo systemctl start docker
[root@localhost ~]#
#查看运行结果
[root@localhost ~]# sudo systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2025-07-07 23:02:00 CST; 59s ago
     Docs: https://docs.docker.com
 Main PID: 10213 (dockerd)
    Tasks: 10
   Memory: 31.0M
   CGroup: /system.slice/docker.service
           └─10213 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

Jul 07 23:02:00 localhost.localdomain systemd[1]: Starting Docker Application Container Engine...
Jul 07 23:02:00 localhost.localdomain dockerd[10213]: time="2025-07-07T23:02:00.100872870+08:00" level=info msg="Starting up"
Jul 07 23:02:00 localhost.localdomain dockerd[10213]: time="2025-07-07T23:02:00.208259704+08:00" level=info msg="Loading containers: start."
Jul 07 23:02:00 localhost.localdomain dockerd[10213]: time="2025-07-07T23:02:00.733767543+08:00" level=info msg="Firewalld: interface docker0 already part of docker zone, returning"
Jul 07 23:02:00 localhost.localdomain dockerd[10213]: time="2025-07-07T23:02:00.798248972+08:00" level=info msg="Loading containers: done."
Jul 07 23:02:00 localhost.localdomain dockerd[10213]: time="2025-07-07T23:02:00.843578508+08:00" level=info msg="Docker daemon" commit=de5c9cf containerd-snapshotter=false storage-...rsion=26.1.4
Jul 07 23:02:00 localhost.localdomain dockerd[10213]: time="2025-07-07T23:02:00.844146224+08:00" level=info msg="Daemon has completed initialization"
Jul 07 23:02:00 localhost.localdomain dockerd[10213]: time="2025-07-07T23:02:00.923836910+08:00" level=info msg="API listen on /run/docker.sock"
Jul 07 23:02:00 localhost.localdomain systemd[1]: Started Docker Application Container Engine.
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost ~]#

还是截图看得清楚

2、Docker 卸载

很多人安装docker时,步骤错了,或者是各种配置错了,没法继续下去了,可以看这一小节操作,可以干净的将docker脏环境卸载干净

2.2、准备工作

1)kill所有容器

bash 复制代码
docker kill $(docker ps -a -q)

2)删除所有容器

bash 复制代码
docker rm $(docker ps -a -q)

3)删除所有docker镜像

bash 复制代码
docker rmi $(docker images -q)

注意:上面三个操作需要docker运行时操作

4)停止docker 服务

bash 复制代码
systemctl stop docker

5)删除docker相关存储目录:

bash 复制代码
 	rm -rf /etc/docker
    rm -rf /run/docker
    rm -rf /var/lib/dockershim
    rm -rf /var/lib/docker

注意:如是删除不掉上面目录 ,用unount 命令强制卸载dokcer文件系统

bash 复制代码
umount /var/lib/docker/devicemapper

然后再执行5)的命令

2.2、正式卸载

1)查看系统安装的docker包

bash 复制代码
 yum list installed | grep docker

2)yum remove 拼接查出的dokcer 包,以空格间格,安装包从查出的list中复制,一行表示一个表

上图docker包拼接的命令如下:

yum remove docker-ce-cli.x86_64 1:20.10.14-3.el7 @docker-ce-stable docker-scan-plugin.x86_64 0.17.0-3.el7 @docker-ce-stable

运行命令:中间会出现Is this ok[y/N],输入'y'确认

看到Complete!

3)再次查看docker包

yum list installed | grep docker

无包了,表示删除成功

4)输入docker version查看

docker version

成功卸载docker

这时可以再去执行第1中的安装docker教程了。

结语:

现在随着软件交付速度的不断提升,测试人员不再局限于"写用例、点页面",而是逐步走向更深层次的自动化、环境管理、协作交付。Docker 的引入,正是迈向这一变革的重要一步。

学会使用 Docker,不仅意味着测试环境可以实现"一键还原",还代表着我们能更高效地进行接口测试、部署自动化测试平台、接入 CI/CD 流程,甚至模拟复杂依赖服务。而这一切的背后,是测试人员能力的跃升,是团队协作效率的提升,也是质量保障体系的全面升级。

掌握 Docker,不只是工具的熟练使用,更是测试工程师向"质量工程师"角色进化的重要一步。

相关推荐
运维小贺7 分钟前
各服务器厂商调整BIOS睿频教程
linux·运维·服务器·性能优化
人生匆匆11 分钟前
docker进入启动失败的容器
运维·docker·容器
特种加菲猫28 分钟前
指尖上的魔法:优雅高效的Linux命令手册
linux·笔记
★Orange★1 小时前
Linux Kernel kfifo 实现和巧妙设计
linux·运维·算法
bemyrunningdog1 小时前
Mock数据
linux·运维·ubuntu
是阿建吖!1 小时前
【Linux | 网络】网络编程套接字
linux·网络
退役小学生呀2 小时前
十、K8s集群资源合理化分配
linux·云原生·容器·kubernetes·k8s
Winner13002 小时前
Debian、Buildroot 和 Ubuntu 都是基于 Linux 的系统区别
linux·ubuntu·debian
1nullptr3 小时前
UNIX程序设计基本概念和术语
linux
是阿建吖!3 小时前
【Linux | 网络】网络基础
linux·网络