docker 私有镜像仓库搭建

原文:https://blog.csdn.net/wu_di_xiao_wei/article/details/54755475

如果大家在操作的时候发现本文中的问题,敬请留言,争取把这边文章弄成可用性比较高的文章!!!!!!!!!!!!!!


我的环境相关设置如下


环境:centos7

IP地址:10.211.55.30

dockere版本:1.10.3

镜像仓库:v2


首先在10.211.55.30机器上下载registry镜像

[html]  view plain  copy
  1. $ docker pull registry  

也可以进行镜像导入的方法进行离线的安装,可以去我的网盘中下载:https://pan.baidu.com/s/1mhY0YDy

然后进入docker中进行导入

[html]  view plain  copy
  1. $ docker load -i registry.tar  

下载完之后我们通过该镜像启动一个容器

[html]  view plain  copy
  1. $ docker run -d -p 5000:5000 registry  

默认情况下,会将仓库存放于容器内的/tmp/registry目录下,这样如果容器被删除,则存放于容器中的镜像也会丢失,所以我们一般情况下会指定本地一个目录挂载到容器内的/tmp/registry下,我将/opt/data/registry目录挂载到/tmp/registry目录下,如果你本地没有这个目录需要新创建,同时需要给/opt/data/registry目录扩大权限

[html]  view plain  copy
  1. chmod +777 /opt/data/registry  

此处有坑:默认情况下是在容器内的/tmp/registry目录下,但是我的容器镜像是存放在容器中的/var/lib/registry  这个位置。

我是搭建完毕之后,上传一个镜像之后然后使用 find / -name ***查到的位置

[html]  view plain  copy
  1. [root@server01 ~]# docker run -d -p 5000:5000 -v /opt/data/registry:/var/lib/registry registry  
  2. 55c60589cb0e2d094d5371c4dd650127cfeae1b361477d50cfe48552e6308830  

可以看到我们启动了一个容器,地址为:10.211.55.30:5000

 

测试

接下来我们就要操作把一个本地镜像push到私有仓库中。首先在10.211.55.30机器下pull一个比较小的镜像来测试(此处使用的是busybox)

[html]  view plain  copy
  1. $ sudo docker pull busybox  

接下来修改一下该镜像的tag,镜像的格式为  镜像仓库IP:端口/镜像名称

[html]  view plain  copy
  1. $ sudo docker tag busybox 10.211.55.30:5000/busybox  

接下来把打了tag的镜像上传到私有仓库。

[html]  view plain  copy
  1. $ sudo docker push 10.211.55.30:5000/busybox  

可以看到push失败,具体错误如下:

[html]  view plain  copy
  1. 2015/01/05 11:01:17 Error: Invalid registry endpoint https://192.168.112.136:5000/v1/: Get https://192.168.112.136:5000/v1/_ping: dial tcp 192.168.112.136:5000: connection refused. If this private registry supports only HTTP or HTTPS with an unknown CA certificate, please add `--insecure-registry 192.168.112.136:5000` to the daemon's arguments. In the case of HTTPS, if you have access to the registry's CA certificate, no need for the flag; simply place the CA certificate at /etc/docker/certs.d/192.168.112.136:5000/ca.crt  

因为Docker从1.3.X之后,与docker registry交互默认使用的是https,然而此处搭建的私有仓库只提供http服务,所以当与私有仓库交互时就会报上面的错误。为了解决这个问题需要在启动docker server时增加启动参数为默认使用http访问。修改docker启动配置文件(此处是修改10.211.55.30机器的配置)centos7下配置文件地址为:/usr/lib/systemd/system/docker.service,在其中增加–insecure-registry 10.211.55.30:5000如下所示:

[html]  view plain  copy
  1. [Unit]  
  2. Description=Docker Application Container Engine  
  3. Documentation=http://docs.docker.com  
  4. After=network.target rhel-push-plugin.socket  
  5. Wants=docker-storage-setup.service  
  6.    
  7. [Service]  
  8. Type=notify  
  9. NotifyAccess=all  
  10. EnvironmentFile=-/etc/sysconfig/docker  
  11. EnvironmentFile=-/etc/sysconfig/docker-storage  
  12. EnvironmentFile=-/etc/sysconfig/docker-network  
  13. Environment=GOTRACEBACK=crash  
  14. ExecStart=/usr/bin/docker-current daemon \  
  15.           --exec-opt native.cgroupdriver=systemd \  
  16.           --insecure-registry=10.211.55.30:5000 \  
  17.           $OPTIONS \  
  18.           $DOCKER_STORAGE_OPTIONS \  
  19.           $DOCKER_NETWORK_OPTIONS \  
  20.           $ADD_REGISTRY \  
  21.           $BLOCK_REGISTRY \  
  22.           $INSECURE_REGISTRY  
  23. LimitNOFILE=1048576  
  24. LimitNPROC=1048576  
  25. LimitCORE=infinity  
  26. TimeoutStartSec=0  
  27. MountFlags=slave  
  28. Restart=on-abnormal  
  29.    
  30. [Install]  
  31. WantedBy=multi-user.target  

修改完之后,重启Docker服务。

[html]  view plain  copy
  1. $ restart docker  

重启完之后我们再次运行推送命令,把本地镜像推送到私有服务器上。

[html]  view plain  copy
  1. $ sudo docker push 10.211.55.30:5000/busybox  
可以看到镜像已经push到私有仓库中去了。

进行到这一步的时候也不一定能够成功,使用journalctl -f 可以查看日志信息,通过日志信息可以查看到如下信息,关注标红的部分,显示的是SELinux 的问题,我的处理方法是直接关闭SELinux

[html]  view plain  copy
  1. Jan 27 16:08:16 server01 docker-current[15241]: time="2017-01-27T08:08:16Z" level=error msg="response completed with error" err.code="blob unknown" err.detail=sha256:45a2e645736c4c66ef34acce2407ded21f7a9b231199d3b92d6c9776df264729 err.message="blob unknown to registry" go.version=go1.7.3 http.request.host="10.211.55.30:5000" http.request.id=a2dbff10-2937-4e9e-94f3-16275739ad61 http.request.method=HEAD http.request.remoteaddr="10.211.55.30:48256" http.request.uri="/v2/centos_20170127/blobs/sha256:45a2e645736c4c66ef34acce2407ded21f7a9b231199d3b92d6c9776df264729" http.request.useragent="docker/1.10.3 go/go1.6.3 git-commit/cb079f6-unsupported kernel/3.10.0-327.36.3.el7.x86_64 os/linux arch/amd64" http.response.contenttype="application/json; charset=utf-8" http.response.duration=1.4262ms http.response.status=404 http.response.written=157 instance.id=f9f97de9-15bc-41e3-9ec3-f1033e57a77e vars.digest="sha256:45a2e645736c4c66ef34acce2407ded21f7a9b231199d3b92d6c9776df264729" vars.name="centos_20170127" version=v2.6.0  
  2. Jan 27 16:08:16 server01 docker-current[15241]: 10.211.55.30 - - [27/Jan/2017:08:08:16 +0000] "HEAD /v2/centos_20170127/blobs/sha256:45a2e645736c4c66ef34acce2407ded21f7a9b231199d3b92d6c9776df264729 HTTP/1.1" 404 157 "" "docker/1.10.3 go/go1.6.3 git-commit/cb079f6-unsupported kernel/3.10.0-327.36.3.el7.x86_64 os/linux arch/amd64"  
  3. Jan 27 16:08:16 server01 docker-current[15241]: time="2017-01-27T08:08:16Z" level=error msg="response completed with error" err.code=unknown err.detail="filesystem: mkdir /var/lib/registry/docker: permission denied" err.message="unknown error" go.version=go1.7.3 http.request.host="10.211.55.30:5000" http.request.id=158612a0-39f5-41f5-9985-c80db7da911f http.request.method=POST http.request.remoteaddr="10.211.55.30:48258" http.request.uri="/v2/centos_20170127/blobs/uploads/" http.request.useragent="docker/1.10.3 go/go1.6.3 git-commit/cb079f6-unsupported kernel/3.10.0-327.36.3.el7.x86_64 os/linux arch/amd64" http.response.contenttype="application/json; charset=utf-8" http.response.duration=1.671997ms http.response.status=500 http.response.written=164 instance.id=f9f97de9-15bc-41e3-9ec3-f1033e57a77e vars.name="centos_20170127" version=v2.6.0  
  4. Jan 27 16:08:16 server01 docker-current[15241]: 10.211.55.30 - - [27/Jan/2017:08:08:16 +0000] "POST /v2/centos_20170127/blobs/uploads/ HTTP/1.1" 500 164 "" "docker/1.10.3 go/go1.6.3 git-commit/cb079f6-unsupported kernel/3.10.0-327.36.3.el7.x86_64 os/linux arch/amd64"  
  5. Jan 27 16:08:16 server01 docker-current[15241]: time="2017-01-27T16:08:16.115997771+08:00" level=error msg="Upload failed, retrying: Received unexpected HTTP status: 500 Internal Server Error"  
  6. Jan 27 16:08:16 server01 setroubleshoot[5771]: failed to retrieve rpm info for /opt/data/registry  
  7. Jan 27 16:08:16 server01 setroubleshoot[5771]: SELinux is preventing /bin/registry from write access on the directory /opt/data/registry. For complete SELinux messages. run sealert -l 748743d8-dd8a-4482-9771-94a403bccf18  
  8. Jan 27 16:08:16 server01 python[5771]: <strong><span style="color:#cc0000;">SELinux is preventing /bin/registry from write access on the directory /opt/data/registry.</span></strong>  
  9.                                         
  10.                                        *****  Plugin catchall_labels (83.8 confidence) suggests   *******************  
  11.                                         
  12.                                        If you want to allow registry to have write access on the registry directory  
  13.                                        Then you need to change the label on /opt/data/registry  
  14.                                        Do  
  15.                                        # semanage fcontext -a -t FILE_TYPE '/opt/data/registry'  
  16.                                        where FILE_TYPE is one of the following: cgroup_t, docker_var_lib_t, svirt_home_t, svirt_sandbox_file_t, virt_home_t.  
  17.                                        Then execute:  
  18.                                        restorecon -v '/opt/data/registry'  
  19.                                         
  20.                                         
  21.                                        *****  Plugin catchall (17.1 confidence) suggests   **************************  
  22.                                         
  23.                                        If you believe that registry should be allowed write access on the registry directory by default.  
  24.                                        Then you should report this as a bug.  
  25.                                        You can generate a local policy module to allow this access.  
  26.                                        Do  
  27.                                        allow this access for now by executing:  
  28.                                        # grep registry /var/log/audit/audit.log | audit2allow -M mypol  
  29.                                        # semodule -i mypol.pp  

关闭方法如下

[html]  view plain  copy
  1. 查看SELinux状态:  
  2. 1、/usr/sbin/sestatus -v      ##如果SELinux status参数为enabled即为开启状态  
  3. SELinux status:                 enabled  
  4. 2、getenforce                 ##也可以用这个命令检查  
  5. 关闭SELinux:  
  6. 1、临时关闭(不用重启机器):  
  7. setenforce 0                  ##设置SELinux 成为permissive模式 setenforce 1 设置SELinux 成为enforcing模式  
  8. 2、修改配置文件需要重启机器:  
  9. 修改/etc/selinux/config 文件  
  10. SELINUX=enforcing改为SELINUX=disabled  
  11. 重启机器即可  

接下来我们删除本地镜像,然后从私有仓库中pull下来该镜像。

[html]  view plain  copy
  1. $ sudo docker pull 10.211.55.30:5000/busybox  

到此就搭建好了Docker私有仓库。上面搭建的仓库是不需要认证的。


管理仓库中的镜像

 

查询

在Private Registry2中查看或检索Repository或images, 将不能用docker search,会报下边的错误

[html]  view plain  copy
  1. $ docker search 10.211.55.30:5000/busybox/  
  2. Error response from daemon: Unexpected status code 404  

但通过v2版本的API,我们可以实现相同目的,必须按照IP:port/v2/_catalog格式:

[html]  view plain  copy
  1. [root@server01 ~]# curl http://10.211.55.30:5000/v2/_catalog         
  2. {"repositories":["centos"]}  
  3. [root@server01 ~]# curl http://10.211.55.30:5000/v2/centos/tags/list  
  4. {"name":"centos","tags":["latest"]}  

拉取镜像如下

[html]  view plain  copy
  1. [root@server01 ~]# docker pull 10.211.55.30:5000/centos  
  2. Using default tag: latest  
  3. Trying to pull repository 10.211.55.30:5000/centos ...  
  4. latest: Pulling from 10.211.55.30:5000/centos  
  5. Digest: sha256:7dfffa13a2addc317ac3bdfbddbd4604ea629decea19c271481e5c45245b7612  
  6. Status: Downloaded newer image for 10.211.55.30:5000/centos:latest  

文章标签:  docker
个人分类:  kubernetes
  • subendong
    subendong 2018-05-07 17:57:45 #2楼
    "争取把这边文章弄成可用性比较高的文章"里面有错别字 "dockere版本:1.10.3"有错别字 目前开始看就碰到这么多问题。
  • lowwwwwww
    lowwwwwwwb 2017-08-07 14:11:34 #1楼
    非常详细 棒
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值