系统要求
官网提示如果要安装 Docker Engine,您需要一个 CentOS 7 以及以上的稳定版本。
卸载旧版本
较旧的 Docker 版本为 docker 或 docker-engine 。 如果已安装这些程序,请卸载它们以及相关 的依赖项。
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
Docker 镜像、容器、数据卷和网络数据都保存在 /var/lib/docker/ 。新的 Docker 引擎包现在为 Docker-ce 。
设置 yum 源
安装 yum
-
utils
软件包(提供了
yum
-
config
-
manager
程序)并设置稳定的
yum
源方便下载
Docker Engine
。
# 安装 yum-utils, 它提供了 yum-config-manager,可用来管理yum源
sudo yum install -y yum-utils
# 设置 yum 源为阿里云方便下载 Docker Engine
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Docker 安装
安装最新版本的 Docker Engine
和容器。
sudo yum -y install docker-ce docker-ce-cli containerd.io
安装过程中如果提示您接受 GPG 密钥,请验证指纹是否与 060A 61C5 1B55 8A7F 742B 77AA C52F EB6B 621E 9F35 匹配,如果是,请接受。
Docker 的启动与停止
# 启动
docker sudo systemctl start docker
# 停止
docker sudo systemctl stop docker
# 重启
docker sudo systemctl restart docker
# 设置开机启动
sudo systemctl enable docker
# 查看 docker 状态
sudo systemctl status docker
# 查看 docker 内容器的运行状态
sudo docker stats
# 查看 docker 概要信息
sudo docker info
# 查看 docker 帮助文档
sudo docker --help
安装校验
[root@localhost local]# docker -v
Docker version 20.10.11, build dea9396
[root@localhost local]# docker version
Client: Docker Engine - Community
Version: 20.10.11
API version: 1.41
Go version: go1.16.9
Git commit: dea9396
Built: Thu Nov 18 00:38:53 2021
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.11
API version: 1.41 (minimum version 1.12)
Go version: go1.16.9
Git commit: 847da18
Built: Thu Nov 18 00:37:17 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.4.12
GitCommit: 7b11cfaabd73bb80907dd23182b9347b4245eb5d
runc:
Version: 1.0.2
GitCommit: v1.0.2-0-g52b36a2
docker-init:
Version: 0.19.0
GitCommit: de40ad0
[root@localhost local]#
配置镜像加速
Docker 从
Docker Hub
拉取镜像,因为是从国外获取,所以速度较慢,会出现以下情况:
[root@localhost ~]# docker run hello-world 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: net/http: TLS handshake timeout.
See 'docker run --help'.
可以通过配置国内镜像源的方式,从国内获取镜像,提高拉取速度。这里介绍中国科学技术大学 (LUG@USTC)的开源镜像:https://docker.mirrors.ustc.edu.cn 和网易的开源镜像:http://hub-mirror.c.163.com
USTC 是老牌的
Linux
镜像服务提供者了,
USTC
的
Docker
镜像加速器速度很快。
USTC
和网易的 优势之一就是不需要注册,属于真正的公共服务。(也可以使用阿里等其他服务商的镜像加速服务)
编辑文件 daemon.json 。
vi /etc/docker/daemon.json
在文件中输入以下内容并保存。
{
"registry-mirrors": [
"http://hub-mirror.c.163.com",
"https://docker.mirrors.ustc.edu.cn"
]
}
重新加载配置信息及重启 Docker 服务。
# 重新加载某个服务的配置文件
sudo systemctl daemon-reload
# 重新启动 docker
sudo systemctl restart docker
hello-world
通过运行 hello
-
world
镜像来验证
Docker Engine
是否已正确安装。
[root@localhost local]# docker run hello-world
# 本地找不到 hello-world 镜像
Unable to find image 'hello-world:latest' locally
# 拉取最新版本的 hello-world 镜像
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:cc15c5b292d8525effc0f89cb299f1804f3a725c8d05e158653a563f15e4f685
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.
(amd64)
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/
docker run hello-world 命令执行流程图如下。
通过以上步骤大家已完成 Docker
安装的所有工作,接下来就可以通过学习镜像命令和容器命令更
加熟悉
Docker
的使用。