Docker 安装

Docker有很多种安装的选择,我们推荐您在Ubuntu下面安装,因为docker是在Ubuntu下面开发的,安装包测试比较充分,可以保证软件包的可用性。Mac, windows和其他的一些linux发行版本无法原生运行Docker,可以使用虚拟软件创建一个ubuntu的虚拟机并在里面运行docker。

Ubuntu Docker 安装

Docker Engine-Community 支持以下的 Ubuntu 版本:

Docker Engine - Community 支持上 x86_64(或 amd64)armhf,arm64,s390x (IBM Z),和 ppc64le(IBM的Power)架构。

卸载旧版本

Docker 的旧版本被称为 docker,docker.io 或 docker-engine 。如果已安装,请卸载它们:

apt remove docker docker-engine docker.io containerd runc

当前称为 Docker Engine-Community 软件包 docker-ce 。

安装 Docker Engine-Community,以下介绍两种方式。

使用 Docker 仓库进行安装

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

设置仓库

更新 apt 包索引。

curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"

curl -fsSL http://mirrors.huaweicloud.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] http://mirrors.huaweicloud.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"

apt update

添加 Docker 的官方 GPG 密钥:

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

||

curl -fsSL http://mirrors.huaweicloud.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

||

curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

安装 Docker Engine-Community

更新 apt 包索引。

sudo apt update

安装最新版本的 Docker Engine-Community 和 containerd ,或者转到下一步安装特定版本:

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

测试 Docker 是否安装成功,输入以下指令,打印出以下信息则安装成功:

docker run hello-world

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete                                                                                                                                  Digest: sha256:c3b4ada4687bbaa170745b3e4dd8ac3f194ca95b2d0518b417fb47e5879d9b5f
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/

使用 Shell 脚本进行安装

Docker 在 get.docker.com 和 test.docker.com 上提供了方便脚本,用于将快速安装 Docker Engine-Community 的边缘版本和测试版本。脚本的源代码在 docker-install 仓库中。 不建议在生产环境中使用这些脚本,在使用它们之前,您应该了解潜在的风险:

本示例使用 get.docker.com 上的脚本在 Linux 上安装最新版本的Docker Engine-Community。要安装最新的测试版本,请改用 test.docker.com。在下面的每个命令,取代每次出现 get 用 test。

curl -fsSL https://get.docker.com -o get-docker.sh
sh get-docker.sh

如果要使用 Docker 作为非 root 用户,则应考虑使用类似以下方式将用户添加到 docker 组:

usermod -aG docker your-user


usermod -aG docker develop

公共的镜像加速器

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://xviwkyb8.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

CentOS Docker 安装

Docker 支持以下的 64 位 CentOS 版本:

建议使用 overlay2 存储驱动程序。

卸载旧版本

较旧的 Docker 版本称为 docker 或 docker-engine 。如果已安装这些程序,请卸载它们以及相关的依赖项。

yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine

安装 Docker Engine-Community

使用 Docker 仓库进行安装

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

设置仓库

安装所需的软件包。yum-utils 提供了 yum-config-manager ,并且 device mapper 存储驱动程序需要 device-mapper-persistent-data 和 lvm2。

yum install -y yum-utils \
device-mapper-persistent-data \
lvm2

使用以下命令来设置稳定的仓库。

yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
http://mirrors.huaweicloud.com/docker-ce/linux/centos/docker-ce.repo

yum makecache fast

安装 Docker Engine-Community

安装最新版本的 Docker Engine-Community 和 containerd,或者转到下一步安装特定版本:

yum install docker-ce docker-ce-cli containerd.io

如果提示您接受 GPG 密钥,请选是。

有多个 Docker 仓库吗?

如果启用了多个 Docker 仓库,则在未在 yum install 或 yum update 命令中指定版本的情况下,进行的安装或更新将始终安装最高版本,这可能不适合您的稳定性需求。

Docker 安装完默认未启动。并且已经创建好 docker 用户组,但该用户组下没有用户。

要安装特定版本的 Docker Engine-Community,请在存储库中列出可用版本,然后选择并安装:

1、列出并排序您存储库中可用的版本。此示例按版本号(从高到低)对结果进行排序。

yum list docker-ce --showduplicates | sort -r

docker-ce.x86_64  3:18.09.1-3.el7                     docker-ce-stable
docker-ce.x86_64  3:18.09.0-3.el7                     docker-ce-stable
docker-ce.x86_64  18.06.1.ce-3.el7                    docker-ce-stable
docker-ce.x86_64  18.06.0.ce-3.el7                    docker-ce-stable

2、通过其完整的软件包名称安装特定版本,该软件包名称是软件包名称(docker-ce)加上版本字符串(第二列),从第一个冒号(:)一直到第一个连字符,并用连字符(-)分隔。例如:docker-ce-18.09.1。

yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io

启动 Docker。

systemctl start docker
systemctl enable docker

通过运行 hello-world 映像来验证是否正确安装了 Docker Engine-Community 。

docker run hello-world

如果要使用 Docker 作为非 root 用户,则应考虑使用类似以下方式将用户添加到 docker 组:

usermod -aG docker your-user

Windows Docker 安装

win7、win8 系统

win7、win8 等需要利用 docker toolbox 来安装,国内可以使用阿里云的镜像来下载,下载地址:windows/docker-toolbox

安装比较简单,双击运行,点下一步即可,可以勾选自己需要的组件:

docker toolbox 是一个工具集,它主要包含以下一些内容:

下载完成之后直接点击安装,安装成功后,桌边会出现三个图标,入下图所示:

点击 Docker QuickStart 图标来启动 Docker Toolbox 终端。

如果系统显示 User Account Control 窗口来运行 VirtualBox 修改你的电脑,选择 Yes。

$ 符号那你可以输入以下命令来执行。

$ docker run hello-world
 Unable to find image 'hello-world:latest' locally
 Pulling repository hello-world
 91c95931e552: Download complete
 a8219747be10: Download complete
 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 Engine CLI client contacted the Docker Engine daemon.
  2. The Docker Engine daemon pulled the "hello-world" image from the Docker Hub.
     (Assuming it was not already locally available.)
  3. The Docker Engine daemon created a new container from that image which runs the
     executable that produces the output you are currently reading.
  4. The Docker Engine daemon streamed that output to the Docker Engine CLI client, which sent it
     to your terminal.

 To try something more ambitious, you can run an Ubuntu container with:
  $ docker run -it ubuntu bash

 For more examples and ideas, visit:
  https://docs.docker.com/userguide/

Win10 系统

现在 Docker 有专门的 Win10 专业版系统的安装包,需要开启Hyper-V。

开启 Hyper-V

1、安装 Toolbox

最新版 Toolbox 下载地址: get-docker

点击 Download Desktop and Take a Tutorial,并下载 Windows 的版本,如果你还没有登录,会要求注册登录:

2、运行安装文件

镜像加速

Windows 10

对于使用 Windows 10 的系统,在系统右下角托盘 Docker 图标内右键菜单选择 Settings,打开配置窗口后左侧导航菜单选择 Daemon。在 Registrymirrors 一栏中填写加速器地址 registry.docker-cn ,之后点击 Apply 保存后 Docker 就会重启并应用配置的镜像地址了。

MacOS Docker 安装

使用 Homebrew 安装

macOS 我们可以使用 Homebrew 来安装 Docker。

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

$ brew cask install docker

==> Creating Caskroom at /usr/local/Caskroom
==> We'll set permissions properly so we won't need sudo in the future
Password:          # 输入 macOS 密码
==> Satisfying dependencies
==> Downloading https://download.docker.com/mac/stable/21090/Docker.dmg
######################################################################## 100.0%
==> Verifying checksum for Cask docker
==> Installing Cask docker
==> Moving App 'Docker.app' to '/Applications/Docker.app'.
&#x1f37a;  docker was successfully installed!

在载入 Docker app 后,点击 Next,可能会询问你的 macOS 登陆密码,你输入即可。之后会弹出一个 Docker 运行的提示窗口,状态栏上也有有个小鲸鱼的图标()。

手动下载安装

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

$ docker --version
Docker version 17.09.1-ce, build 19e2cf6

镜像加速

鉴于国内网络问题,后续拉取 Docker 镜像十分缓慢,我们可以需要配置加速器来解决,我使用的是网易的镜像地址:hub-mirror.c.163

在任务栏点击 Docker for mac 应用图标 -> Perferences... -> Daemon -> Registry mirrors。在列表中填写加速器地址即可。修改完成之后,点击 Apply & Restart 按钮,Docker 就会重启并应用配置的镜像地址了。

$ docker info
...
Registry Mirrors:
 http://hub-mirror.c.163.com
Live Restore Enabled: false

阿里云 docker 容器镜像服务

阿里云 docker 容器镜像服务

Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从Apache2.0协议开源。

Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。

容器是完全使用沙箱机制,相互之间不会有任何接口(类似 iPhone 的 app),更重要的是容器性能开销极低。

Docker 从 17.03 版本之后分为 CE(Community Edition: 社区版) 和 EE(Enterprise Edition: 企业版),我们用社区版就可以了。

Docker的应用场景

Docker 的优点

Docker 架构

Docker 使用客户端-服务器 (C/S) 架构模式,使用远程API来管理和创建Docker容器。

Docker 容器通过 Docker 镜像来创建。

容器与镜像的关系类似于面向对象编程中的对象与类。

Docker 面向对象
容器 对象
镜像
Docker 面向对象
Docker 镜像(Images) Docker 镜像是用于创建 Docker 容器的模板。
Docker 容器(Container) 容器是独立运行的一个或一组应用。
Docker 客户端(Client) Docker 客户端通过命令行或者其他工具使用 Docker API (https://docs.docker.com/reference/api/docker_remote_api) 与 Docker 的守护进程通信。
Docker 主机(Host) 一个物理或者虚拟的机器用于执行 Docker 守护进程和容器。
Docker 仓库(Registry) Docker 仓库用来保存镜像,可以理解为代码控制中的代码仓库。
Docker Hub(https://hub.docker.com) 提供了庞大的镜像集合供使用。
Docker Machine Docker Machine是一个简化Docker安装的命令行工具,通过一个简单的命令行即可在相应的平台上安装Docker,比如VirtualBox、 Digital Ocean、Microsoft Azure。

Docker 安装

Ubuntu Docker 安装

CentOS Docker 安装

Windows Docker 安装

MacOS Docker 安装

Docker 命令大全

Docker 资源汇总

Docker官方英文资源

docker官网:http://www.docker.com

Docker Windows 入门:https://docs.docker.com/docker-for-windows/

Docker CE(社区版) Ubuntu:https://docs.docker.com/install/linux/docker-ce/ubuntu/

Docker mac 入门:https://docs.docker.com/docker-for-mac/

Docker 用户指引:https://docs.docker.com/config/daemon/

Docker 官方博客:http://blog.docker.com/

Docker Hub: https://hub.docker.com/

Docker开源: https://www.docker.com/open-source

Docker 国内镜像

阿里云的加速器:https://help.aliyun.com/document_detail/60750.html

网易加速器:http://hub-mirror.c.163.com

官方中国加速器:https://registry.docker-cn.com

ustc的镜像:https://docker.mirrors.ustc.edu.cn

daocloud:https://www.daocloud.io/mirror#accelerator-doc(注册后使用)


容器镜像服务

https://cr.console.aliyun.com/

登录

docker login --username=itqmdx@qq.com registry.cn-hangzhou.aliyuncs.com

华东1(杭州)

公网地址

registry.cn-hangzhou.aliyuncs.com/wzhz/[image:[version]]

专有网络

registry-vpc.cn-hangzhou.aliyuncs.com/wzhz/[image:[version]]

经典网络

registry-internal.cn-hangzhou.aliyuncs.com/wzhz/[image:[version]]

华北1(青岛)

公网地址

registry.cn-qingdao.aliyuncs.com/wzhz/[image:[version]]

专有网络

registry-vpc.cn-qingdao.aliyuncs.com/wzhz/[image:[version]]

经典网络

registry-internal.cn-qingdao.aliyuncs.com/wzhz/[image:[version]]

华南1(深圳)

公网地址

registry.cn-shenzhen.aliyuncs.com/wzhz/[image:[version]]

专有网络

registry-vpc.cn-shenzhen.aliyuncs.com/wzhz/[image:[version]]

经典网络

registry-internal.cn-shenzhen.aliyuncs.com/wzhz/[image:[version]]

中国(香港)

公网地址

registry.cn-hongkong.aliyuncs.com/wzhz/[image:[version]]

专有网络

registry-vpc.cn-hongkong.aliyuncs.com/wzhz/[image:[version]]

经典网络

registry-internal.cn-hongkong.aliyuncs.com/wzhz/[image:[version]]

镜像加速器

加速器

使用加速器可以提升获取Docker官方镜像的速度

https://wzhz.mirror.aliyuncs.com

操作文档

1. 安装/升级Docker客户端

推荐安装1.10.0以上版本的Docker客户端,参考文档 docker-ce

2. 配置镜像加速器

针对Docker客户端版本大于 1.10.0 的用户

您可以通过修改daemon配置文件 /etc/docker/daemon.json 来使用加速器

创建文本
vim docker-aliyun-daemon
输入
#!/bin/bash

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://wzhz.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
执行
sudo chmod 755 docker-aliyun-daemon

./docker-aliyun-daemon

volume

# 列出未使用的volume
$ docker volume ls -qf dangling=true
# 删除未使用的volume
$ docker volume rm $(docker volume ls -qf dangling=true)
# 全部清除
$ docker system prune