Docker分为两个版本:Docker-CE和Docker-EE。
CE 意为 Community Edition(社区版),适用人群是开发者或者刚开始使用Docker的小团队;
EE 意为 Enterprise Edition(企业版),适用人群企业或者负责在生产和规模上构建、发布和运行关键业务应用程序的IT团队。

这里我们安装Docker-CE社区版即可。

参考官方文档:
https://docs.docker.com/install/linux/docker-ce/centos/#install-docker-ce
https://docs.docker.com/install/linux/docker-ce/debian/#install-docker-ce

安装Docker-CE有几种方法,你可以按需选择:

从仓库安装

设置Docker的仓库

CentOS

  1. 安装必要依赖包。yum-utils包中提供了yum-config-manager工具,devicemapper硬盘驱动工具需要device-mapper-persistent-datalvm2

    1
    sudo yum install -y yum-utils device-mapper-persistent-data lvm2
  2. 用以下命令来设置stable(稳定版本)的仓库。stable的仓库会是你常用的仓库。如果要添加edge或者test仓库,在下面的命令中,把edge或者test单词加入到stable单词后。

1
2
3
sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
  1. 可选:开启edgetest仓库。docker.repo文件中包含了这两个仓库,但是默认是关闭的。你可以让他们和stable仓库共存。
1
sudo yum-config-manager --enable docker-ce-edge
1
sudo yum-config-manager --enable docker-ce-test

你可以通过yum-config-manager--disable选项来关闭edge或者test仓库。如果要重新开启,用--enable选项即可。下面命令用来关闭edge仓库。

1
sudo yum-config-manager --disable docker-ce-edge

注意:从Docker 17.06开始,稳定版本也会推送到edgetest仓库。

点击了解stableedge的构建。

Debian

  1. 更新apt包索引

    1
    sudo apt-get update
  2. 安装一个能让apt通过https连接仓库的程序:

Debian8及以上版本:

1
2
3
4
5
6
sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg2 \
software-properties-common

Debian7及以下版本:

1
2
3
4
5
sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
python-software-properties

  1. 添加Docker官方的GPG key:

    1
    curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add -
  2. 用以下命令来设置stable(稳定版本)的仓库。stable的仓库会是你常用的仓库。如果要添加edge或者test仓库,在下面的命令中,把edge或者test单词加入到stable单词后。

Note:lsb_release -cs子命令会返回你当前系统的版本代号,比如jessie

x86_64 / amd64架构:

1
2
3
4
add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/debian \
$(lsb_release -cs) \
stable"

armhf架构:

1
2
3
echo "deb [arch=armhf] https://download.docker.com/linux/debian \
$(lsb_release -cs) stable" | \
sudo tee /etc/apt/sources.list.d/docker.list

安装Docker-CE

CentOS

  1. 安装最新版的Docker-CE,或者看下一步安装指定版本:
    1
    sudo yum install docker-ce

如果提示需要接受GPG密钥,验证指纹是否匹配060A 61C5 1B55 8A7F 742B 77AA C52F EB6B 621E 9F35,如果是,就接受。

如果你开启了多个Docker的仓库,没有指定版本号的yum install安装或者yum update更新,默认安装最新版,可能不与你的稳定性兼容。

此时,Docker已经安装好了,但是没有启动。Docker用户组已经创建,但是没有添加用户到组里。

  1. 要安装指定版本的Docker-CE,请先查看仓库中可用的版本,再选择安装哪一个版本。

a. 列出可用版本。示例是以搜索结果的版本号,从高到低排序,并且显示适合你的系统的软件包:

1
2
3
yum list docker-ce --showduplicates | sort -r

docker-ce.x86_64 18.03.0.ce-1.el7.centos docker-ce-stable

列表返回的内容取决于你开启的仓库,并且从你的CentOS版本号而来(从例子中的.el7后缀可以看出)。

b. 安装指定版本的docker-ce,在后面加上-,再加上全部软件包名。比如docker-ce-18.03.0.ce

1
sudo yum install docker-ce-<VERSION STRING>

此时,Docker已经安装好了,但是没有启动。Docker用户组已经创建,但是没有添加用户到组里。

  1. 启动docker。

    1
    sudo systemctl start docker
  2. hello-world镜像来验证是否正确安装。

    1
    sudo docker run hello-world

这串命令下载了一个测试镜像,并且在一个容器内运行这个镜像。当容器运行时,他会输出一些信息,然后退出。

此时,Docker CE已经安装好,并且运行。你需要用sudo来运行docker的命令。查看Linux postinstall页面,来允许非特权用户运行docker命令,或者其他可选的配置步骤。

Debian

  1. 更新apt包索引:

    1
    apt-get update
  2. 安装最新版的Docker-CE,或者看第三步安装指定版本

    1
    apt-get install docker-ce
  3. 要安装指定版本的Docker-CE,请先查看仓库中可用的版本,再选择安装哪一个版本。

a. 列出可用版本

1
2
3
apt-cache madison docker-ce

docker-ce | 18.03.0~ce-0~debian | https://download.docker.com/linux/debian jessie/stable amd64 Packages

b. 安装指定版本的docker-ce,在后面加上=,再加上全部版本号。比如docker-ce=18.03.0.ce

1
sudo apt-get install docker-ce=<VERSION_STRING>

  1. hello-world镜像来验证是否正确安装
    x86_64架构:
    1
    $ sudo docker run hello-world

armhf架构:

1
$ sudo docker run armhf/hello-world

比如我的显示如下

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
root@debian:~# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
9bb5a5d4561a: Pull complete
Digest: sha256:f5233545e43561214ca4891fd1157e1c3c563316ed8e237750d59bde73361e77
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/engine/userguide/

更新Docker CE

查看安装步骤,选择你想要安装的新版本。

使用安装包安装

CentOS

如果你不能从仓库安装,你可以下载.rpm安装包来手动安装。每次你想要更新,你都要手动下载新版本的安装包。

  1. 进入https://download.docker.com/linux/centos/7/x86_64/stable/Packages/,然后下载你要安装的版本的.rpm文件。

注意:要安装edge安装包,修改上述URL中的stableedge

  1. 安装Docker CE,修改下面命令中的路径为你下载安装包的路径。
    1
    sudo yum install /path/to/package.rpm

此时,Docker已经安装好了,但是没有启动。Docker用户组已经创建,但是没有添加用户到组里。

  1. 启动docker

    1
    sudo systemctl start docker
  2. hello-world镜像来验证是否正确安装。

    1
    sudo docker run hello-world

这串命令下载了一个测试镜像,并且在一个容器内运行这个 镜像。当容器运行时,他会输出一些信息,然后退出。

此时,Docker CE已经安装好,并且运行。你需要用sudo来运行docker的命令。查看Post-installation steps for Linux页面,来允许非特权用户运行docker命令,或者其他可选的配置步骤。

更新Docker CE

下载新版本的安装包文件,然后重复安装步骤,使用yum -y upgrade代替yum -y install,并指向新的文件。

Debian

如果你不能从仓库安装,你可以下载.deb安装包来手动安装。

  1. 进入https://download.docker.com/linux/debian/dists/,选择你的发行版本,继续查看pool/stable/,选择架构,amd64还是armhf,然后下载你要安装的版本.deb文件。

  2. 安装

    1
    $ sudo dpkg -i /path/to/package.deb
  3. 验证是否正确安装

    1
    $ sudo docker run hello-world

从自动化脚本安装

1
$ curl -sSL https://get.docker.com/ | sh

<省略输出>

如果你想要在非root账号下运行,你需要将你的账号加入到docker用户组:

1
sudo usermod -aG docker your-user

然后,请注销账号,并重新登录才能生效!

警告:把用户添加到”docker”用户组来得到运行容器的权限,同时也会获得在docker宿主机的root权限。

查看链接获得更多的信息https://docs.docker.com/engine/security/security/#docker-daemon-attack-surface