Docker Engine在Centos下的安装

  • A+
所属分类:linux技术
摘要

Centos7.8CentOS 7.8。必须开启 centos-extrasyum仓库源。默认是开启的,如果关闭了,需要重新开启,如下


实践环境

Centos7.8

先决条件

CentOS 7.8。

必须开启 centos-extrasyum仓库源。默认是开启的,如果关闭了,需要重新开启,如下

编辑 /etc/yum.repos.d/CentOS-Base.repo,找到extras配置结点,修改enabled=1后,更新yum源

#additional packages that may be useful [extras] name=CentOS-$releasever - Extras mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras&infra=$infra #baseurl=http://mirror.centos.org/centos/$releasever/extras/$basearch/ gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 

推荐使用 overlay2存储驱动

安装方法

  • 大部分用户使用 Docker 仓库源 并从此源安装 ,为了更方便安装和升级,该方法是最推荐的
  • 有些用户下载RPM安装包,然后 手动安装,手动升级。如果目标机不能上网,这是个不错的选择。
  • 在测试和开发环境,部分用户选择使用安装官方提供给的方便 脚本 安装

使用Docker仓库源安装

第一次安装docker-ce之前,需要设置Docker仓库源。此后,基于该仓库源安装和升级

设置仓库源

安装 yum-utils 包(该软件包提供了yum-config-manager 工具),并且设置稳定版stable仓库源.

$ sudo yum install -y yum-utils # ...略  $ sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo Loaded plugins: fastestmirror adding repo from: https://download.docker.com/linux/centos/docker-ce.repo grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo repo saved to /etc/yum.repos.d/docker-ce.repo 

安装docker-ce

  1. 安装最新版本的docker-cecontainerd

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

    以上命令执行完成后,默认会创建docker组,但是不会添加任何用户到该组,不同方式安装都会创建该组,不再赘述

  2. 安装指定版本的docker-ce

    a. 按顺序显示repo库源中可获取的版本(例中为按版本从高到低排序):

    $ yum list docker-ce --showduplicates | sort -r  docker-ce.x86_64  3:18.09.1-3.el7                     docker-ce-stable docker-ce.x86_64  3:18.09.0-3.el7                     docker-ce-stable docker-ce.x86_64  18.06.1.ce-3.el7                    docker-ce-stable docker-ce.x86_64  18.06.0.ce-3.el7                    docker-ce-stable 

    b. 安装指定版本的docker-ce

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

    例子:安装docker-ce-18.09.1.

    $ sudo yum install docker-ce-18.09.1 docker-ce-cli-18.09.1 containerd.io 
  3. 启动 Docker

    安装完docker-ce后,会在/lib/systemd/system目录下生成docker.service

    $ sudo systemctl start docker $ sudo systemctl enable docker.service # 设置开机自启动 

  4. 确认docker-ce是否正确安装

    可通过运行 sudo docker run hello-world命令测试Docker是否安装成功

    $ sudo docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world b8dfde127a29: Pull complete Digest: sha256:89b647c604b2a436fc3aa56ab1ec515c26b085ac0c15b0d105bc475be15738fb 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-ce

参考 安装指南

通过安装包安装

  1. 通过下载 .rpm 包,然后手工安装。下载地址:https://download.docker.com/linux/centos/ ,找到 x86_64/stable/Packages/目录下载目标版本的 .rpm

  2. rpm安装

    $ sudo yum install /path/to/package.rpm 
  3. 其它剩余步骤同上,略

更新docker-ce

通过rpm -y update命令

通过脚本安装

卸载docker-ce

  1. 卸载docker-ce, CLI, 和 Containerd软件包

    $ sudo yum remove docker-ce docker-ce-cli containerd.io 
  2. 手动移除镜像,容器,和数据卷,及其它自定配置文件

    删除所有镜像,容器,和数据卷:

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