CentOS Docker 安装

在 CentOS 系统上,常用的 Docker 安装方式有两种:使用官方脚本安装 和 使用 yum 安装。

 

1. 使用官方脚本安装

安装命令如下:

$ curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun

也可以使用国内 Docker极速下载 daocloud 的一键安装命令安装。

$ curl -sSL https://get.daocloud.io/docker | sh

 

2. 使用 yum 安装

1)卸载旧版本 Docker

如果机器上安装了旧版本的 Docker,那么就需要先行卸载旧的版本。

如果没有安装过 Docker,可以忽略此步骤。即使按照本步骤操作,也不会有副作用。

$ yum remove docker docker-client \
    docker-client-latest \
    docker-common \
    docker-latest \
    docker-latest-logrotate \
    docker-logrotate \
    docker-engine

2)安装最新版本的 Docker(社区版)

$ yum install docker-ce docker-ce-cli containerd.io

3)安装指定版本的 Docker(社区版)

列出并排序您存储库中可用的版本。此示例按版本号(从高到低)对结果进行排序。

$ yum list docker-ce --showduplicates | sort -r
docker-ce.x86_64  3:20.10.6-3.el7  docker-ce-stable 
docker-ce.x86_64  3:20.10.6-3.el7  @docker-ce-stable
docker-ce.x86_64  3:20.10.5-3.el7  docker-ce-stable 
docker-ce.x86_64  3:20.10.4-3.el7  docker-ce-stable 
docker-ce.x86_64  3:20.10.3-3.el7   docker-ce-stable

通过其完整的软件包名称安装特定版本,该软件包名称是软件包名称(docker-ce)加上版本号。版本号位于第二列,从第一个冒号(:)一直到连字符(-)之间的字符串。例如:docker-ce-20.10.6。

$ yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io

Docker 安装完默认未启动。

 

3. 启动 Docker

$ systemctl start docker

 

4. 验证 Docker 安装是否成功

使用 Docker 运行 hello-world 镜像就可以测试 Docker 是否安装成功。

输入指令后,打印以下信息则安装成功:

$ docker run hello-world

执行结果为:
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete 
Digest: sha256:f2266cbfc127c960fd30e76b7c792dc23b588c0db76233517e1891a4e357d519
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/

 

5. 卸载 docker

删除安装包:

$ yum remove docker-ce docker-ce-cli containerd.io

删除镜像、容器、配置文件等数据:

$ rm -rf /var/lib/docker

Docker 主要利用 Linux 内核的 namespace、cgroup 和 unionFS 三种技术,实现了容器的环境隔离、资源控制和镜像打包等功能。Docker 容器的运行必须依赖于 Linux 内核环境,所以 Docker 必须部署在 Linux 内核的系统上。如果要在非 Linux 内核的系统上部署 Docker,就必须安装一个虚拟的 Linux 环境。