1、环境检查
docker支持centos6.5及以后的版本,建议使用centos 7
要求64位操作系统,内核版本至少为3.10
查看内核版本:
[root@lyy ~]# uname -a
Linux lyy 3.10.0-514.el7.x86_64 #1 SMP Tue Nov 22 16:42:41 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
3.10.0-514.el7.x86_64 即为内核版本号,有x86_64就是64位的,没有就是32位的
2、在线安装
添加docker的yum源:
在https://download.docker.com/linux/centos/docker-ce.repo下载docker-ce.repo
将docker-ce.repo放到/etc/yum.repos.d目录下
清空yum源的缓存并安装docker-ce
[root@lyy yum.repos.d]# yum clean all
[root@localhost yum.repos.d]# yum list docker-ce --showduplicates | sort -r
* updates: mirrors.cn99.com
Loading mirror speeds from cached hostfile
Loaded plugins: fastestmirror
* extras: mirrors.huaweicloud.com
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
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.163.com
Available Packages
[root@lyy yum.repos.d]# yum install -y docker-ce
[root@localhost yum.repos.d]# docker version
Client:
Version: 18.09.0
API version: 1.39
Go version: go1.10.4
Git commit: 4d60db4
Built: Wed Nov 7 00:48:22 2018
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 18.09.0
API version: 1.39 (minimum version 1.12)
Go version: go1.10.4
Git commit: 4d60db4
Built: Wed Nov 7 00:19:08 2018
OS/Arch: linux/amd64
Experimental: false
如果想在非root用户下正常使用docker命令,可以将非root用户添加到docker安装时自动创建的用户组docker中:
[root@localhost ~]# usermod -aG docker lyytest
[root@localhost ~]# su - lyytest
Last login: Mon Dec 17 20:20:01 CST 2018 on pts/0
[lyytest@localhost ~]$ docker version
Client:
Version: 18.09.0
API version: 1.39
Go version: go1.10.4
Git commit: 4d60db4
Built: Wed Nov 7 00:48:22 2018
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 18.09.0
API version: 1.39 (minimum version 1.12)
Go version: go1.10.4
Git commit: 4d60db4
Built: Wed Nov 7 00:19:08 2018
OS/Arch: linux/amd64
Experimental: false
3、离线安装
通过二进制文件安装
(1)下载二进制安装包,从下面的网站获取一个稳定的版本,下载
网址1:https://yum.dockerproject.org/repo/main/centos/7/Packages/
网址2:https://download.docker.com/linux/static/stable/ (or change stable to edge or test)
(2)将包传到服务器并解压:
[root@lyy opt]# tar -xvf docker-18.06.1-ce.tgz
docker/
docker/docker-containerd
docker/docker-proxy
docker/docker
docker/docker-runc
docker/dockerd
docker/docker-containerd-ctr
docker/docker-containerd-shim
docker/docker-init
[root@lyy opt]# ll
total 4320068
-rw-r--r--. 1 root root 4379901952 Dec 15 16:43 CentOS-7-x86_64-DVD-1611.iso
drwxrwxr-x. 2 1000 1000 4096 Aug 22 01:29 docker
-rw-r--r--. 1 root root 43831680 Dec 15 17:30 docker-18.06.1-ce.tgz
-rw-r--r--. 1 root root 877 Dec 15 16:45 maxc.repo
[root@lyy opt]# cd docker
[root@lyy docker]# ll
total 143620
-rwxr-xr-x. 1 1000 1000 37589867 Aug 22 01:29 docker
-rwxr-xr-x. 1 1000 1000 26393752 Aug 22 01:29 docker-containerd
-rwxr-xr-x. 1 1000 1000 14725592 Aug 22 01:29 docker-containerd-ctr
-rwxr-xr-x. 1 1000 1000 4173632 Aug 22 01:29 docker-containerd-shim
-rwxr-xr-x. 1 1000 1000 53072264 Aug 22 01:29 dockerd
-rwxr-xr-x. 1 1000 1000 764144 Aug 22 01:29 docker-init
-rwxr-xr-x. 1 1000 1000 2837280 Aug 22 01:29 docker-proxy
-rwxr-xr-x. 1 1000 1000 7495056 Aug 22 01:29 docker-runc
3、将docker目录下的二进制文件拷贝到/usr/bin下面
[root@lyy docker]# cd ../
[root@lyy opt]# cp docker/* /usr/bin/
4、启动docker
[root@lyy opt]#setsid dockerd >/dev/null 2>&1 &
5、添加为服务
在/usr/lib/systemd/system/目录下新增docker.service
内容如下:
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
BindsTo=containerd.service
After=network-online.target firewalld.service
Wants=network-online.target
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd -H unix://
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always
# Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
# Both the old, and new location are accepted by systemd 229 and up, so using the old location
# to make them work for either version of systemd.
StartLimitBurst=3
# Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.
# Both the old, and new name are accepted by systemd 230 and up, so using the old name to make
# this option work for either version of systemd.
StartLimitInterval=60s
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
# Comment TasksMax if your systemd version does not supports it.
# Only systemd 226 and above support this option.
TasksMax=infinity
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
# kill only the docker process, not all processes in the cgroup
KillMode=process
[Install]
WantedBy=multi-user.target
后面便可以使用指令systemctl启动/停止/重启/查看状态 docker服务
systemctl start docker.service
systemctl stop docker.service
systemctl restart docker.service
systemctl status docker.service