CentOS 7 安装 Docker 环境与前置准备
参考官方文档链接:https://docs.docker.com/engine/installation/linux/centos/ 。本文内容主要基于官方文档整理,若读者具备较好的英文阅读能力,建议直接查阅官方资料;若英语基础较弱,可参考以下经过翻译整理的说明。
后续所有操作步骤均需在 root 用户权限下执行。
Docker 的安装要求系统为 64 位架构且内核版本高于 3.10。因此,若使用 CentOS 系统,必须确保版本为 CentOS 7.0 或更高。本例中使用的具体环境为 CentOS 7.2 minimal 版本。
通过命令 uname -r 查看当前内核版本,示例输出为:3.10.0-327.28.3.el7.x86_64。
在安装 Docker 之前,建议先执行全系统的软件包升级,以确保依赖库的最新状态:
yum -y update
第一步是配置 Yum 软件源。
为确保安装过程顺利,首先使用 yum update 更新现有的 Yum 包。如果长时间未更新,可能需要处理大量包的升级(如 1500+ 个),请耐心等待。随后,需要在 Yum 软件库中添加 Docker 的配置文件:
# tee /etc/yum.repos.d/docker.repo <<-'EOF'
[dockerrepo]
name=Docker Repository
baseurl=https://yum.dockerproject.org/repo/main/centos/7/
enabled=1
gpgcheck=1
gpgkey=https://yum.dockerproject.org/gpg
EOF
第二步是安装 Docker。
完成 Yum 源配置后,安装过程变得非常简单,只需执行一条命令即可:
# yum install docker-engine
第三步是启动 Docker 服务。 当所有准备工作就绪后,使用 start 命令来启动 Docker 守护进程:
# service docker start
第四步是测试 Hello World。 程序员似乎对打印 hello-world 情有独钟,在使用 Docker 时也不例外。我们首先尝试运行官方的 hello-world 镜像,其原理是将预先写好的镜像下载到本地并执行。
使用以下命令进行测试:
# docker run hello-world
如果控制台输出类似如下信息,则表明 Docker 环境安装成功:

在此过程中,我首次尝试时遇到了失败,系统显示错误信息:
Unable to find image 'hello-world:latest' locally
docker: Error response from daemon: Get https://registry-1.docker.io/v2/library/hello-world/manifests/latest: Get https://auth.docker.io/token?scope=repository%3Alibrary%2Fhello-world%3Apull&service=registry.docker.io: net/http: TLS handshake timeout.
See 'docker run --help'.
再次尝试后操作成功,这可能是因为网络访问受限导致的连接超时问题。
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
c04b14da8d14: Pull complete
Digest: sha256:0256e8a36e2070f7bf2d0b0763dbabdd67798512411de4cdcf9431a1feb60fd9
Status: Downloaded newer image for hello-world:latest
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.
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 Hub account:
https://hub.docker.com
For more examples and ideas, visit:
https://docs.docker.com/engine/userguide/
设置 Docker 开机自启动:
chkconfig docker on
调整 Docker 的数据存储目录。
若需将独立的数据分区设置为 Docker 的数据目录,需要手动将原目录下的数据迁移至新的存储分区,并将新分区挂载到 /var/lib/docker 目录下。
service docker stop
执行数据拷贝及分区挂载操作:

service docker start
第四步(进阶):创建一个专用的 docker 用户组。
虽然 Docker 默认需要 root 权限运行,但可以通过创建专用用户组的方式,允许具备 sudo 权限的普通用户管理 Docker 服务,从而提升安全性。
复制代码 代码如下: # groupadd docker # usermod -aG docker bjxtb
退出当前会话并重新登录后,即可使用 bjxtb 用户直接管理 Docker:
$ docker run hello-world
接下来,运行一个 Docker 容器进行演示:
[root@localhost ~]# docker run -i -t centos /bin/bash
[root@dbf66395436d /]#
我们可以看到,CentOS 容器已成功启动,并返回了 bash 提示符。在 docker 命令中,我们使用了 “-i” 选项来捕获标准输入输出,以及 “-t” 选项来分配一个终端或控制台。若要断开与容器的连接,只需输入 exit。
[root@cd05639b3f5c /]# cat /etc/RedHat-release
CentOSLinux release 7.0.1406(Core)
[root@cd05639b3f5c /]#exit
exit
[root@localhost ~]#
我们还可以搜索基于 Fedora 和 Ubuntu 操作系统的容器镜像。
[root@localhost ~]# docker search ubuntu
[root@localhost ~]# docker search fedora
该命令用于显示当前正在运行的容器列表。
