本文将介绍在Ubuntu 16.04中安装Docker。

注意:Ubuntu14和16底层架构发生了变化,本文仅针对16版本。

系统要求

Docker Engine - Community 支持以下版本的Ubuntu的64位机器:

1. Disco 19.04

2. Cosmic 18.10

3. Bionic 18.04 (LTS)

4. Xenial 16.04 (LTS)

卸载旧版本

旧版本的Docker称为docker、docker.io 或者docker engine,如果机器之前安装过这些,需要完全卸载:

sudo apt-get remove docker docker-engine docker.io contained runc 

如果提示没有安装这些软件,就可以了。

建议最好执行下上述命令,我在执行这条命令后返回结果如下:


root@iZwz99z2ljzabken0hvkjrZ:/home/dimple/project# sudo apt-get remove docker docker-engine docker.io containerd runc
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Package 'docker-engine' is not installed, so not removed
Package 'docker' is not installed, so not removed
Package 'containerd' is not installed, so not removed
Package 'docker.io' is not installed, so not removed
Package 'runc' is not installed, so not removed
The following packages were automatically installed and are no longer required:
  aufs-tools cgroupfs-mount containerd.io git git-man liberror-perl libltdl7 pigz
Use 'sudo apt autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 261 not upgraded.


可以选择保留在/etc/lib/docker下的内容,这是Docker运行的一些镜像、容器、数据卷和网络设置之类的。

安装Docker Engine -Community社区版

当然Docker的安装方式不止我接下来说的这一种,我选择官方推荐的方式(仓库)进行安装。

在新主机上首次安装Docker Engine - Community之前,需要设置Docker仓库。之后,就可以从存储库安装和更新Docker。

设置Docker仓库

1. 更新apt包索引:


sudo apt-get update


2. 设置使用HTTP的方式使用仓库


sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common


ps:这里的\表示换行

结果如下:


root@iZwz99z2ljzabken0hvkjrZ:/etc/docker# sudo add-apt-repository \
>    "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
>    $(lsb_release -cs) \
>    stable"
root@iZwz99z2ljzabken0hvkjrZ:/etc/docker# sudo apt-get update
Hit:1 http://mirrors.cloud.aliyuncs.com/ubuntu xenial InRelease
Hit:2 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-updates InRelease                                          
Hit:3 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-security InRelease                                                                   
Hit:4 http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial InRelease                                                                    
Get:5 https://download.docker.com/linux/ubuntu xenial InRelease [66.2 kB]       
Get:6 https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages [10.0 kB]
Fetched 76.3 kB in 1s (40.2 kB/s)
Reading package lists... Done
root@iZwz99z2ljzabken0hvkjrZ:/etc/docker# sudo apt-get install docker-ce docker-ce-cli containerd.io
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed:
  docker-ce docker-ce-cli
The following packages will be upgraded:
  containerd.io
1 upgraded, 2 newly installed, 0 to remove and 257 not upgraded.
Need to get 87.5 MB of archives.
After this operation, 301 MB of additional disk space will be used.
Get:1 http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 containerd.io amd64 1.2.6-3 [22.6 MB]
Get:2 http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 docker-ce-cli amd64 5:19.03.1~3-0~ubuntu-xenial [42.2 MB]
Get:3 http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 docker-ce amd64 5:19.03.1~3-0~ubuntu-xenial [22.6 MB]
Fetched 87.5 MB in 5s (14.7 MB/s)     
(Reading database ... 103560 files and directories currently installed.)
Preparing to unpack .../containerd.io_1.2.6-3_amd64.deb ...
Unpacking containerd.io (1.2.6-3) over (1.2.5-1) ...
Selecting previously unselected package docker-ce-cli.
Preparing to unpack .../docker-ce-cli_5%3a19.03.1~3-0~ubuntu-xenial_amd64.deb ...
Unpacking docker-ce-cli (5:19.03.1~3-0~ubuntu-xenial) ...
Selecting previously unselected package docker-ce.
Preparing to unpack .../docker-ce_5%3a19.03.1~3-0~ubuntu-xenial_amd64.deb ...
Unpacking docker-ce (5:19.03.1~3-0~ubuntu-xenial) ...
Processing triggers for man-db (2.7.5-1) ...
Processing triggers for systemd (229-4ubuntu19) ...
Processing triggers for ureadahead (0.100.0-19) ...
Setting up containerd.io (1.2.6-3) ...
Setting up docker-ce-cli (5:19.03.1~3-0~ubuntu-xenial) ...
Setting up docker-ce (5:19.03.1~3-0~ubuntu-xenial) ...


3. 添加Docker官方秘钥


curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -


结果如下:


root@iZwz99z2ljzabken0hvkjrZ:/etc/docker# curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
OK


通过搜索秘钥的最后8个字符来确定使用了“9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88”这个秘钥


 sudo apt-key fingerprint 0EBFCD88


执行结果如下:


root@iZwz99z2ljzabken0hvkjrZ:/etc/docker#  sudo apt-key fingerprint 0EBFCD88
pub   4096R/0EBFCD88 2017-02-22
      Key fingerprint = 9DC8 5822 9FC7 DD38 854A  E2D8 8D81 803C 0EBF CD88
uid                  Docker Release (CE deb) 
sub   4096R/F273FCD8 2017-02-22


4. 使用以下命令设置仓库


sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"


安装Docker Engine-Community社区版

1. 更新包索引


sudo apt-get update


2. 安装最新版本的Docker Engine和Containerd


sudo apt-get install docker-ce docker-ce-cli containerd.io


结果如下:


root@iZwz99z2ljzabken0hvkjrZ:/etc/docker# sudo add-apt-repository \
>    "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
>    $(lsb_release -cs) \
>    stable"
root@iZwz99z2ljzabken0hvkjrZ:/etc/docker# sudo apt-get update
Hit:1 http://mirrors.cloud.aliyuncs.com/ubuntu xenial InRelease
Hit:2 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-updates InRelease                                          
Hit:3 http://mirrors.cloud.aliyuncs.com/ubuntu xenial-security InRelease                                                                   
Hit:4 http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial InRelease                                                                    
Get:5 https://download.docker.com/linux/ubuntu xenial InRelease [66.2 kB]       
Get:6 https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages [10.0 kB]
Fetched 76.3 kB in 1s (40.2 kB/s)
Reading package lists... Done
root@iZwz99z2ljzabken0hvkjrZ:/etc/docker# sudo apt-get install docker-ce docker-ce-cli containerd.io
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed:
  docker-ce docker-ce-cli
The following packages will be upgraded:
  containerd.io
1 upgraded, 2 newly installed, 0 to remove and 257 not upgraded.
Need to get 87.5 MB of archives.
After this operation, 301 MB of additional disk space will be used.
Get:1 http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 containerd.io amd64 1.2.6-3 [22.6 MB]
Get:2 http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 docker-ce-cli amd64 5:19.03.1~3-0~ubuntu-xenial [42.2 MB]
Get:3 http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 docker-ce amd64 5:19.03.1~3-0~ubuntu-xenial [22.6 MB]
Fetched 87.5 MB in 5s (14.7 MB/s)     
(Reading database ... 103560 files and directories currently installed.)
Preparing to unpack .../containerd.io_1.2.6-3_amd64.deb ...
Unpacking containerd.io (1.2.6-3) over (1.2.5-1) ...
Selecting previously unselected package docker-ce-cli.
Preparing to unpack .../docker-ce-cli_5%3a19.03.1~3-0~ubuntu-xenial_amd64.deb ...
Unpacking docker-ce-cli (5:19.03.1~3-0~ubuntu-xenial) ...
Selecting previously unselected package docker-ce.
Preparing to unpack .../docker-ce_5%3a19.03.1~3-0~ubuntu-xenial_amd64.deb ...
Unpacking docker-ce (5:19.03.1~3-0~ubuntu-xenial) ...
Processing triggers for man-db (2.7.5-1) ...
Processing triggers for systemd (229-4ubuntu19) ...
Processing triggers for ureadahead (0.100.0-19) ...
Setting up containerd.io (1.2.6-3) ...
Setting up docker-ce-cli (5:19.03.1~3-0~ubuntu-xenial) ...
Setting up docker-ce (5:19.03.1~3-0~ubuntu-xenial) ...


3. 选择一个可用版本的Docker engine,然后安装

a. 列出当前可用的仓库


apt-cache madison docker-ce


我这里列出的结果为:


root@iZwz99z2ljzabken0hvkjrZ:/etc/docker# apt-cache madison docker-ce
 docker-ce | 5:19.03.1~3-0~ubuntu-xenial | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 5:19.03.1~3-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 5:19.03.0~3-0~ubuntu-xenial | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 5:19.03.0~3-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 5:18.09.8~3-0~ubuntu-xenial | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 5:18.09.8~3-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 5:18.09.7~3-0~ubuntu-xenial | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 5:18.09.7~3-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 5:18.09.6~3-0~ubuntu-xenial | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 5:18.09.6~3-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 5:18.09.5~3-0~ubuntu-xenial | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 5:18.09.5~3-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 5:18.09.4~3-0~ubuntu-xenial | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 5:18.09.4~3-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 5:18.09.3~3-0~ubuntu-xenial | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 5:18.09.3~3-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 5:18.09.2~3-0~ubuntu-xenial | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 5:18.09.2~3-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 5:18.09.1~3-0~ubuntu-xenial | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 5:18.09.1~3-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 5:18.09.0~3-0~ubuntu-xenial | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 5:18.09.0~3-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 18.06.3~ce~3-0~ubuntu | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 18.06.3~ce~3-0~ubuntu | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 18.06.2~ce~3-0~ubuntu | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 18.06.2~ce~3-0~ubuntu | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 18.06.1~ce~3-0~ubuntu | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 18.06.1~ce~3-0~ubuntu | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 18.06.0~ce~3-0~ubuntu | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 18.06.0~ce~3-0~ubuntu | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 18.03.1~ce-0~ubuntu | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 18.03.1~ce-0~ubuntu | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 18.03.0~ce-0~ubuntu | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 18.03.0~ce-0~ubuntu | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 17.12.1~ce-0~ubuntu | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 17.12.1~ce-0~ubuntu | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 17.12.0~ce-0~ubuntu | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 17.12.0~ce-0~ubuntu | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 17.09.1~ce-0~ubuntu | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 17.09.1~ce-0~ubuntu | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 17.09.0~ce-0~ubuntu | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 17.09.0~ce-0~ubuntu | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 17.06.2~ce-0~ubuntu | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 17.06.2~ce-0~ubuntu | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 17.06.1~ce-0~ubuntu | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 17.06.1~ce-0~ubuntu | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 17.06.0~ce-0~ubuntu | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 17.06.0~ce-0~ubuntu | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 17.03.3~ce-0~ubuntu-xenial | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 17.03.3~ce-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 17.03.2~ce-0~ubuntu-xenial | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 17.03.2~ce-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 17.03.1~ce-0~ubuntu-xenial | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 17.03.1~ce-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 17.03.0~ce-0~ubuntu-xenial | http://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages
 docker-ce | 17.03.0~ce-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages


b. 使用第二列中的版本进行安装

ps:这里我使用5:19.03.1~3-0~ubuntu-xenial这个版本,注意替换以下命令中的版本号


sudo apt-get install docker-ce= docker-ce-cli= containerd.io


结果如下:


 sudo apt-get install docker-ce=5:19.03.1~3-0~ubuntu-xenial docker-ce-cli=5:19.03.1~3-0~ubuntu-xenial containerd.io
Reading package lists... Done
Building dependency tree       
Reading state information... Done
containerd.io is already the newest version (1.2.6-3).
docker-ce-cli is already the newest version (5:19.03.1~3-0~ubuntu-xenial).
docker-ce is already the newest version (5:19.03.1~3-0~ubuntu-xenial).
0 upgraded, 0 newly installed, 0 to remove and 257 not upgraded.


4. 检测是否安装成功


sudo docker run hello-world


出现以下界面表示安装成功


root@iZwz99z2ljzabken0hvkjrZ:/# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete 
Digest: sha256:6540fc08ee6e6b7b63468dc3317e3303aae178cb8a45ed3123180328bcc1d20f
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/


至此,Docker的安装已经成功