CentOS安装Docker

1. yum方式安装

在线安装,需要服务器有公网访问权限

  1. 查看操作系统内核版本,尽量保持在3.10及以上

    [root@centos7 ~]# uname -a
    Linux centos7 3.10.0-1127.el7.x86_64 #1 SMP Tue Mar 31 23:36:51 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
    
  2. 更新yum到最新

    [root@centos7 ~]# yum update
    已加载插件:fastestmirror, langpacks
    Loading mirror speeds from cached hostfile
     * base: mirrors.aliyun.com
     * extras: mirrors.aliyun.com
     * updates: mirrors.aliyun.com
    ......
    
  3. 安装依赖包

    [root@centos7 ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
    已加载插件:fastestmirror, langpacks
    Loading mirror speeds from cached hostfile
     * base: mirrors.aliyun.com
     * extras: mirrors.aliyun.com
     * updates: mirrors.aliyun.com
    ......
    
  4. 设置yum源

    中央仓库

    [root@centos7 ~]# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
    已加载插件:fastestmirror, langpacks
    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
    

    阿里云仓库

    [root@centos7 ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
    已加载插件:fastestmirror, langpacks
    adding repo from: https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
    grabbing file https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
    repo saved to /etc/yum.repos.d/docker-ce.repo
    
  5. 查看仓库中所有docker版本

    [root@centos7 ~]# yum list docker-ce --showduplicates | sort -r
    已加载插件:fastestmirror, langpacks
    可安装的软件包
     * updates: mirrors.aliyun.com
    Loading mirror speeds from cached hostfile
     * extras: mirrors.aliyun.com
    docker-ce.x86_64            3:20.10.7-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.x86_64            3:20.10.2-3.el7                     docker-ce-stable
    docker-ce.x86_64            3:20.10.1-3.el7                     docker-ce-stable
    docker-ce.x86_64            3:20.10.0-3.el7                     docker-ce-stable
    ......
    
  6. 选择当前稳定版本进行安装

    安装最新版

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

    安装指定版本

    # yum install -y docker-ce-<version>
    [root@centos7 ~]# yum install -y docker-ce-20.10.5
    

    安装指定版本(包括依赖)

    # yum install -y docker-ce-<version> docker-ce-cli-<version>
    [root@centos7 ~]# yum install -y docker-ce-20.10.5 docker-ce-cli-20.10.5
    
  7. 测试安装是否成功

    [root@centos7 ~]# systemctl start docker
    [root@centos7 ~]# docker version
    Client: Docker Engine - Community
     Version:           20.10.5
     API version:       1.41
     Go version:        go1.13.15
     Git commit:        55c4c88
     Built:             Tue Mar  2 20:33:55 2021
     OS/Arch:           linux/amd64
     Context:           default
     Experimental:      true
    
    Server: Docker Engine - Community
     Engine:
      Version:          20.10.5
      API version:      1.41 (minimum version 1.12)
      Go version:       go1.13.15
      Git commit:       363e9a8
      Built:            Tue Mar  2 20:32:17 2021
      OS/Arch:          linux/amd64
      Experimental:     false
     containerd:
      Version:          1.4.8
      GitCommit:        7eba5930496d9bbe375fdf71603e610ad737d2b2
     runc:
      Version:          1.0.0
      GitCommit:        v1.0.0-0-g84113ee
     docker-init:
      Version:          0.19.0
      GitCommit:        de40ad0
    

2. rpm方式安装

离线安装,不需要服务器有公网访问权限

  1. 到docker官方下载地址https://download.docker.com/linux/centos/7/x86_64/stable/Packages/下载rpm文件,可能有多个rpm文件,如20.10.6版本需要下载docker-ce、docker-ce-cli、docker-ce-rootless-extras、docker-scan-plugin

  2. 然后将下载的rpm文件放到服务器的某个目录下,再执行安装命令,若提示缺失依赖,则根据提示去下载相应缺失的依赖后再重新执行安装命令

    [root@centos7 docker]# pwd
    /opt/software/docker
    [root@centos7 docker]# ll
    总用量 74840
    -rw-r--r-- 1 root root 27885524 721 16:28 docker-ce-20.10.6-3.el7.x86_64.rpm
    -rw-r--r-- 1 root root 34713112 721 16:28 docker-ce-cli-20.10.6-3.el7.x86_64.rpm
    -rw-r--r-- 1 root root  9660840 721 16:31 docker-ce-rootless-extras-20.10.6-3.el7.x86_64.rpm
    -rw-r--r-- 1 root root  4373740 721 16:31 docker-scan-plugin-0.8.0-3.el7.x86_64.rpm
    [root@centos7 docker]# rpm -ivh ./*.rpm
    准备中...                          ################################# [100%]
    正在升级/安装...
       1:docker-scan-plugin-0:0.8.0-3.el7 ################################# [ 25%]
       2:docker-ce-cli-1:20.10.6-3.el7    ################################# [ 50%]
       3:docker-ce-rootless-extras-0:20.10################################# [ 75%]
       4:docker-ce-3:20.10.6-3.el7        ################################# [100%]
    

3. 启动与停止

  1. 启动

    # 启动
    [root@centos7 ~]# systemctl start docker
    # 设置开机自启
    [root@centos7 ~]# systemctl enable docker
    Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
    
  2. 停止

    # 停止
    [root@centos7 ~]# systemctl stop docker
    # 关闭开机自启
    [root@centos7 ~]# systemctl disable docker
    Removed symlink /etc/systemd/system/multi-user.target.wants/docker.service.
    

4. 测试

  1. 配置阿里云镜像仓库地址

    [root@centos7 ~]# mkdir -p /etc/docker
    [root@centos7 ~]# vim /etc/docker/daemon.json
    [root@centos7 ~]# systemctl daemon-reload
    [root@centos7 ~]# systemctl restart docker
    

    daemon.json文件内容如下,注册镜像地址需要自行登录阿里云控制台,在容器镜像服务的镜像加速器中获取

    {
      "registry-mirrors": ["https://******.mirror.aliyuncs.com"]
    }
    
  2. 运行hello-world

    [root@centos7 ~]# docker run hello-world
    Unable to find image 'hello-world:latest' locally
    latest: Pulling from library/hello-world
    b8dfde127a29: Pull complete 
    Digest: sha256:df5f5184104426b65967e016ff2ac0bfcd44ad7899ca3bbcf8e44e4461491a9e
    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. 卸载

  1. 卸载docker-ce

    [root@centos7 ~]# yum remove -y docker-ce
    
  2. 卸载相关依赖包

    [root@centos7 ~]# rpm -qa | grep docker | xargs rpm -e --nodeps
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JackieGGu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值