系统要求
Docker 官方建议将 Docker 运行在 Linux系统上,当然也可以在其他平台运行,本篇博客只介绍在 Linux 系统上的安装方法。
Docker 运行在 CentOS7.X 版本以上,本文使用阿里云 ECS 云服务器 CentOS 7.4 版本。
Docker 需要安装在 64 位平台。
Docker 要求 CentOS 系统的内核版本高于 3.10 ,查看本页面的前提条件来验证你的 CentOS 版本是否支持 Docker。
通过 uname -r 命令查看你当前的内核版本 $ uname -r
。
root权限更新yum包
Linux 命令不熟悉的同学本文建议使用 root 权限登陆安装 Docker,省去很多不必要麻烦
yum update
卸载旧版本
列出安装过的 Docker 包:
yum list installed | grep docker
旧版名称是 Docker,最新社区版 docker-engine, 目前已改名为docker-ce
yum -y remove docker docker-common docker-selinux docker-engine
设置yum源
本文以 yum 安装为例子进行安装安装 yum-utils,使用 yum-config-manager 工具设置 yum 源,后面两个是 devicemapper 驱动依赖
yum install -y yum-utils device-mapper-persistent-data lvm2
使用阿里源访问
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
出现以下内容则表示成功
Loaded plugins: fastestmirror
adding repo from: http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
grabbing file http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
查看所有仓库中docker版本
选择特定版本安装,此处我们查看社区版 docker-ce。
yum list docker-ce --showduplicates | sort -r
Loading mirror speeds from cached hostfile
Loaded plugins: fastestmirror
docker-ce.x86_64 3:18.09.6-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.5-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.4-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.3-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.2-3.el7 docker-ce-stable
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.3.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.2.ce-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
docker-ce.x86_64 18.03.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 18.03.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.12.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.12.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.09.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.09.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.06.2.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.06.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.06.0.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.03.3.ce-1.el7 docker-ce-stable
docker-ce.x86_64 17.03.2.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.03.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.03.0.ce-1.el7.centos docker-ce-stable
Available Packages
安装docker
此处安装社区版。
yum install docker-ce (这样写默认安装最新版本)
yum install docker-ce- (指定安装版本)
例: yum install docker-ce-18.03.1.ce
安装成功提示信息如下:
Installed:
docker-ce.x86_64 3:18.09.6-3.el7
Dependency Installed:
container-selinux.noarch 2:2.95-2.el7_6
Complete!
启动并加入开机启动
systemctl start docker 启动
systemctl restart docker 重启
systemctl enable docker 开机启动
执行 docker version
查看 Docker 版本号。
Client:
Version: 18.09.6
API version: 1.39
Go version: go1.10.8
Git commit: 481bc77156
Built: Sat May 4 02:34:58 2019
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 18.09.6
API version: 1.39 (minimum version 1.12)
Go version: go1.10.8
Git commit: 481bc77
Built: Sat May 4 02:02:43 2019
OS/Arch: linux/amd64
Experimental: false
验证是否安装成功
执行命令:docker run hello-world
测试是否安装成功。
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 已经安装成功了 。
接下来我们就可以访问并下载你所需要的镜像,或者使用 dockerfile 自行构建 Docker 镜像,部署安装你的应用程序到 Docker 容器中了。
配置可用镜像
注意,现在 Docker 拉取镜像被限制了,我们需要额外执行以下逻辑:
# 创建目录
sudo mkdir -p /etc/docker
# 写入配置文件
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": [
"https://docker.unsee.tech",
"https://dockerpull.org",
"https://docker.1panel.live",
"https://dockerhub.icu"
]
}
EOF
# 重启docker服务
sudo systemctl daemon-reload && sudo systemctl restart docker