环境
1、安装Docker要求CentOS至少是CentOS 7,且内核版本不低于3.10
CentOS 7(内核:4.18.13-1.el7.elrepo.x86_64)
2、Docker版本:18.06.1-ce
安装Docker
使用阿里云官方安装脚本自动安装
$ curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
离线安装Docker
$ wget https://yum.dockerproject.org/repo/main/centos/7/Packages/docker-engine-1.12.6-1.el7.centos.x86_64.rpm
$ wget https://yum.dockerproject.org/repo/main/centos/7/Packages/docker-engine-selinux-1.12.6-1.el7.centos.noarch.rpm
$ yum localinstall -y docker-engine*
启动Docker
# 设置docker开机启动
$ systemctl start docker.service
#启动docker
$ systemctl enable docker.service
设置非root用户免sudo操作docker
# 添加docker用户组
$ sudo groupadd docker
# 将当前用户加入docker用户组
$ sudo gpasswd -a ${USER} docker
# 重启docker
$ sudo service docker restart
# 切换用户组
$ newgrp - 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
Docker配置阿里云镜像加速器
国内访问Docker Hub可能有些慢,因此我们需要配置加速器。阿里云提供了阿里云加速器,注册用户后登陆https://cr.console.aliyun.com/cn-hangzhou/repositories
,便可以领取一个加速器。
$ sudo mkdir -p /etc/docker
$ sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://xxxxximyvi.mirror.aliyuncs.com"]
}
EOF
$ sudo systemctl daemon-reload
$ sudo systemctl restart docker
CentOS 7开启Docker远程访问
Docker默认守护进程Unix socket(/var/run/docker.sock)来进行本地进程通信,不监听任何端口,因此只能在本地使用docker客户端或者使用Docker API进行操作。如果想在其他主机上操作Docker主机,就需要让Docker守护进程打开一个HTTP Socket,这样才能实现远程通信。实现方式如下:
$ vi /usr/lib/systemd/system/docker.service
ExecStart=-H tcp://0.0.0.0:2376 -H unix://var/run/docker.sock
$ systemctl daemon-reload
$ systemctl restart docker
此时使用如下命令可验证是否开启远程访问:
$ curl docker_host_ip:2376/version
{"Version":"17.03.0-ce","ApiVersion":"1.26","MinAPIVersion":"1.12","GitCommit":"60ccb22","GoVersion":"go1.7.5","Os":"linux","Arch":"amd64","KernelVersion":"3.10.0-693.5.2.el7.x86_64","BuildTime":"2017-02-23T10:54:03.848297239+00:00"}
# docker_host_ip:为要访问的docker所在的主机ip