[云原生]Docker - 安装&卸载

参考:

系统要求

(以CentOS为例)安装Docker Engine,需要maintain version的CentOS 7 或者 8,Archived version无法支持。

centos-extras仓库必须启用,默认情况下这个仓库是启用的,如果之前禁用了的话,需要重新启用它。

overlay2存储驱动也是必要的。

卸载旧版本

旧版本的Docker叫做docker或者docker-engine。如果这些先前被安装了,先卸载他们,以及相关的依赖包。

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

/var/lib/docker目录中,保留了镜像、容器、卷和网络的一些相关文件。Docker Engine软件包现在叫做docker-ce

安装Docker

可以通过不同的方式安装Docker,取决于个人需求:

  • 大多数用户会通过设置Docker仓库的方式,便于安装与升级,这是一种推荐的方式。

  • 还有的用户会下载RPM包然后手动的安装与升级,适用于一些场景,比如说在一些没有联网的环境中安装Docker。

  • 在测试或者开发环境中,有的用户还会选择使用自动化脚本来进行安装。

方法一:通过repo安装

在安装Docker engine在一台新机器之前,需要设置Docker repository。然后才能够通过repo来进行安装和升级。

设置Repository

安装yum-utils包(提供yum-config-manager命令),然后设置稳定的仓库。

sudo yum install -y yum-utils
sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo
[root@localhost /]# yum repolist
repo id                                repo name
appstream                              CentOS Linux 8 - AppStream
baseos                                 CentOS Linux 8 - BaseOS
docker-ce-stable                       Docker CE Stable - x86_64
extras                                 CentOS Linux 8 - Extras

安装Docker Engine

  1. 安装最新版本的Docker Engine和containerd,或者到下一步去安装特殊的版本。

    sudo yum install docker-ce docker-ce-cli containerd.io
    
  2. 安装特殊版本的Docker Engine,先查看仓库列表中的可用版本,然后选择安装。

    [root@localhost /]# yum list docker-ce --showduplicates | sort -r
    docker-ce.x86_64                3:20.10.9-3.el8                 docker-ce-stable
    docker-ce.x86_64                3:20.10.8-3.el8                 docker-ce-stable
    docker-ce.x86_64                3:20.10.7-3.el8                 docker-ce-stable
    docker-ce.x86_64                3:20.10.6-3.el8                 docker-ce-stable
    docker-ce.x86_64                3:20.10.5-3.el8                 docker-ce-stable
    docker-ce.x86_64                3:20.10.4-3.el8                 docker-ce-stable
    docker-ce.x86_64                3:20.10.3-3.el8                 docker-ce-stable
    docker-ce.x86_64                3:20.10.2-3.el8                 docker-ce-stable
    docker-ce.x86_64                3:20.10.1-3.el8                 docker-ce-stable
    docker-ce.x86_64                3:20.10.11-3.el8                docker-ce-stable
    docker-ce.x86_64                3:20.10.10-3.el8                docker-ce-stable
    docker-ce.x86_64                3:20.10.0-3.el8                 docker-ce-stable
    docker-ce.x86_64                3:19.03.15-3.el8                docker-ce-stable
    docker-ce.x86_64                3:19.03.14-3.el8                docker-ce-stable
    docker-ce.x86_64                3:19.03.13-3.el8                docker-ce-stable
    Docker CE Stable - x86_64                        73 kB/s |  19 kB     00:00    
    Available Packages
    
    sudo yum install docker-ce-<version_string> docker-ce-cli-<version_string> containerd.io
    

​ 补充:如果这里报错,可以添加--allowerasing参数来移除一些冲突的包,如下:

image-20211208141905208

  1. 启动Docker(开机启动)

    systemctl enable --now docker
    
  2. 通过hello-world镜像,检验是否成功安装Docker。

    sudo docker run hello-world
    

升级Docker Engine

去看官网的安装指导,选择新版本安装。Install Docker Engine on CentOS | Docker Documentation

方法二:通过package安装

如果没办法通过Docker's repository安装Docker,你可以通过下载你需要的版本的.rpm包来手动安装Docker,在你需要更新的时候再下载新的包来更新。

  1. Index of linux/centos/ (docker.com)选择CentOS对应版本的仓库。然后查看x86_64/stable/Packages目录,下载相应的.rpm包来安装。

  2. 安装docker

    sudo yum install /path/to/package.rpm
    
  3. 启动Docker

    sudo systemctl enable --now docker
    
  4. 检查是否安装成功

    sudo docker run hello-world
    

方法三:通过脚本安装

Docker官方提供了一个脚本在https://get.docker.com,可以通过它去安装Docker环境无需交互。这个脚本不推荐在生产环境中使用,不过可以作为一个例子用于参考以量身定制属于自己的脚本。

curl -fsSL https://get.docker.com -o get-docker.sh
DRY_RUN=1 sh ./get-docker.sh

卸载Docker

  1. 卸载Docker Engine

    sudo yum remove docker-ce docker-ce-cli containerd.io
    
  2. 镜像、容器、卷还有自定义配置的相关文件还在主机上,没有被自动清除,需要手动删除,执行如下命令可手动删除:

    sudo rm -rf /var/lib/docker
    sudo rm -rf /var/lib/containerd
    

hello-world镜像

hello-world镜像是官方提供用来测试docker环境是否搭建完成用的。

[root@localhost /]# sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
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/
posted @ 2021-12-08 14:47  SkyBiuBiu  阅读(661)  评论(0编辑  收藏  举报