2021-02-26

Docker 安装、更新、卸载

gmg [后端工程师进阶](javascript:void(0);) 2018-11-04

安装、更新、卸载

Docker 分为 CE 和 EE 两大版本。CE 即社区版(免费),EE 即企业版,强调安全,付费使用。Docker支持在主流的操作系统平台上使用,包括Ubuntu、Centos、Windows、MacOS系统等。
Docker CE 分为 stable, test, 和 nightly 三个更新频道。每六个月发布一个 stable 版本 (18.09, 19.03, 19.09...)。

1、 Ubuntu

1.1 安装、更新

警告:切勿在没有配置 Docker APT 源的情况下直接使用 apt 命令安装 Docker.

准备工作

系统要求

Docker CE 支持以下版本的 Ubuntu 操作系统:

  • Bionic 18.04 (LTS)

  • Xenial 16.04 (LTS)

  • Trusty 14.04 (LTS)

Docker CE 可以安装在 64 位的 x86 平台或 ARM 平台上。Ubuntu 发行版中,LTS(Long-Term-Support)长期支持版本,会获得 5 年的升级维护支持,这样的版本会更稳定,因此在生产环境中推荐使用 LTS 版本,当前最新的 LTS 版本为 Ubuntu 18.04。

卸载旧版本

旧版本的 Docker 称为 docker 或者 docker-engine,使用以下命令卸载旧版本:

<pre class="" style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. $ sudo apt-get remove docker \

  2. docker-engine \

  3. docker.io

</pre>

Ubuntu 14.04 可选内核模块

从 Ubuntu 14.04 开始,一部分内核模块移到了可选内核模块包 ( linux-image-extra-*) ,以减少内核软件包的体积。正常安装的系统应该会包含可选内核模块包,而一些裁剪后的系统可能会将其精简掉。 AUFS 内核驱动属于可选内核模块的一部分,作为推荐的 Docker 存储层驱动,一般建议安装可选内核模块包以使用 AUFS

如果系统没有安装可选内核模块的话,可以执行下面的命令来安装可选内核模块包:

<pre class="" style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. $ sudo apt-get update

  2. $ sudo apt-get install \

  3. linux-image-extra-$(uname -r) \

  4. linux-image-extra-virtual

</pre>

Ubuntu 16.04 +

Ubuntu 16.04 + 上的 Docker CE 默认使用 overlay2 存储层驱动,无需手动配置。

1.1.1 使用 APT 安装、升级

由于 apt 源使用 HTTPS 以确保软件下载过程中不被篡改。因此,我们首先需要添加使用 HTTPS 传输的软件包以及 CA 证书。

<pre class="" style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. $ sudo apt-get update

  2. $ sudo apt-get install \

  3. apt-transport-https \

  4. ca-certificates \

  5. curl \

  6. software-properties-common

</pre>

鉴于国内网络问题,强烈建议使用国内源,官方源请在注释中查看。

为了确认所下载软件包的合法性,需要添加软件源的 GPG 密钥。

<pre class="" style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. $ curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

  2. # 官方源

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

</pre>

然后,我们需要向 source.list 中添加 Docker 软件源

<pre class="" style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. $ sudo add-apt-repository \

  2. "deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu \

  3. $(lsb_release -cs) \

  4. stable"

  5. # 官方源

  6. # $ sudo add-apt-repository \

  7. # "deb [arch=amd64] https://download.docker.com/linux/ubuntu \

  8. # $(lsb_release -cs) \

  9. # stable"

</pre>

以上命令会添加稳定版本的 Docker CE APT 镜像源,如果需要测试或每日构建版本的 Docker CE 请将 stable 改为 test 或者 nightly。

安装

更新 apt 软件包缓存,并安装 docker-ce

<pre class="" style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. $ sudo apt-get update

  2. $ sudo apt-get install docker-ce

</pre>

指定版本安装

<pre class="" style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. 版本列表

  2. apt-cache madison docker-ce

  3. docker-ce | 18.03.0~ce-0~ubuntu | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages

</pre>

<pre class="" style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. 执行安装命令

  2. sudo apt-get install docker-ce=<VERSION>

</pre>

升级

<pre class="" style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. sudo apt-get update

</pre>

1.1.2 使用脚本自动安装

在测试或开发环境中 Docker 官方为了简化安装流程,提供了一套便捷的安装脚本,Ubuntu 系统上可以使用这套脚本安装:

<pre class="" style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. $ curl -fsSL get.docker.com -o get-docker.sh

  2. $ sudo sh get-docker.sh --mirror Aliyun

</pre>

执行这个命令后,脚本就会自动的将一切准备工作做好,并且把 Docker CE 的 Edge 版本安装在系统中。

1.1.3 使用deb包安装

  • 下载.deb包,下载地址。

  • 执行命令

<pre class="" style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. sudo dpkg -i /path/to/package.deb

</pre>

启动 Docker CE

<pre class="" style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. $ sudo systemctl enable docker

  2. $ sudo systemctl start docker

</pre>

Ubuntu 14.04 请使用以下命令启动:

<pre class="" style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. $ sudo service docker start

</pre>

建立 docker 用户组

默认情况下, docker 命令会使用 Unix socket 与 Docker 引擎通讯。而只有 root 用户和 docker 组的用户才可以访问 Docker 引擎的 Unix socket。出于安全考虑,一般 Linux 系统上不会直接使用 root 用户。因此,更好地做法是将需要使用 docker 的用户加入 docker 用户组。

建立 docker 组:

<pre class="" style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. $ sudo groupadd docker

</pre>

将当前用户加入 docker 组:

<pre class="" style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. $ sudo usermod -aG docker $USER

</pre>

退出当前终端并重新登录,进行如下测试。

测试 Docker 是否安装正确

<pre class="" style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. $ docker run hello-world

  2. Unable to find image 'hello-world:latest' locally

  3. latest: Pulling from library/hello-world

  4. d1725b59e92d: Pull complete

  5. Digest: sha256:0add3ace90ecb4adbf7777e9aacf18357296e799f81cabc9fde470971e499788

  6. Status: Downloaded newer image for hello-world:latest

  7. Hello from Docker!

  8. This message shows that your installation appears to be working correctly.

  9. To generate this message, Docker took the following steps:

  10. 1. The Docker client contacted the Docker daemon.

  11. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.

  12. (amd64)

  13. 3. The Docker daemon created a new container from that image which runs the

  14. executable that produces the output you are currently reading.

  15. 4. The Docker daemon streamed that output to the Docker client, which sent it

  16. to your terminal.

  17. To try something more ambitious, you can run an Ubuntu container with:

  18. $ docker run -it ubuntu bash

  19. Share images, automate workflows, and more with a free Docker ID:

  20. https://hub.docker.com/

  21. For more examples and ideas, visit:

  22. https://docs.docker.com/get-started/

</pre>

若能正常输出以上信息,则说明安装成功。

镜像加速

鉴于国内网络问题,后续拉取 Docker 镜像十分缓慢,强烈建议安装 Docker 之后配置国内镜像加速(参考配置镜像加速器)。

1.2 卸载

<pre class="" style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. sudo apt-get purge docker-ce

  2. sudo rm -rf /var/lib/docker

</pre>

参考文档

  • Docker 官方 Ubuntu 安装文档

2、Centos

2.1、CentOS 安装 Docker CE

警告:切勿在没有配置 Docker YUM 源的情况下直接使用 yum 命令安装 Docker.

准备工作

系统要求

Docker CE 支持 64 位版本 CentOS 7,并且要求内核版本不低于 3.10。 CentOS 7 满足最低内核的要求,但由于内核版本比较低,部分功能(如 overlay2 存储层驱动)无法使用,并且部分功能可能不太稳定。

卸载旧版本

旧版本的 Docker 称为 docker 或者 docker-engine,使用以下命令卸载旧版本:

<pre class="" style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. $ sudo yum remove docker \

  2. docker-client \

  3. docker-client-latest \

  4. docker-common \

  5. docker-latest \

  6. docker-latest-logrotate \

  7. docker-logrotate \

  8. docker-selinux \

  9. docker-engine-selinux \

  10. docker-engine

</pre>

2.1.1 使用 yum 安装、升级

执行以下命令安装依赖包:

<pre class="" style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. $ sudo yum install -y yum-utils \

  2. device-mapper-persistent-data \

  3. lvm2

</pre>

鉴于国内网络问题,强烈建议使用国内源,官方源请在注释中查看。

执行下面的命令添加 yum 软件源:

<pre class="" style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. $ sudo yum-config-manager \

  2. --add-repo \

  3. https://mirrors.ustc.edu.cn/docker-ce/linux/centos/docker-ce.repo

  4. # 官方源

  5. # $ sudo yum-config-manager \

  6. # --add-repo \

  7. # https://download.docker.com/linux/centos/docker-ce.repo

</pre>

如果需要测试版本的 Docker CE 请使用以下命令:

<pre class="" style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. $ sudo yum-config-manager --enable docker-ce-test

</pre>

如果需要每日构建版本的 Docker CE 请使用以下命令:

<pre class="" style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. $ sudo yum-config-manager --enable docker-ce-nightly

</pre>

安装 Docker CE

更新 yum 软件源缓存,并安装 docker-ce

<pre class="" style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. $ sudo yum makecache fast

  2. $ sudo yum install docker-ce

</pre>

指定版本安装,通过以下命令查看可用的安装版本:

<pre class="" style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. yum list docker-ce.x86_64 --showduplicates |sort -r

</pre>

在安装时可以指定版本号来安装指定版本的Docker:

<pre class="" style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. sudo yum -y install docker-ce-<VERSION_STRING>

</pre>

升级

<pre class="" style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. sudo yum makecache fast

</pre>

2.1.2 使用脚本自动安装

在测试或开发环境中 Docker 官方为了简化安装流程,提供了一套便捷的安装脚本,CentOS 系统上可以使用这套脚本安装:

<pre class="" style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. $ curl -fsSL get.docker.com -o get-docker.sh

  2. $ sudo sh get-docker.sh --mirror Aliyun

</pre>

执行这个命令后,脚本就会自动的将一切准备工作做好,并且把 Docker CE 的 Edge 版本安装在系统中。

2.1.3 使用rpm安装、升级

安装

去rpm地址下载.rpm文件。 执行如下命令:

<pre class="" style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. sudo yum -y install xxx.rpm

</pre>

升级

使用.rpm包安装的Docker不支持在线升级,升级时同样需要下载新的安装文件并使用yum -y upgrade命令进行升级安装:sudo yum-y upgrade xxx.rpm

启动 Docker CE

<pre class="" style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. $ sudo systemctl enable docker

  2. $ sudo systemctl start docker

</pre>

建立 docker 用户组

默认情况下, docker 命令会使用 Unix socket 与 Docker 引擎通讯。而只有 root 用户和 docker 组的用户才可以访问 Docker 引擎的 Unix socket。出于安全考虑,一般 Linux 系统上不会直接使用 root 用户。因此,更好地做法是将需要使用 docker 的用户加入 docker 用户组。

建立 docker 组:

<pre class="" style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. $ sudo groupadd docker

</pre>

将当前用户加入 docker 组:

<pre class="" style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. $ sudo usermod -aG docker $USER

</pre>

退出当前终端并重新登录,进行如下测试。

测试 Docker 是否安装正确

<pre class="" style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. $ docker run hello-world

  2. Unable to find image 'hello-world:latest' locally

  3. latest: Pulling from library/hello-world

  4. d1725b59e92d: Pull complete

  5. Digest: sha256:0add3ace90ecb4adbf7777e9aacf18357296e799f81cabc9fde470971e499788

  6. Status: Downloaded newer image for hello-world:latest

  7. Hello from Docker!

  8. This message shows that your installation appears to be working correctly.

  9. To generate this message, Docker took the following steps:

  10. 1. The Docker client contacted the Docker daemon.

  11. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.

  12. (amd64)

  13. 3. The Docker daemon created a new container from that image which runs the

  14. executable that produces the output you are currently reading.

  15. 4. The Docker daemon streamed that output to the Docker client, which sent it

  16. to your terminal.

  17. To try something more ambitious, you can run an Ubuntu container with:

  18. $ docker run -it ubuntu bash

  19. Share images, automate workflows, and more with a free Docker ID:

  20. https://hub.docker.com/

  21. For more examples and ideas, visit:

  22. https://docs.docker.com/get-started/

</pre>

若能正常输出以上信息,则说明安装成功。

镜像加速

鉴于国内网络问题,后续拉取 Docker 镜像十分缓慢,强烈建议安装 Docker 之后配置 国内镜像加速(参考配置镜像加速器)。

添加内核参数

默认配置下,如果在 CentOS 使用 Docker CE 看到下面的这些警告信息:

<pre class="" style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. WARNING: bridge-nf-call-iptables is disabled

  2. WARNING: bridge-nf-call-ip6tables is disabled

</pre>

请添加内核配置参数以启用这些功能。

<pre class="" style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. $ sudo tee -a /etc/sysctl.conf <<-EOF

  2. net.bridge.bridge-nf-call-ip6tables = 1

  3. net.bridge.bridge-nf-call-iptables = 1

  4. EOF

</pre>

然后重新加载 sysctl.conf 即可

<pre class="" style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. $ sudo sysctl -p

</pre>

2.2 卸载 Docker CE

<pre class="" style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. sudo yum remove docker-ce

  2. sudo rm -rf /var/lib/docker

</pre>

参考文档

  • Docker 官方 CentOS 安装文档。

3、macOS 安装 Docker

系统要求

Docker for Mac 要求系统最低为 macOS 10.10.3 Yosemite。如果系统不满足需求,可以安装 Docker Toolbox。

安装

使用 Homebrew 安装

Homebrew 的 Cask 已经支持 Docker for Mac,因此可以很方便的使用 Homebrew Cask 来进行安装:

<pre class="" style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. $ brew cask install docker

</pre>

手动下载安装

如果需要手动下载,请点击以下链接下载 Stable 或 Edge 版本的 Docker for Mac。

如同 macOS 其它软件一样,安装也非常简单,双击下载的 .dmg 文件,然后将那只叫 Moby 的鲸鱼图标拖拽到 Application 文件夹即可(其间需要输入用户密码)。

图片

运行

从应用中找到 Docker 图标并点击运行。

图片

运行之后,会在右上角菜单栏看到多了一个鲸鱼图标,这个图标表明了 Docker 的运行状态。

图片

第一次点击图标,可能会看到这个安装成功的界面,点击 "Got it!" 可以关闭这个窗口。

图片

以后每次点击鲸鱼图标会弹出操作菜单。

图片

启动终端后,通过命令可以检查安装后的 Docker 版本。

<pre class="" style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. $ docker --version

  2. Docker version 17.10.0-ce, build f4ffd25

  3. $ docker-compose --version

  4. docker-compose version 1.17.0-rc1, build a0f95af

  5. $ docker-machine --version

  6. docker-machine version 0.13.0, build 9ba6da9

</pre>

如果 docker versiondocker info 都正常的话,可以尝试运行一个 Nginx 服务器:

<pre class="" style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. $ docker run -d -p 80:80 --name webserver nginx

</pre>

服务运行后,可以访问 http://localhost,如果看到了 "Welcome to nginx!",就说明 Docker for Mac 安装成功了。

图片

要停止 Nginx 服务器并删除执行下面的命令:

<pre class="" style="margin: 0px; padding: 8px 0px 6px; max-width: 100%; box-sizing: border-box; overflow-wrap: break-word !important; background-color: rgb(241, 239, 238); border-radius: 0px; overflow-y: auto; color: rgb(80, 97, 109); font-size: 10px; line-height: 12px; font-family: consolas, menlo, courier, monospace, "Microsoft Yahei" !important; border-width: 1px !important; border-style: solid !important; border-color: rgb(226, 226, 226) !important;">

  1. $ docker stop webserver

  2. $ docker rm webserver

</pre>

镜像加速

鉴于国内网络问题,后续拉取 Docker 镜像十分缓慢,强烈建议安装 Docker 之后配置国内镜像加速,参考(配置镜像加速器)

4、Windows 10 PC 安装 Docker CE

系统要求

Docker for Windows 支持 64 位版本的 Windows 10 Pro,且必须开启 Hyper-V。

安装

点击以下链接下载 Stable 或 Edge 版本的 Docker for Windows。

下载好之后双击 Docker for Windows Installer.exe 开始安装。

运行

在 Windows 搜索栏输入 Docker 点击 Docker for Windows 开始运行。

图片

Docker CE 启动之后会在 Windows 任务栏出现鲸鱼图标。

图片

等待片刻,点击 Got it 开始使用 Docker CE。

图片

镜像加速

鉴于国内网络问题,后续拉取 Docker 镜像十分缓慢,强烈建议安装 Docker 之后配置 国内镜像加速,参考(配置镜像加速器)。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,716评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,558评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,431评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,127评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,511评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,692评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,915评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,664评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,412评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,616评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,105评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,424评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,098评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,096评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,869评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,748评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,641评论 2 271

推荐阅读更多精彩内容