本次部署示例为 CentOS 7 ,其他系统部署参考[这里](Install Docker Engine | Docker Documentation)。

一、基础环境

二、Docker 部署

1、设置仓库

(1)yum-utils 包安装

1
yum install -y yum-utils

(2)设置 stable 镜像仓库

可以使用国内仓库,也可以使用Docker 国外官方仓库,这里推荐使用国内镜像仓库,国外的可能由于网络等原因导致报错。

1
2
3
# 国内镜像仓库设置

yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
1
2
3
# 国外镜像仓库设置

yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

2、更新 yum 软件包索引

1
yum makecache fast

3、安装 docker Engine

1
yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin -y

4、启动 docker

1
systemctl start docker

三、验证

1、版本查看

使用 docker version 命令,有以下输出则安装成功。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[root@localhost ~]# docker version
Client: Docker Engine - Community
Version: 20.10.17
API version: 1.41
Go version: go1.17.11
Git commit: 100c701
Built: Mon Jun 6 23:05:12 2022
OS/Arch: linux/amd64
Context: default
Experimental: true

Server: Docker Engine - Community
Engine:
Version: 20.10.17
API version: 1.41 (minimum version 1.12)
Go version: go1.17.11
Git commit: a89b842
Built: Mon Jun 6 23:03:33 2022
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.6.6
GitCommit: 10c12954828e7c7c9b6e0ea9b0c02b01407d3ae1
runc:
Version: 1.1.2
GitCommit: v1.1.2-0-ga916309
docker-init:
Version: 0.19.0
GitCommit: de40ad0

2、容器运行测试

运行容器 hello-world ,出现以下信息,则说明 Docker 环境安装成功。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[root@localhost ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:13e367d31ae85359f42d637adf6da428f76d75dc9afeb3c21faea0d976f5c651
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

1、卸载软件包

卸载Docker 引擎 ,CLI ,Containerd 和 Docker Compose

1
yum remove docker-ce docker-ce-cli containerd.io docker-compose-plugin

2、本地文件删除

卸载 Docker 不会自动删除主机上的映像、容器、卷或自定义配置文件。要删除所有图像、容器和卷,请执行以下操作:

1
2
rm -rf /var/lib/docker
rm -rf /var/lib/containerd

参考文档

Install Docker Engine on CentOS | Docker Documentation