• Docker安装与部属


    1 使用官方安装脚本自动安装

    实际上就是下载一个安装脚本,再执行安装(不推荐,自动安装最新版本,不能自行选择版本)

    [root@johnnyg05 ~]# curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
    //末尾表示下载docker并选择是阿里云的镜像(自动下载最新版本)
    
    • 1
    • 2
    [root@johnnyg05 ~]# systemctl enable --now docker
    Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
    //查看docker版本
    [root@johnnyg05 ~]# docker version 
    Client: Docker Engine - Community
     Version:           20.10.17
     API version:       1.41
     Go version:        go1.17.11
     Git commit:        100c701
     Built:             Mon Jun  6 23:05:12 2022
     OS/Arch:           linux/amd64
     Context:           default
     Experimental:      true
    
    Server: Docker Engine - Community
     Engine:
      Version:          20.10.17
      API version:      1.41 (minimum version 1.12)
      Go version:       go1.17.11
      Git commit:       a89b842
      Built:            Mon Jun  6 23:03:33 2022
      OS/Arch:          linux/amd64
      Experimental:     false
     containerd:
      Version:          1.6.7
      GitCommit:        0197261a30bf81f1ee8e6a4dd2dea0ef95d67ccb
     runc:
      Version:          1.1.3
      GitCommit:        v1.1.3-0-g6724737
     docker-init:
      Version:          0.19.0
      GitCommit:        de40ad0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    [root@johnnyg05 ~]# ll /etc/yum.repos.d/
    total 16
    -rw-r--r-- 1 root root  614 Aug  9 21:26 CentOS-Base.repo
    -rw-r--r-- 1 root root  230 Aug  9 21:26 CentOS-Epel.repo
    -rw-r--r-- 1 root root  616 Nov 23  2020 CentOS-x86_64-kernel.repo
    -rw-r--r-- 1 root root 2081 Aug  9 21:28 docker-ce.repo
    //可以清晰看到有一个docker-ce.repo文件
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    2 CentOS 7 (使用yum进行安装,推荐)

    2.1 添加docker-ce 源信息

    [root@johnnyg05 ~]# wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
    
    • 1

    2.2 修改docker-ce 源

    [root@johnnyg05 ~]# sed -i 's@download.docker.com@mirrors.tuna.tsinghua.edu.cn/docker-ce@g' /etc/yum.repos.d/docker-ce.repo
    
    • 1

    2.3 更新并安装 Docker-CE

    [root@johnnyg05 ~]# yum makecache fast
    [root@johnnyg05 ~]# yum install docker-ce -y
    //默认安装最新版本
    
    • 1
    • 2
    • 3
    [root@johnnyg05 ~]# systemctl enable --now docker
    Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
    //查看docker版本
    [root@johnnyg05 ~]# docker version
    Client: Docker Engine - Community
     Version:           20.10.17
     API version:       1.41
     Go version:        go1.17.11
     Git commit:        100c701
     Built:             Mon Jun  6 23:05:12 2022
     OS/Arch:           linux/amd64
     Context:           default
     Experimental:      true
    
    Server: Docker Engine - Community
     Engine:
      Version:          20.10.17
      API version:      1.41 (minimum version 1.12)
      Go version:       go1.17.11
      Git commit:       a89b842
      Built:            Mon Jun  6 23:03:33 2022
      OS/Arch:          linux/amd64
      Experimental:     false
     containerd:
      Version:          1.6.7
      GitCommit:        0197261a30bf81f1ee8e6a4dd2dea0ef95d67ccb
     runc:
      Version:          1.1.3
      GitCommit:        v1.1.3-0-g6724737
     docker-init:
      Version:          0.19.0
      GitCommit:        de40ad0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    2.4 安装指定版本的docker

    2.4.1 查看都有哪些版本

    [root@johnnyg05 ~]# yum list docker-ce.x86_64 --showduplicates | sort -r
    
    • 1

    2.4.2 下载指定版本

    [root@johnnyg05 ~]# yum install docker-ce-18.09.9-3.el7 -y
    
    • 1
    [root@johnnyg05 ~]# systemctl enable --now docker
    Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
    //查看docker版本
    [root@johnnyg05 ~]# docker version
    Client: Docker Engine - Community
     Version:           20.10.17
     API version:       1.39
     Go version:        go1.17.11
     Git commit:        100c701
     Built:             Mon Jun  6 23:05:12 2022
     OS/Arch:           linux/amd64
     Context:           default
     Experimental:      true
    
    Server: Docker Engine - Community
     Engine:
      Version:          18.09.9
      API version:      1.39 (minimum version 1.12)
      Go version:       go1.11.13
      Git commit:       039a7df
      Built:            Wed Sep  4 16:22:32 2019
      OS/Arch:          linux/amd64
      Experimental:     false
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    3 CentOS 7 (二进制安装,推荐)

    下载包网址链接:https://download.docker.com/linux/static/stable/x86_64/
    安装版本:docker-20.10.17-ce

    3.1 下载安装

    [root@johnnyg05 ~]# wget -c https://download.docker.com/linux/static/stable/x86_64/docker-20.10.17.tgz -P /usr/local/
    [root@johnnyg05 ~]# cd /usr/local/
    [root@johnnyg05 local]# ll
    total 63512
    drwxr-xr-x.  2 root root     4096 Apr 11  2018 bin
    -rw-r--r--   1 root root 64988857 Aug  5 07:58 docker-20.10.17.tgz
    drwxr-xr-x.  2 root root     4096 Apr 11  2018 etc
    drwxr-xr-x.  2 root root     4096 Apr 11  2018 games
    drwxr-xr-x.  2 root root     4096 Apr 11  2018 include
    drwxr-xr-x.  2 root root     4096 Apr 11  2018 lib
    drwxr-xr-x.  2 root root     4096 Apr 11  2018 lib64
    drwxr-xr-x.  2 root root     4096 Apr 11  2018 libexec
    drwxr-xr-x  16 root root     4096 Aug  9 22:01 qcloud
    drwxr-xr-x.  2 root root     4096 Apr 11  2018 sbin
    drwxr-xr-x.  5 root root     4096 Mar  7  2019 share
    drwxr-xr-x.  2 root root     4096 Apr 11  2018 src
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    [root@johnnyg05 local]# tar -xf /usr/local/docker-20.10.17.tgz 
    [root@johnnyg05 local]# ll
    total 63516
    drwxr-xr-x.  2 root root     4096 Apr 11  2018 bin
    drwxrwxr-x   2 1000 1000     4096 Jun  7 07:03 docker
    -rw-r--r--   1 root root 64988857 Aug  5 07:58 docker-20.10.17.tgz
    drwxr-xr-x.  2 root root     4096 Apr 11  2018 etc
    drwxr-xr-x.  2 root root     4096 Apr 11  2018 games
    drwxr-xr-x.  2 root root     4096 Apr 11  2018 include
    drwxr-xr-x.  2 root root     4096 Apr 11  2018 lib
    drwxr-xr-x.  2 root root     4096 Apr 11  2018 lib64
    drwxr-xr-x.  2 root root     4096 Apr 11  2018 libexec
    drwxr-xr-x  16 root root     4096 Aug  9 22:01 qcloud
    drwxr-xr-x.  2 root root     4096 Apr 11  2018 sbin
    drwxr-xr-x.  5 root root     4096 Mar  7  2019 share
    drwxr-xr-x.  2 root root     4096 Apr 11  2018 src
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    [root@johnnyg05 local]# cd docker/
    [root@johnnyg05 docker]# ll
    total 204048
    -rwxr-xr-x 1 1000 1000 39838504 Jun  7 07:03 containerd
    -rwxr-xr-x 1 1000 1000  7585792 Jun  7 07:03 containerd-shim
    -rwxr-xr-x 1 1000 1000  9859072 Jun  7 07:03 containerd-shim-runc-v2
    -rwxr-xr-x 1 1000 1000 23834624 Jun  7 07:03 ctr
    -rwxr-xr-x 1 1000 1000 50511896 Jun  7 07:03 docker
    -rwxr-xr-x 1 1000 1000 60261480 Jun  7 07:03 dockerd
    -rwxr-xr-x 1 1000 1000   704520 Jun  7 07:03 docker-init
    -rwxr-xr-x 1 1000 1000  2559454 Jun  7 07:03 docker-proxy
    -rwxr-xr-x 1 1000 1000 13774272 Jun  7 07:03 runc
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    3.2 配置启动脚本

    //从其它使用yum安装相同版本docker的主机中传送启动脚本至本机
    [root@johnnyg01 ~]# scp /usr/lib/systemd/system/docker.service 10.0.2.13:/usr/lib/systemd/system/
    [root@johnnyg01 ~]# scp /usr/lib/systemd/system/docker.socket 10.0.2.13:/usr/lib/systemd/system/
    [root@johnnyg01 ~]# scp /usr/lib/systemd/system/containerd.service 10.0.2.13:/usr/lib/systemd/system/
    
    • 1
    • 2
    • 3
    • 4
    //创建docker系统账号
    [root@johnnyg05 ~]# useradd -r docker
    
    • 1
    • 2

    3.3 创建软链接(启动脚本路径不一致)

    //要与启动脚本中指示的路径一致
    [root@johnnyg05 ~]# ln -s /usr/local/docker/* /usr/bin/
    
    • 1
    • 2

    3.4 配置生效环境变量(方便使用docker命令)

    [root@johnnyg05 ~]# echo "export PATH=/usr/local/docker:$PATH" > /etc/profile.d/docker.sh
    [root@johnnyg05 ~]# source /etc/profile.d/docker.sh
    
    • 1
    • 2

    3.5 启动docker

    [root@johnnyg05 ~]# systemctl enable --now docker
    Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
    //查看docker版本
    [root@johnnyg05 ~]# docker version
    Client:
     Version:           20.10.17
     API version:       1.41
     Go version:        go1.17.11
     Git commit:        100c701
     Built:             Mon Jun  6 22:56:42 2022
     OS/Arch:           linux/amd64
     Context:           default
     Experimental:      true
    
    Server: Docker Engine - Community
     Engine:
      Version:          20.10.17
      API version:      1.41 (minimum version 1.12)
      Go version:       go1.17.11
      Git commit:       a89b842
      Built:            Mon Jun  6 23:01:45 2022
      OS/Arch:          linux/amd64
      Experimental:     false
     containerd:
      Version:          v1.6.6
      GitCommit:        10c12954828e7c7c9b6e0ea9b0c02b01407d3ae1
     runc:
      Version:          1.1.2
      GitCommit:        v1.1.2-0-ga916309f
     docker-init:
      Version:          0.19.0
      GitCommit:        de40ad0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    3.6 配置Docker命令补全脚本

    //从其它使用yum安装相同版本docker的主机中传送docker补全文件至本机
    [root@johnnyg01 ~]# scp /usr/share/bash-completion/completions/docker 10.0.2.12:/usr/share/bash-completion/completions/
    
    • 1
    • 2
    //通过source调用刷新一下
    [root@johnnyg05 docker]# source /usr/share/bash-completion/completions/docker 
    [root@johnnyg05 docker]# source /usr/share/bash-completion/bash_completion
    
    • 1
    • 2
    • 3
    [root@johnnyg05 docker]# docker 
    attach     cp         history    load       pause      restart    service    tag        wait
    build      create     image      login      plugin     rm         stack      top        
    builder    diff       images     logout     port       rmi        start      trust      
    commit     events     import     logs       ps         run        stats      unpause    
    config     exec       info       manifest   pull       save       stop       update     
    container  export     inspect    network    push       search     swarm      version    
    context    help       kill       node       rename     secret     system     volume
    //可以看到docker命令&参数可以补全和显示了
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    4 配置docker镜像加速(访问外网下载镜像会更快)

    进入阿里云官网登录之后,在控制台搜索栏输入容器镜像服务并点击重定向到镜像服务界面
    在这里插入图片描述
    进入此界面后,在左侧服务栏中选择镜像工具中的镜像加速器,进入下图界面
    在这里插入图片描述
    如下图所示,将红圈所框内容复制进当前Docker服务主机中,按回车执行命令即可
    请添加图片描述
    如下方所示执行命令即可实现Docker镜像加速

    [root@johnnyg05 ~]# sudo mkdir -p /etc/docker
    [root@johnnyg05 ~]# sudo tee /etc/docker/daemon.json <<-'EOF'
    > {
    >   "registry-mirrors": ["https://j14fvzao.mirror.aliyuncs.com"]
    > }
    > EOF
    {
      "registry-mirrors": ["https://j14fvzao.mirror.aliyuncs.com"]
    }
    [root@johnnyg05 ~]# sudo systemctl daemon-reload
    [root@johnnyg05 ~]# sudo systemctl restart docker
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
  • 相关阅读:
    docker&Kubernetes入门篇(二)
    halcon分割粘连字符
    python 的configparse 读取ini 文件
    docker 安装 redis 6.0.8 cluster 实战 (3主3从) 安装篇
    Java Stream流详解
    [HNCTF 2022 WEEK2]ez_ssrf题目解析
    思腾云计算
    【运维篇】5.4 Redis 并发延迟检测
    使用docker搭建homarr
    事务隔离级别和MVCC
  • 原文地址:https://blog.csdn.net/JohnnyG2000/article/details/126255042