美文网首页我爱编程
docker私有仓库搭建并且配置仓库认证

docker私有仓库搭建并且配置仓库认证

作者: 霂蔺 | 来源:发表于2016-12-22 18:28 被阅读5428次
什么是Docker Registry:

Docker Registry由三个部分组成:index,registry,registry client。
可以把Index认为是负责登录、负责认证、负责存储镜像信息和负责对外显示的外部实现,而registry则是负责存储镜像的内部实现,而Registry Client则是docker客户端。

私有仓库搭建

1.安装Docker Registry,Docker版本需要1.6以上:
安装方法详见我的另一文章centos7 docker安装,我这边已经安装好了。

docker --version
Docker version 1.12.5, build 7392c3b

2.安装Registry,并启动(启动方法具体见官方的registry):

docker pull registry

默认情况下,会将仓库存放于容器内的/var/lib/registry(官网Dockerfile中查看)目录下,这样如果容器被删除,则存放于容器中的镜像也会丢失,所以我们一般情况下会指定本地一个目录挂载到容器内的/var/lib/registry下,如下:
docker run -d -ti --restart always --name docker-hub -p 5000:5000 -v /docker-hub/registry:/var/lib/registry registry

3.可以用docker images 查看镜像,docker ps -a查看容器启动情况。

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
registry            latest              c9bd19d022f6        9 weeks ago         33.27 MB
hello-world         latest              c54a2cc56cbb        5 months ago        1.848 kB
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
7411a02041aa        registry            "/entrypoint.sh /etc/"   3 minutes ago       Up 3 minutes        0.0.0.0:5000->5000/tcp   docker-hub

4.访问私有仓库,由于刚启动还没有提交镜像,所以这里显示为空。

[root@localhost ~]# curl -XGET 127.0.0.1:5000/v2/_catalog
{"repositories":[]}
  • 查看镜像版本列表

curl -XGET 127.0.0.1:5000/v2/image_name/tags/list

5.push镜像到仓库;

[root@localhost ~]# docker tag hello-world 127.0.0.1:5000/helloworld
[root@localhost ~]# docker images
REPOSITORY                  TAG                 IMAGE ID            CREATED             SIZE
registry                    latest              c9bd19d022f6        9 weeks ago         33.27 MB
127.0.0.1:5000/helloworld   latest              c54a2cc56cbb        5 months ago        1.848 kB
hello-world                 latest              c54a2cc56cbb        5 months ago        1.848 kB
[root@localhost ~]# docker push 127.0.0.1:5000/helloworld 
The push refers to a repository [127.0.0.1:5000/helloworld]
a02596fdd012: Pushed 
latest: digest: sha256:a18ed77532f6d6781500db650194e0f9396ba5f05f8b50d4046b294ae5f83aa4 size: 524
[root@localhost ~]# curl 127.0.0.1:5000/v2/_catalog
{"repositories":["helloworld"]}
[root@localhost ~]# 

我上传了一个测试的helloworld镜像,这边再查询的就可以看见了。

如果出现无法从私有仓库pull镜像或无法push到私有仓库的问题,如下报错情况。

[root@localhost ~]# docker pull 192.168.1.163:5000/hellworld
Using default tag: latest
Error response from daemon: Get https://192.168.1.163:5000/v1/_ping: http: server gave HTTP response to HTTPS client
[root@localhost ~]# 

这是因为我们启动的registry服务不是安全可信赖的。这是我们需要修改docker的目录/etc/docker/下创建 daemon.json 文件,添加下面的内容:

$ tee /etc/docker/daemon.json << EOF
{ "insecure-registries":["192.168.1.163:5000"] }
EOF

保存后重启docker。

systemctl restart docker.service

然后再PULL即可。

私有仓库认证

私有仓库搭建以后其他所有客户端均可以push、pull,docker官方提供认证方法对docker仓库进行权限保护。
我这只用的用户权限限制,官网配置是TLS和用户权限验证一起使用。

1.删除原启动的仓库容器

docker stop docker-hub
docker rm -f docker-hub

2.创建存放密码账号的文件

mkdir -p /docker-hub/auth
docker run --entrypoint htpasswd registry -Bbn testuser testpassword > auth/htpasswd

3.重新启动docker-hub容器

docker run -d -p 5000:5000 --restart=always --name docker-hub \
  -v /docker-hub/registry:/var/lib/registry \
  -v /docker-hub/auth:/auth \
  -e "REGISTRY_AUTH=htpasswd" \
  -e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" \
  -e REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd \
  registry

4.现在客户端再pull、push就会提示报错,无法提交,需要登录私有仓库。

登录
docker login -u testuser -p testpassword 192.168.1.163:5000
退出
docker logout 192.168.1.163:5000

认证以后无法直接在服务器查看 curl 127.0.0.1:5000/v2/_catalog 仓库的镜像,会出现报错,但是可以用浏览器访问。

参考网址:
https://docs.docker.com/registry/deploying/#/running-a-domain-registry

相关文章

网友评论

    本文标题:docker私有仓库搭建并且配置仓库认证

    本文链接:https://www.haomeiwen.com/subject/kalhvttx.html