【运维笔记】Centos 7.5 安装 Docker详细步骤

一、注意事项:

  • 注意1:本文的命令使用的是 root 用户登录执行,不是 root 的话所有命令前面要加 sudo
  • 注意2:Linux 内核:官方建议3.10 以上,3.8以上貌似也可
  • 注意3:本文所有需要执行的linux命令均在[root@localhost /]# 结尾之后,除此之外均为执行后的提示,阅读作者的所有文章雷同

二、查看当前系统内核,命令:

  • 说明:3.10.0-862.el7.x86_64,表示内核为:3.10.0,即满足条件
    [root@localhost /]# cd /
    [root@localhost /]# uname -r
    3.10.0-862.el7.x86_64
    [root@localhost /]# 
    

三、更新yum插件(2选1):

  • 升级所有包同时也升级软件和系统内核(本次演示使用此命令,并且之前已经更新过)
    [root@localhost /]# yum -y update
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
     - base: ftp.riken.jp
     - extras: mirror.lzu.edu.cn
     - updates: mirrors.bupt.edu.cn
    No packages marked for update
    
  • 只升级所有包,不升级软件和系统内核
    [root@localhost /]# yum -y upgrade
    

四、Docker常用命令(新机器忽略此项):

  • 查询当前Docker中正在运行的容器,命令:
    [root@localhost /]# docker ps -a
    CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
    
  • 启动docker中所有的容器,命令:
    [root@localhost /]# docker start $(docker ps -a | awk '{ print $1}' | tail -n +2)
    
  • 关闭docker中所有的容器,命令:
    [root@localhost /]# docker stop $(docker ps -a | awk '{ print $1}' | tail -n +2)
    
  • 删除docker中所有容器,命令:
    [root@localhost /]# docker rm $(docker ps -a | awk '{ print $1}' | tail -n +2)
    
  • 删除docker中所有镜像,命令:
    [root@localhost /]# docker rmi $(docker images | awk '{print $3}' |tail -n +2)
    

五、卸载旧版本Docker:

  • 卸载命令1:
    [root@localhost /]# yum remove docker  docker-common docker-selinux docker-engine
    Loaded plugins: fastestmirror
    No Match for argument: docker
    #################### 篇幅有限,中间日志部分内容省略 ####################
    Dependency Removed:
      containerd.io.x86_64 0:1.6.24-3.1.el7  docker-ce.x86_64 3:24.0.6-1.el7 后面一截,篇幅限制,省略。。。
    Complete!
    [root@localhost /]# docker -v
    -bash: /usr/bin/docker: No such file or directory
    [root@localhost /]# 
    
  • 卸载命令2(命令1无法卸载,使用此命令):
    [root@localhost /]# yum remove docker-ce docker-ce-cli containerd.io
    Removed:
      docker-ce-cli.x86_64 1:24.0.6-1.el7
    Complete!
    [root@localhost /]# docker -v
    -bash: /usr/bin/docker: No such file or directory
    [root@localhost /]# 
    

六、安装Docker依赖:

  • 更新yum-utildevicemapper软件包,命令:
    [root@localhost /]# yum install -y yum-utils device-mapper-persistent-data lvm2
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
     * base: ftp.riken.jp
     * extras: mirror.lzu.edu.cn
     * updates: mirrors.bupt.edu.cn
    Package yum-utils-1.1.31-54.el7_8.noarch already installed and latest version
    Package device-mapper-persistent-data-0.8.5-3.el7_9.2.x86_64 already installed and latest version
    Package 7:lvm2-2.02.187-6.el7_9.5.x86_64 already installed and latest version
    Nothing to do
    [root@localhost /]# 
    
  • 设置yum镜像仓库地址,命令(二选一,都能用):
    • 阿里仓库,命令(推荐使用):
      [root@localhost /]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
      Loaded plugins: fastestmirror
      adding repo from: http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
      grabbing file http://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
      [root@localhost /]# 
      
    • 中央仓库,命令:
      [root@localhost /]# yum-config-manager --add-repo http://download.docker.com/linux/centos/docker-ce.repo
      

七、选择Docker版本进行安装:

  • 查看远程仓库有那些docker版本(此处按照最新的版本选择),命令:
    [root@localhost /]# yum list docker-ce --showduplicates | sort -r
     * updates: mirrors.bupt.edu.cn
    Loading mirror speeds from cached hostfile
    Loaded plugins: fastestmirror
     * extras: mirror.lzu.edu.cn
    docker-ce.x86_64            3:24.0.6-1.el7                      docker-ce-stable
    docker-ce.x86_64            3:24.0.5-1.el7                      docker-ce-stable
    #################### 篇幅有限,中间日志部分内容省略 ####################
    docker-ce.x86_64            17.03.1.ce-1.el7.centos             docker-ce-stable
    docker-ce.x86_64            17.03.0.ce-1.el7.centos             docker-ce-stable
     * base: ftp.riken.jp
    Available Packages
    [root@localhost /]# 
    
  • 选择一个版本进行安装(选最新版本往下数第三个一般相对稳定),命令:
    [root@localhost /]# yum -y install docker-ce-24.0.6-1.el7
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
     * base: ftp.riken.jp
    #################### 篇幅有限,中间日志部分内容省略 ####################
    Installed:
      docker-ce.x86_64 3:24.0.6-1.el7 
    Dependency Installed:
      container-selinux.noarch 2:2.119.2-1.911c772.el7_8  containerd.io.x86_64 0:1.6.24-3.1.el7 后面一截,篇幅限制,省略。。。
    Complete!
    
  • 启动Docker & 设置Docker开机自动启动,命令:
    [root@localhost /]# systemctl start docker
    [root@localhost /]# systemctl enable docker
    
  • 查看Docker是否安装完成,命令(显示:Version: xx.x.x表示安装成功):
    [root@localhost /]# docker version
    Client: Docker Engine - Community
     Version:           24.0.6
     API version:       1.43
     Go version:        go1.20.7
     Git commit:        ed223bc
     Built:             Mon Sep  4 12:35:25 2023
     OS/Arch:           linux/amd64
     Context:           default
    
    Server: Docker Engine - Community
     Engine:
      Version:          24.0.6
      API version:      1.43 (minimum version 1.12)
      Go version:       go1.20.7
      Git commit:       1a79695
      Built:            Mon Sep  4 12:34:28 2023
      OS/Arch:          linux/amd64
      Experimental:     false
     containerd:
      Version:          1.6.24
      GitCommit:        61f9fd88f79f081d64d6fa3bb1a0dc71ec870523
     runc:
      Version:          1.1.9
      GitCommit:        v1.1.9-0-gccaecfc
     docker-init:
      Version:          0.19.0
      GitCommit:        de40ad0
    [root@localhost /]# 
    

八、参考文献:

  • centos7安装Docker详细步骤(无坑版教程):https://blog.csdn.net/heima005/article/details/127053152

你可能感兴趣的:(运维,centos,docker,linux)