# 使用 docker 官方的脚本安装 docker 服务
提示
本文是对以下 2 篇文章的镜像:
非常感谢原作者的创作和分享。
# 安装 docker 的方法
在 Linux 上安装 docker 有 2 种方法:
- 使用包管理软件,例如
apt,添加软件源后,进行安装 - 使用 docker 官方的 scripts 进行自动安装
使用 scripts 进行安装的好处是:全自动、多系统通用;
下面介绍下如何使用 scripts 进行安装;
# 使用脚本进行安装
此脚本为 docker 官方维护,支持选择 mirror 镜像,以加速中国大陆区域用户的安装;
脚本地址:Get Docker (opens new window)
如果需要使用 test 版本的 docker,地址修改为 https://test.docker.com
使用下面的命令下载镜像,并进行安装:
curl -fsSL get.docker.com -o get-docker.sh
sudo sh get-docker.sh --mirror Aliyun
2
这里使用 阿里云 的 mirror (opens new window) 进行安装;
可选镜像的名称为:
提示
你可以下载好 scripts 之后,修改里面的内容,新增你偏好的镜像站点;
稍等片刻,脚本会自动安装其他的依赖,然后安装好 docker;
# 启动 docker 服务
设置开机自动启动;然后启动 docker 服务;
sudo systemctl enable docker
sudo systemctl start docker
2
# 建立 docker 用户组
默认情况下,docker 命令会使用 Unix socket 与 Docker 引擎通讯。
而只有 root 用户和 docker 组的用户才可以访问 Docker 引擎的 Unix socket。
出于安全考虑,一般 Linux 系统上不会直接使用 root 用户。
因此,更好地做法是将需要使用 docker 的用户加入 docker 用户组。
建立 docker 组:
sudo groupadd docker
将当前用户加入 docker 组:
sudo usermod -aG docker $USER
退出当前终端并重新登录,进行如下测试。
# 测试 Docker 是否安装正确
docker run --rm hello-world
如果获得类似如下的输出,则说明安装正常:
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete
Digest: sha256:308866a43596e83578c7dfa15e27a73011bdd402185a84c5cd7f32a88b501a24
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/
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
# 配置镜像加速器
如果在使用过程中发现拉取 Docker 镜像十分缓慢,可以配置 Docker 国内镜像加速 (opens new window)。
国内从 Docker Hub 拉取镜像有时会遇到困难,此时可以配置镜像加速器。国内很多云服务商都提供了国内加速器服务,例如:
- 阿里云加速器 (点击管理控制台 -> 登录账号(淘宝账号) -> 右侧镜像工具 -> 镜像加速器 -> 复制加速器地址) (opens new window)
- 网易云加速器 (opens new window)
- 百度云加速器 (opens new window)
由于镜像服务可能出现宕机,建议同时配置多个镜像。各个镜像站测试结果请到 docker-practice/docker-registry-cn-mirror-test (opens new window) 查看。
国内各大云服务商(腾讯云、阿里云、百度云)均提供了 Docker 镜像加速服务,建议根据运行 Docker 的云平台选择对应的镜像加速服务,具体请参考本页最后一小节。 本节我们以 网易云 镜像服务 https://hub-mirror.c.163.com (opens new window) 为例进行介绍。
# Ubuntu 16.04+、Debian 8+、CentOS 7+
目前主流 Linux 发行版均已使用 systemd 进行服务管理,这里介绍如何在使用 systemd 的 Linux 发行版中配置镜像加速器。
请首先执行以下命令,查看是否在 docker.service 文件中配置过镜像地址。
systemctl cat docker | grep '\-\-registry\-mirror'
如果该命令有输出,那么请执行 systemctl cat docker 查看 ExecStart= 出现的位置,修改对应的文件内容去掉 --registry-mirror 参数及其值,并按接下来的步骤进行配置。
如果以上命令没有任何输出,那么就可以在 /etc/docker/daemon.json 中写入如下内容(如果文件不存在请新建该文件):
{
"registry-mirrors": [
"https://hub-mirror.c.163.com",
"https://mirror.baidubce.com"
]
}
2
3
4
5
6
注意,一定要保证该文件符合 json 规范,否则 Docker 将不能启动。
之后重新启动服务。
sudo systemctl daemon-reload
sudo systemctl restart docker
2
注意
请一定要重启 docker 服务
# 检查加速器是否生效
执行 docker info,如果从结果中看到了如下内容,说明配置成功。
Registry Mirrors:
https://hub-mirror.c.163.com/
2
# k8s.gcr.io 镜像
可以登录 阿里云 容器镜像服务 镜像中心 -> 镜像搜索 查找。
例如 k8s.gcr.io/coredns:1.6.7 镜像可以用 registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.6.7 代替。
一般情况下有如下对应关系:
docker pull k8s.gcr.io/xxx
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/xxx
# 不再提供服务的镜像
某些镜像不再提供服务,添加无用的镜像加速器,会拖慢镜像拉取速度,你可以从镜像配置列表中删除它们。
- https://dockerhub.azk8s.cn 已转为私有 (opens new window)
- https://reg-mirror.qiniu.com (opens new window)
- https://registry.docker-cn.com (opens new window)
建议 watch(页面右上角) 镜像测试 这个 GitHub 仓库 (opens new window),我们会在此更新各个镜像地址的状态。
# 云服务商
某些云服务商提供了 仅供内部 访问的镜像服务,当您的 Docker 运行在云平台时可以选择它们。