为什么要使用私有仓库

  • 在使用Docker一段时间后,往往会发现手头积累了大量的自定义镜像文件,这些文件通过公有仓库进行管理并不方便;另外有时候只是希望在内部用户之间进行分享,不希望暴露出去。这种情况下,就有必要搭建一个本地私有镜像仓库。

关于Docker Registry

  • Docker Registry工具目前最新为2.0系列版本,这一版本与一些类库、工具一起被打包为负责容器内容分发的工具集:Docker Distribution。目前其核心的功能组件仍为负责镜像仓库的管理。新版本的Registry基于Golang进行了重构,提供更好的性能和扩展性,并且支持Docker 1.6+的API,非常适合用来构建私有的镜像注册服务器。官方仓库中也提供了Registry的镜像,因此用户可以通过容器运行和源码安装两种方式来使用Registry。
  • 使用Docker Registry的两种主要方式:通过容器方式运行和通过本地安装运行并注册为系统服务,以及添加Nginx反向代理,添加用户认证功能。Docker Registry配置文件中各个选项的含义和使用。如何通过脚本来实现对镜像的批量管理,以及使用Registry的通知系统来支持更多应用场景。

基于容器搭建registry私有仓库

1.下载镜像

[root@docker ~]# docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE
[root@docker ~]# docker pull  registry:latset
latset: Pulling from library/registry
[DEPRECATION NOTICE] Docker Image Format v1, and Docker Image manifest version 2, schema 1 support will be removed in an upcoming release. Suggest the author of docker.io/library/registry:2.1 to upgrade the image to the OCI Format, or Docker Image manifest v2, schema 2. More information at https://docs.docker.com/go/deprecated-image-specs/
9943fffae777: Pull complete 
fb15e825cb68: Pull complete 
b9583a207297: Pull complete 
a3ed95caeb02: Pull complete 
87fee1c528e9: Pull complete 
829473b2393f: Pull complete 
2c1adb4b358c: Pull complete 
Digest: sha256:e641943a78a8f634c16ad69f5c9d779f470b147865c2121d89c52ea0da6fc1bd
Status: Downloaded newer image for registry:latset
docker.io/library/registry:latset
[root@docker ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
registry     latset       52bb991b482e   8 years ago   220MB
[root@docker ~]# 

2. 启动镜像

[root@docker ~]# docker run -d -p 5000:5000 --restart=always --name registry -v /opt/data/registry:/var/lib/registry registry:2.1 
6b9d6ee76d00b76a7d0647a7d574d508f525f24f0103e6eedc0d3b734a90e8cc
[root@docker ~]# docker ps
CONTAINER ID   IMAGE          COMMAND                   CREATED         STATUS         PORTS                                       NAMES
6b9d6ee76d00   registry:2.1   "/bin/registry /etc/…"   6 seconds ago   Up 5 seconds   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   registry
[root@docker ~]# netstat -tnlp|grep -w 5000
tcp        0      0 0.0.0.0:5000            0.0.0.0:*               LISTEN      5690/docker-proxy   
tcp6       0      0 :::5000                 :::*                    LISTEN      5696/docker-proxy   
[root@docker ~]# 

3.修改系统配置文件

  • Docker Pushh默认往Docker仓库,未修改一下配置会报错:
    -[root@docker ~]# docker push 192.168.40.21:5000/ubuntu:v1.0 The push refers to repository [192.168.40.21:5000/ubuntu] Get "https://192.168.40.21:5000/v2/": http: server gave HTTP response to HTTPS client

在文件中添加内容 “insecure-registries”: [“192.168.40.21:5000”],
192.168.40.21是我VM虚拟机的IP

[root@docker ~]# vi /etc/docker/daemon.json
[root@docker ~]# cat /etc/docker/daemon.json
{
  "insecure-registries": ["192.168.40.21:5000"],
  "registry-mirrors": ["https://i9t81ora.mirror.aliyuncs.com"]
}
[root@docker ~]# systemctl daemon-reload
[root@docker ~]# systemctl restart docker
[root@docker ~]# 

4.下载ubuntu镜像,修改名称

[root@docker ~]# docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
7b1a6ab2e44d: Pull complete 
Digest: sha256:626ffe58f6e7566e00254b638eb7e0f3b11d4da9675088f4781a50ae288f3322
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest
[root@docker ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
ubuntu       latest    ba6acccedd29   2 years ago   72.8MB
registry     latset       52bb991b482e   8 years ago   220MB
[root@docker ~]# docker ps
CONTAINER ID   IMAGE          COMMAND                   CREATED         STATUS         PORTS                                       NAMES
6b9d6ee76d00   registry:2.1   "/bin/registry /etc/…"   7 minutes ago   Up 7 minutes   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   registry
[root@docker ~]# docker tag ubuntu:latest 192.168.40.21:5000/ubuntu:v1.0
[root@docker ~]# docker images
REPOSITORY                  TAG       IMAGE ID       CREATED       SIZE
192.168.40.21:5000/ubuntu   v1.0      ba6acccedd29   2 years ago   72.8MB
ubuntu                      latest    ba6acccedd29   2 years ago   72.8MB
registry                    latset    52bb991b482e   8 years ago   220MB
[root@docker ~]# 

3.提交镜像

[root@docker ~]# docker push 192.168.40.21:5000/ubuntu:v1
The push refers to repository [192.168.40.21:5000/ubuntu]
9f54eef41275: Pushed 
v1: digest: sha256:7cc0576c7c0ec2384de5cbf245f41567e922aab1b075f3e8ad565f508032df17 size: 529
[root@docker ~]#

4.查看镜像

[root@docker ~]# curl http://192.168.40.21:5000/v2/_catalog
{"repositories":["ubuntu"]}
[root@docker ~]# curl http://192.168.40.21:5000/v2/ubuntu/tags/list
{"name":"ubuntu","tags":["v1"]}
[root@docker ~]# 

本地搭建私有仓库(官方已放弃,旧版本已没法编译成功)

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐