如何將 Docker 升級至最新版本

最近在安裝 Docker 時發現,怎麼使用 yum install -y docker 安裝的 Docker 版本都會是 Docker version 1.13.1 (官網),這版本已經是 2017年2月8日,好舊的版本,目前官方都已經更新到 Docker version 20.10.3 (官網) 2021年2月1日。

整理了一下,如何將舊版本強制更新至最新版流程。
使用的OS為 CentOS7

一、確認目前的版本

可使用以下指令確認,並記錄目前已安裝的套件
docker -v or docker version
rpm -qa | grep docker

[root@web ~]# docker -v
Docker version 1.13.1, build 0be3e21/1.13.1
[root@web ~]# rpm -qa | grep docker
docker-common-1.13.1-203.git0be3e21.el7.centos.x86_64
docker-client-1.13.1-203.git0be3e21.el7.centos.x86_64
docker-1.13.1-203.git0be3e21.el7.centos.x86_64

二、使用 yum remove 移除安裝套件

說明 yum command [packages]
command:
install:安裝
reinstall:重新安裝
update:升級,若要整個系統都升級,後不接套件
remove:移除

執行以下指令進行移除三個套件
yum remove docker-common-1.13.1-203.git0be3e21.el7.centos.x86_64 docker-client-1.13.1-203.git0be3e21.el7.centos.x86_64 docker-1.13.1-203.git0be3e21.el7.centos.x86_64

[root@83-swoft ~]# yum remove docker-common-1.13.1-203.git0be3e21.el7.centos.x86_64 docker-client-1.13.1-203.git0be3e21.el7.centos.x86_64 docker-1.13.1-203.git0be3e21.el7.centos.x86_64
Loaded plugins: fastestmirror, product-id, search-disabled-repos, subscription-
              : manager

This system is not registered with an entitlement server. You can use subscription-manager to register.

Resolving Dependencies
--> Running transaction check
---> Package docker.x86_64 2:1.13.1-203.git0be3e21.el7.centos will be erased
---> Package docker-client.x86_64 2:1.13.1-203.git0be3e21.el7.centos will be erased
---> Package docker-common.x86_64 2:1.13.1-203.git0be3e21.el7.centos will be erased
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package         Arch     Version                               Repository
                                                                           Size
================================================================================
Removing:
 docker          x86_64   2:1.13.1-203.git0be3e21.el7.centos    @extras    65 M
 docker-client   x86_64   2:1.13.1-203.git0be3e21.el7.centos    @extras    13 M
 docker-common   x86_64   2:1.13.1-203.git0be3e21.el7.centos    @extras   4.4 k

Transaction Summary
================================================================================
Remove  3 Packages
//=============(略)===============//
Removed:
  docker.x86_64 2:1.13.1-203.git0be3e21.el7.centos
  docker-client.x86_64 2:1.13.1-203.git0be3e21.el7.centos                       
  docker-common.x86_64 2:1.13.1-203.git0be3e21.el7.centos                       

Complete!

三、執行 Curl 更新至最新版本

執行指令 curl -fsSL https://get.docker.com/ | sh

[root@web ~]# curl -fsSL https://get.docker.com/ | sh
# Executing docker install script, commit: 3d8fe77c2c46c5b7571f94b42793905e5b3e42e4
+ sh -c 'yum install -y -q yum-utils'
+ sh -c 'yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo'
Loaded plugins: fastestmirror, product-id, subscription-manager

This system is not registered with an entitlement server. You can use subscription-manager to register.

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
+ '[' stable '!=' stable ']'
+ sh -c 'yum makecache'
Loaded plugins: fastestmirror, product-id, search-disabled-repos, subscription-
              : manager

This system is not registered with an entitlement server. You can use subscription-manager to register.

Loading mirror speeds from cached hostfile
 * base: free.nchc.org.tw
 * extras: free.nchc.org.tw
 * updates: free.nchc.org.tw
base                                                     | 3.6 kB     00:00     
docker-ce-stable                                         | 3.5 kB     00:00     
extras                                                   | 2.9 kB     00:00     
updates                                                  | 2.9 kB     00:00     
(1/10): docker-ce-stable/7/x86_64/filelists_db             |  24 kB   00:00     
(2/10): docker-ce-stable/7/x86_64/primary_db               |  55 kB   00:00     
(3/10): docker-ce-stable/7/x86_64/updateinfo               |   55 B   00:00     
(4/10): docker-ce-stable/7/x86_64/other_db                 | 117 kB   00:00     
(5/10): extras/7/x86_64/filelists_db                       | 225 kB   00:00     
(6/10): updates/7/x86_64/other_db                          | 454 kB   00:00     
(7/10): extras/7/x86_64/other_db                           | 135 kB   00:00     
(8/10): base/7/x86_64/other_db                             | 2.6 MB   00:00     
(9/10): updates/7/x86_64/filelists_db                      | 3.4 MB   00:00     
(10/10): base/7/x86_64/filelists_db                        | 7.2 MB   00:01     
Metadata Cache Created
+ '[' -n '' ']'
+ sh -c 'yum install -y -q docker-ce'
警告:/var/cache/yum/x86_64/7/docker-ce-stable/packages/containerd.io-1.4.3-3.1.el7.x86_64.rpm: 表頭 V4 RSA/SHA512 Signature, key ID 621e9f35: NOKEY
Public key for containerd.io-1.4.3-3.1.el7.x86_64.rpm is not installed
Importing GPG key 0x621E9F35:
 Userid     : "Docker Release (CE rpm) <docker@docker.com>"
 Fingerprint: 060a 61c5 1b55 8a7f 742b 77aa c52f eb6b 621e 9f35
 From       : https://download.docker.com/linux/centos/gpg
If you would like to use Docker as a non-root user, you should now consider
adding your user to the "docker" group with something like:

  sudo usermod -aG docker your-user

Remember that you will have to log out and back in for this to take effect!

WARNING: Adding a user to the "docker" group will grant the ability to run
         containers which can be used to obtain root privileges on the
         docker host.
         Refer to https://docs.docker.com/engine/security/security/#docker-daemon-attack-surface
         for more information.

四、重新啟動 Docker

執行指領 server docker restart 及查看版本 docker -v

確認已更新最新版本 20.10.3

[root@web ~]# service docker restart
Redirecting to /bin/systemctl restart docker.service
[root@web ~]# docker -v
Docker version 20.10.3, build 48d30b5