ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Docker学习笔记之创建私有仓库

2022-05-19 01:33:53  阅读:190  来源: 互联网

标签:5000 私有 笔记 127.0 registry 镜像 Docker root docker


概述

Docker的使用越来越多,在实际应用中,我们可能不止一台服务器,也可能不只是在同一个云上,那么面对同一个镜像,要部署到不同的云,不同的服务器,有什么便捷的方式呢?当然,有同学可能会说使用官方仓库的。这个固然没有错误,但如果是涉及比较保密的镜像,或者考虑到速度的原因,我们就需要考虑构建自己的仓库了。

获取仓库镜像

使用pull从官方仓库下载registry(私有仓库)镜像文件,没指定版本,则默认下载最新版本,下载完成后,通过images 可能查看到镜像下载成功。

[root@localhost ~]# docker pull registry
......下载过程,省略.......
[root@localhost ~]# docker images registry
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
registry     latest    2e200967d166   6 weeks ago   24.2MB


运行仓库镜像

1、使用 run 命令构建并运行私有仓库的容器(这里将宿主机的5000端口【第一个】,绑定私有仓库的5000端口【第二个】);
2、然后 使用 ps 命令查看运行情呢况;
3、最后使用 curl 获取端口的镜像信息;

[root@localhost ~]# docker run -d -p 5000:5000 --name registry registry
5d55707c01593bc888927f8ebed37f13b56f517b51283b85366cf844ed110b02
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE      COMMAND                  CREATED         STATUS         PORTS                                       NAMES
5d55707c0159   registry   "/entrypoint.sh /etc…"   2 seconds ago   Up 2 seconds   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   registry
[root@localhost ~]# curl -XGET http://127.0.0.1:5000/v2/_catalog
{"repositories":[]}

这里需要强调一下,registry 默认使用的是5000的端口,如果这个端口有被其他镜像使用的,则可以通过 参数 REGISTRY_HTTP_ADDR 进修改,如果既没有修改端口,也没有绑定私有仓库的默认5000端口,则会连接私有仓库失败


向仓库推送镜像

现在仓库已经正常运行了,接下来我们测试一下向仓库推送镜像,在推送镜像之前,我们要把本地的测试镜像的标签修改为私有仓库对应的标签,否则无法推送,操作如下:
1、使用images ubuntu 列出ubuntu的镜像;
2、使用tag 将本地的ubuntu镜像加上 127.0.0.1:5000/ubuntu:18.04 标签;
3、使用images 列出本地镜像,可以看到已新增了一条 127.0.0.1:5000/ubuntu 的镜像记录;
4、设置私有仓库地址 vim /etc/docker/daemon.json ,内容参数为:{"insecure-registries":["172.16.3.168:5000"]} ,172.16.3.168 是宿主机IP ;
4、使用push 将镜像推送到 127.0.0.1:5000 的仓库;
5、使用curl 查看仓库接口,结果输出 {"repositories":["ubuntu"]} 证明镜像已经被推送到了私有仓库了;

[root@localhost ~]# docker images ubuntu
REPOSITORY                             TAG            IMAGE ID       CREATED        SIZE
ubuntu                                 18.04          f5cbed4244ba   6 weeks ago    63.2MB
[root@localhost ~]# docker tag ubuntu:18.04 127.0.0.1:5000/ubuntu:18.04
[root@localhost ~]# docker images
REPOSITORY                             TAG            IMAGE ID       CREATED        SIZE
127.0.0.1:5000/ubuntu                  18.04          f5cbed4244ba   6 weeks ago    63.2MB
ubuntu                                 18.04          f5cbed4244ba   6 weeks ago    63.2MB
[root@localhost ~]# docker push 127.0.0.1:5000/ubuntu:18.04
The push refers to repository [127.0.0.1:5000/ubuntu]
95c443da13bf: Pushed 
18.04: digest: sha256:512274f1739676880585e70eea6a883db7b6d92841b02647b6c92b478356572c size: 529
[root@localhost ~]# curl -XGET http://127.0.0.1:5000/v2/_catalog
{"repositories":["ubuntu"]}


从仓库拉取镜像

从上面的步骤中,我们已经完成将本地的镜像推送到我们自己构建的私有仓库,那么反过来,又是否可以从私有仓库中拉取镜像到本地呢?答案是可以的。在拉取镜像之前,为了方便分辨,我们需要将本地存在的对应的镜像先删除,然后再拉取,操作如下:
1、使用 rmi 删除本地镜像 127.0.1.1:5000/ubunt:18.04 ;
2、然后使用 images 查看本地镜像,可以看到列表中已经不存在镜像 127.0.1.1:5000/ubunt:18.04 了;
3、拉着使用 pull 从私有仓库 127.0.1.1:5000 中拉取镜像 ubunt:18.04 到本地;
4、最后使用 images 查看本地镜像,此时,列表中出现了我们刚刚拉取的 127.0.1.1:5000/ubunt 镜像;

[root@localhost ~]# docker rmi 127.0.1.1:5000/ubunt:18.04
Untagged: 127.0.1.1:5000/ubunt:18.04
Untagged: 127.0.1.1:5000/ubunt@sha256:512274f1739676880585e70eea6a883db7b6d92841b02647b6c92b478356572c
[root@localhost ~]# docker images
REPOSITORY                             TAG            IMAGE ID       CREATED        SIZE
ubuntu                                 18.04          f5cbed4244ba   6 weeks ago    63.2MB
[root@localhost ~]# docker pull 127.0.1.1:5000/ubunt:18.04
18.04: Pulling from ubunt
Digest: sha256:512274f1739676880585e70eea6a883db7b6d92841b02647b6c92b478356572c
Status: Downloaded newer image for 127.0.1.1:5000/ubunt:18.04
127.0.1.1:5000/ubunt:18.04
[root@localhost ~]# docker images
REPOSITORY                             TAG            IMAGE ID       CREATED        SIZE
127.0.1.1:5000/ubunt                   18.04          f5cbed4244ba   6 weeks ago    63.2MB
ubuntu                                 18.04          f5cbed4244ba   6 weeks ago    63.2MB


用户认证

前面我们在执行操作的过程中,其实是没有用户认证的,但在实际的使用中,这相当于裸奔。那要怎么认证呢?
1、首先我们要创建存储用户认证文件的目录:

[root@localhost ~]# mkdir /etc/docker/registry
[root@localhost ~]# mkdir /etc/docker/registry/auth

2、然后使用 htpasswd 生成用户名和密码到认证的文件:

[root@localhost auth]# docker run --entrypoint htpasswd registry -Bbn Jack 123456 > /etc/docker/registry/auth/htpasswd
docker: Error response from daemon: failed to create shim: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "htpasswd": executable file not found in $PATH: unknown.

这个时候,问题来了,发生了上面的错误,似乎无法使用 htpasswd 创建认证的文件,经过一番搜索和尝试之后,发现,这个是因为 registry 版本的原因,导致 htpasswd 无法执行,我们只需要使用 registry:2.6.2 的版本来去执行就可以了。
如果要添加多个用户,可以多次执行,变换用户名和密码即可。

[root@localhost ~]# docker run --entrypoint htpasswd registry:2.6.2 -Bbn Jack 123456 > /etc/docker/registry/auth/htpasswd
Unable to find image 'registry:2.6.2' locally
2.6.2: Pulling from library/registry
486039affc0a: Pulling fs layer
ba51a3b098e6: Pulling fs layer
470e22cd431a: Pulling fs layer
1048a0cdabb0: Pulling fs layer
ca5aa9d06321: Pulling fs layer
1048a0cdabb0: Waiting
ca5aa9d06321: Waiting
486039affc0a: Verifying Checksum
486039affc0a: Download complete
486039affc0a: Pull complete
ba51a3b098e6: Verifying Checksum
ba51a3b098e6: Download complete
ba51a3b098e6: Pull complete
470e22cd431a: Verifying Checksum
470e22cd431a: Download complete
470e22cd431a: Pull complete
ca5aa9d06321: Verifying Checksum
ca5aa9d06321: Download complete
1048a0cdabb0: Verifying Checksum
1048a0cdabb0: Download complete
1048a0cdabb0: Pull complete
ca5aa9d06321: Pull complete
Digest: sha256:c4bdca23bab136d5b9ce7c06895ba54892ae6db0ebfc3a2f1ac413a470b17e47
Status: Downloaded newer image for registry:2.6.2

使用vim命令查看认证的文件

[root@localhost ~] vim /etc/docker/registry/auth/htpasswd

可以查看到下面的内容

Jack:$2y$05$ct69t03XZOglFZ.Nmpb1Wev9ofnhKUY2HhRAV4rCtEYAbQzOXk.QG

下面我们就来验证一下登录:

[root@localhost ~]# docker login 127.0.0.1:5000 -u Jack -p 123456
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

看到上面的信息,证明密码设置成功了。而退出,则使用:

[root@localhost ~]# docker logout 127.0.0.1:5000
Removing login credentials for 127.0.0.1:5000

总结

至此,我们完成了私有仓库的构建,registry 还有其他的信息,有兴趣的同学,可以查看 官网提供的文档;另外,除了可以用官方提供的Registry构建私有仓库外,我们还可以第三方的 Harbor 搭建私有仓库,应该说 Harbor 要比官方提供的 Registry 要好用些(虽然官方的Registry也可以启用界面)。关于 Harbor 我们在后面的文章中有机会将给大家介绍。

标签:5000,私有,笔记,127.0,registry,镜像,Docker,root,docker
来源: https://www.cnblogs.com/jlonghe/p/docker-private-repository.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有