离线部署Docker

生产环境中一般都是没有公网资源的,本文介绍如何在生产服务器上离线部署Docker

括号内的字母表示该操作需要在哪些服务器上执行

Docker-offile-install-top

Centos7 离线安装Docker

YUM本地文件安装(推荐)

推荐这种方式,是因为在生产环境种一般会选定某个指定的文档软件版本使用。

查询可用的软件版本(A)

  1. #下载清华的镜像源文件
  2. wget -O /etc/yum.repos.d/docker-ce.repo https://download.docker.com/linux/centos/docker-ce.repo
  3. sudo sed -i 's+download.docker.com+mirrors.tuna.tsinghua.edu.cn/docker-ce+' /etc/yum.repos.d/docker-ce.repo
  4. yum update
  1. sudo yum list docker-ce --showduplicates|sort -r
  2. Loading mirror speeds from cached hostfile
  3. Loaded plugins: fastestmirror
  4. docker-ce.x86_64 3:19.03.8-3.el7 docker-ce-stable
  5. docker-ce.x86_64 3:19.03.7-3.el7 docker-ce-stable
  6. docker-ce.x86_64 3:19.03.6-3.el7 docker-ce-stable
  7. docker-ce.x86_64 3:19.03.5-3.el7 docker-ce-stable
  8. docker-ce.x86_64 3:19.03.4-3.el7 docker-ce-stable
  9. docker-ce.x86_64 3:19.03.3-3.el7 docker-ce-stable
  10. docker-ce.x86_64 3:19.03.2-3.el7 docker-ce-stable
  11. docker-ce.x86_64 3:19.03.1-3.el7 docker-ce-stable
  12. ....

下载到指定文件夹(A)

  1. sudo yum install --downloadonly --downloaddir=/tmp/docker-19.03 docker-ce-19.03.8-3.el7 docker-ce-cli-19.03.8-3.el7
  1. Dependencies Resolved
  2. ====================================================================================================================================================================================
  3. Package Arch Version Repository Size
  4. ====================================================================================================================================================================================
  5. Installing:
  6. docker-ce x86_64 3:19.03.8-3.el7 docker 25 M
  7. Installing for dependencies:
  8. container-selinux noarch 2:2.107-3.el7 extras 39 k
  9. containerd.io x86_64 1.2.13-3.1.el7 docker 23 M
  10. docker-ce-cli x86_64 1:19.03.8-3.el7 docker 40 M
  11. Transaction Summary
  12. ====================================================================================================================================================================================
  13. Install 1 Package (+3 Dependent packages)
  14. Total download size: 87 M
  15. Installed size: 363 M
  16. Background downloading packages, then exiting:
  17. (1/4): container-selinux-2.107-3.el7.noarch.rpm | 39 kB 00:00:00
  18. (2/4): containerd.io-1.2.13-3.1.el7.x86_64.rpm | 23 MB 00:00:00
  19. (3/4): docker-ce-19.03.8-3.el7.x86_64.rpm | 25 MB 00:00:00
  20. (4/4): docker-ce-cli-19.03.8-3.el7.x86_64.rpm | 40 MB 00:00:00
  21. ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  22. Total 118 MB/s | 87 MB 00:00:00
  23. exiting because "Download Only" specified

复制到目标服务器之后进入文件夹安装(C-N)

  1. yum install *.rpm

锁定软件版本(C-N)

下载锁定版本软件

可参考下文的网络源搭建

  1. sudo yum install yum-plugin-versionlock
锁定软件版本
  1. sudo yum versionlock add docker
查看锁定列表
  1. sudo yum versionlock list
  1. Loaded plugins: fastestmirror, versionlock
  2. 3:docker-ce-18.09.9-3.el7.*
  3. versionlock list done
锁定后无法再更新
  1. sudo yum install docker-ce
  2. Loaded plugins: fastestmirror, versionlock
  3. Loading mirror speeds from cached hostfile
  4. Excluding 1 update due to versionlock (use "yum versionlock status" to show it)
  5. Package 3:docker-ce-18.09.9-3.el7.x86_64 already installed and latest version
  6. Nothing to do
解锁指定软件
  1. sudo yum versionlock delete docker-ce
  1. Loaded plugins: fastestmirror, versionlock
  2. Deleting versionlock for: 3:docker-ce-18.09.9-3.el7.*
  3. versionlock deleted: 1
解锁所有软件
  1. sudo yum versionlock delete all

YUM 本地源服务器搭建安装Docker

挂载 ISO 镜像搭建本地 File 源(AB)

  1. # 删除其他网络源
  2. rm -f /etc/yum.repo.d/*
  3. # 挂载光盘或者iso镜像
  4. mount /dev/cdrom /mnt
  1. # 添加本地源
  2. cat >/etc/yum.repos.d/local_files.repo<< EOF
  3. [Local_Files]
  4. name=Local_Files
  5. baseurl=file:///mnt
  6. enable=1
  7. gpgcheck=0
  8. gpgkey=file:///mnt/RPM-GPG-KEY-CentOS-7
  9. EOF
  1. # 测试刚才的本地源,安装createrepo软件
  2. yum clean all
  3. yum install createrepo -y

根据本地文件搭建BASE网络源(B)

  1. # 安装apache 服务器
  2. yum install httpd -y
  3. # 挂载光盘
  4. mount /dev/cdrom /mnt
  5. # 新建centos目录
  6. mkdir /var/www/html/base
  7. # 复制光盘内的文件到刚才新建的目录
  8. cp -R /mnt/Packages/* /var/www/html/base/
  9. createrepo /var/www/html/centos/
  10. systemctl enable httpd
  11. systemctl start httpd

下载Docker-CE 镜像仓库(A)

在有网络的服务器上下载Docker-ce镜像

  1. # 下载清华的镜像源文件
  2. wget -O /etc/yum.repos.d/docker-ce.repo https://download.docker.com/linux/centos/docker-ce.repo
  3. sudo sed -i 's+download.docker.com+mirrors.tuna.tsinghua.edu.cn/docker-ce+' /etc/yum.repos.d/docker-ce.repo
  1. # 新建 docker-ce目录
  2. mkdir /tmp/docker-ce/
  3. # 把镜像源同步到镜像文件中
  4. reposync -r docker-ce-stable -p /tmp/docker-ce/

创建仓库索引(B)

把下载的 docker-ce 文件夹复制到离线的服务器

  1. # 把docker-ce 文件夹复制到/var/www/html/docker-ce
  2. # 重建索引
  3. createrepo /var/www/html/docker-ce/

YUM 客户端设置(C…N)

  1. rm -f /etc/yum.repo.d/*
  2. cat >/etc/yum.repos.d/local_files.repo<< EOF
  3. [local_base]
  4. name=local_base
  5. # 改成B服务器地址
  6. baseurl=http://x.x.x.x/base
  7. enable=1
  8. gpgcheck=0
  9. proxy=_none_
  10. [docker_ce]
  11. name=docker_ce
  12. # 改成B服务器地址
  13. baseurl=http://x.x.x.x/base
  14. enable=1
  15. gpgcheck=0
  16. proxy=_none_
  17. EOF

Docker 安装(C…N)

  1. sudo yum makecache fast
  2. sudo yum install docker-ce docker-ce-cli containerd.io
  3. sudo systemctl enable docker