操作系统要求

要安装 Docker 引擎,您需要 CentOS 7 或 8 的维护版本。不支持或测试存档版本。

centos-extras库必须启用。默认情况下启用此存储库,但如果您已禁用它,则需要 重新启用它。

overlay2推荐使用存储驱动。

卸载旧版本

旧版本的 Docker 被称为docker或docker-engine。如果安装了这些,请卸载它们以及相关的依赖项。

[root@nues ~]# yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine

安装与配置

1.首先安装 Docker 必要依赖包 :

[root@nues ~]# yum install -y yum-utils device-mapper-persistent-data lvm2

2.由于自带 yum 没有 Docker-CE 所以我们需要先增加 docker repo(官方源国内访问不到,添加阿里云的源):

[root@nues ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

3.用 yum 安装 Docker:

[root@nues ~]# yum install -y docker-ce

4.直接yum安装,安装成功后查看版本

[root@nues ~]# docker -v

5.启动docker

[root@nues ~]# systemctl start docker

6.设置开机启动

[root@nues ~]# systemctl enable docker

7.查看docker状态

[root@nues ~]# systemctl status docker

8.配置镜像加速器
因为国内访问Docker Hub较慢,可以使用腾讯云提供的国内镜像源,加速访问Docker Hub(采用阿里云提供镜像源)

[root@nues ~]# mkdir -p /etc/docker
[root@nues ~]# tee /etc/docker/daemon.json <<-'EOF'
> {
> "registry-mirrors": ["https://自已的编码.mirror.aliyuncs.com"]
> }
> EOF
{
"registry-mirrors": ["https://自已的编码.mirror.aliyuncs.com"]
}

[root@nues ~]# systemctl daemon-reload #重新加载服务配置文件
[root@nues ~]# systemctl restart docker #重启服务

验证与应用

1.下载一个官方的 hello-world 镜像到本地

[root@nues ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
...

2.下载好的镜像就会出现在镜像列表里

[root@nues ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 26 hours ago 13.3kB

3.运行容器

[root@nues ~]# docker run hello-world

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

4.查看所有的容器信息

[root@nues ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6a0df7b2d4eb hello-world "/hello" 44 seconds ago Exited (0) 44 seconds ago intelligent_franklin
部分参数释义:
.CONTAINER ID 容器ID
.Image 镜像ID
.COMMAND 指令
.Created 创建容器的时间点.
.Ports 暴露的端口.
.Status 容器状态.
.Names 容器名称.