1、检查环境
a)查看CentOS版本
[root@localhost etc]# cat /etc/system-release
CentOS Linux release 7.3.1611 (Core)
b)查看内核版本(需高于3.10)
[root@localhost ~]# uname -r
3.10.0-514.el7.x86_64
2、更新yum包(需root权限)
[root@localhost ~]# sudo yum update
3、卸载旧版本(如果安装过旧版本的话)
[root@localhost ~]# sudo yum remove docker docker-common docker-selinux docker-engine
4、安装需要的软件包
yum-util 提供yum-config-manager功能
另外两个是devicemapper驱动依赖的
[root@localhost ~]# sudo yum install -y yum-utils device-mapper-persistent-data lvm2
5、设置yum源
[root@localhost ~]# sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
Loaded plugins: fastestmirror, langpacks
adding repo from: https://download.docker.com/linux/centos/docker-ce.repo
grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
6、可以查看所有仓库中所有docker版本,并选择特定版本安装
[root@localhost ~]# yum list docker-ce --showduplicates | sort -r
* updates: mirrors.tuna.tsinghua.edu.cn
Loading mirror speeds from cached hostfile
Loaded plugins: fastestmirror, langpacks
* extras: mirrors.tuna.tsinghua.edu.cn
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
* base: mirrors.aliyun.com
Available Packages
7、安装docker
a)在线
[root@localhost ~]# sudo yum install docker-ce
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.tuna.tsinghua.edu.cn
* updates: mirrors.tuna.tsinghua.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package docker-ce.x86_64 0:18.06.1.ce-3.el7 will be installed
--> Processing Dependency: container-selinux >= 2.9 for package: docker-ce-18.06.1.ce-3.el7.x86_64
--> Running transaction check
---> Package container-selinux.noarch 2:2.68-1.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=======================================================================================================================================
Package Arch Version Repository Size
=======================================================================================================================================
Installing:
docker-ce x86_64 18.06.1.ce-3.el7 docker-ce-stable 41 M
Installing for dependencies:
container-selinux noarch 2:2.68-1.el7 extras 36 k
Transaction Summary
=======================================================================================================================================
Install 1 Package (+1 Dependent package)
Total download size: 41 M
Installed size: 41 M
Is this ok [y/d/N]: y
b)离线
下载
[root@localhost docker]# pwd
/usr/local/src/docker
[root@localhost docker]# ll
total 41748
-rw-r--r--. 1 root root 35676 Oct 3 22:34 container-selinux-2.68-1.el7.noarch.rpm
-rw-r--r--. 1 root root 42712724 Oct 3 22:26 docker-ce-18.06.1.ce-3.el7.x86_64.rpm
安装
[root@localhost docker]# rpm -ivh docker-ce-18.06.1.ce-3.el7.x86_64.rpm
warning: docker-ce-18.06.1.ce-3.el7.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 621e9f35: NOKEY
error: Failed dependencies:
container-selinux >= 2.9 is needed by docker-ce-18.06.1.ce-3.el7.x86_64
注意:需先安装依赖!
下面代码为正确安装顺序
[root@localhost docker]# rpm -ivh container-selinux-2.68-1.el7.noarch.rpm
warning: container-selinux-2.68-1.el7.noarch.rpm: Header V4 DSA/SHA1 Signature, key ID 192a7d7d: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:container-selinux-2:2.68-1.el7 ################################# [100%]
[root@localhost docker]# rpm -ivh docker-ce-18.06.1.ce-3.el7.x86_64.rpm
warning: docker-ce-18.06.1.ce-3.el7.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 621e9f35: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:docker-ce-18.06.1.ce-3.el7 ################################# [100%]
[root@localhost docker]#
8、启动并加入开机启动
[root@localhost docker]# sudo systemctl start docker
[root@localhost docker]# sudo systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
9、验证安装是否成功
(有client和service两部分表示docker安装启动都成功了)
[root@localhost docker]# docker version
Client:
Version: 18.06.1-ce
API version: 1.38
Go version: go1.10.3
Git commit: e68fc7a
Built: Tue Aug 21 17:23:03 2018
OS/Arch: linux/amd64
Experimental: false
Server:
Engine:
Version: 18.06.1-ce
API version: 1.38 (minimum version 1.12)
Go version: go1.10.3
Git commit: e68fc7a
Built: Tue Aug 21 17:25:29 2018
OS/Arch: linux/amd64
Experimental: false
10、启用停用
[root@localhost docker]# systemctl stop docker.service
[root@localhost docker]# systemctl start docker.service
11、镜像加速
[root@localhost workspace]# mkdir -p /etc/docker
[root@localhost docker]# sudo tee -a /etc/docker/daemon.json <<-EOF
{
"registry-mirrors": ["https://registry.docker-cn.com"]
}
EOF
[root@localhost workspace]# sudo systemctl daemon-reload
[root@localhost workspace]# sudo systemctl restart docker
12、IP转发
sudo tee -a /etc/sysctl.conf <<-EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
# 重新加载
sudo sysctl -p
13、建立用户
# 增加docker用户到 docker组
useradd docker -g docker
# 修改密码
passwd docker
sudo systemctl restart docker