CentOS7 docker试水

时间:2022-01-20 11:53:06

CentOS 7.0,无外网直接访问权限,有一台代理服务器。

首先安装docker-ce,参考http://blog.51cto.com/aaronsa/2056882

除非特殊说明,以下操作都用root用户:

$ export http_proxy=http://xxxx

$ export https_proxy=http://xxxx

$ yum install -y yum-utils # 安装yum-config-manager

$ yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo # 添加docker-ce yum源

$ yum install docker-ce

$ systemctl start docker

第一个坑,启动失败,通过journalctl -xe查看启动日志,报错

devmapper: Error while creating filesystem xfs on device ....

参考http://www.cnblogs.com/FoChen/p/8708932.html

$ yum update xfsprogs

第二个坑,普通用户无法使用docker命令,报错

Got permission denied while trying to connect to the Docker daemon socket at ...

查了一下资料,原来docker命令通过一个Unix socket与docker daemon通信,涉及到对Unix socket 访问权限问题,参考https://www.cnblogs.com/franson-2016/p/6412971.html

查了一下已经有docker组了,应该是yum install docker-ce时自动创建的,于是把普通用户添加进docker组就可以了;

$ gpasswd -a <user> docker

普通用户需要重新登录;

第三个坑,docker pull hello-world,报错:

Error response from daemon: Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)

确认了一下代理服务器,不是代理服务器出了问题;

在浏览器里试了一下https://registry-1.docker.io,没有内容,以为是被墙了,大误,导致走了一大段错路,后来回头想想看,这应该是个api服务器,只是对空请求返回空结果而已,HTTP返回码是200,不是被墙,哪怕当时试一下https://registry-1.docker.io/v2/,都不会走这条弯路;

刚开始按照错误思路,想添加国内registry镜像,查找资料:

  • 尝试在/etc/default/docker和/etc/sysconfig/docker中通过DOCKER_OPTS设置--registry-mirror参数无效,疑似配置文件不对;
  • 参考了很多资料之后(尤其是DaoCloud的Docker加速器文档和set_mirror.sh脚本),了解到docker从1.10~1.12版本经历了一次改版,开始使用/etc/docker/daemon.json下的json格式的配置文件,连版本号格式都变了;于是在/etc/docker/daemon.json中写入docker镜像加速器的地址;(无论aliyun还是daocloud,都要求注册登录以获取专用加速器地址,如果按照别人的文档把别人的加速器地址拿来用了,应该会发生带宽抢占);

既然是错误思路,当然没有解决我的问题,不过也不算是空手而归,对docker的架构有了些微了解:

  • docker pull的时候,dockerd服务是干活的主体,docker工具就是个命令行封装;
  • docker的hub与registry服务器,有点类似于openstack里的glance和swift;

正确思路是将代理设置到dockerd的环境变量里,这就涉及到了systemd的一点知识,参考了Arch-wiki

然后重启dockerd服务;

$ vi /etc/systemd/system/docker.service.d/proxy.conf

[Service]
Environment="HTTP_PROXY=192.168.1.1:8080"
Environment="HTTPS_PROXY=192.168.1.1:8080" $ systemctl daemon-reload $ systemctl show docker --property Environment #确认环境变量生效 $ systemctl restart docker

用普通用户再试一下:

$ docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
9bb5a5d4561a: Pull complete
Digest: sha256:f5233545e43561214ca4891fd1157e1c3c563316ed8e237750d59bde73361e77
Status: Downloaded newer image for hello-world:latest $ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest e38bc07ac18e 2 weeks ago 1.85kB $ 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/engine/userguide/