Docker 入门私人笔记(十二)构建 Docker 私有仓库之 Docker 官方的仓库

两种常用的镜像仓库:

  • Docker 官方的仓库 registry
  • VMware 的开源仓库 harbor(内置了 registry

Docker 官方的镜像仓库是部署在容器中的基于 http 协议的应用。
部署和使用方式如下:

Docker 官方的镜像仓库部署在 registry 容器中,查找并下载容器镜像:

[root@k8s-master /data]# docker search registry
NAME                                 DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
registry                             The Docker Registry 2.0 implementation for s…   2725                [OK]                
distribution/registry                WARNING: NOT the registry official image!!! …   58                                      [OK]
stefanscherer/registry-windows       Containerized docker registry for Windows Se…   27                                      
budry/registry-arm                   Docker registry build for Raspberry PI 2 and…   18                                      

第一个标记 OFFICIAL 的镜像即是,下载下来:

[root@k8s-master /data]# docker pull registry
Using default tag: latest
latest: Pulling from library/registry
c87736221ed0: Pull complete 
1cc8e0bb44df: Pull complete 
54d33bcb37f5: Pull complete 
e8afc091c171: Pull complete 
b4541f6d3db6: Pull complete 
Digest: sha256:8004747f1e8cd820a148fb7499d71a76d45ff66bac6a29129bfdbfdc0154d146
Status: Downloaded newer image for registry:latest
docker.io/library/registry:latest
[root@k8s-master /data]# 
[root@k8s-master /data]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
registry            latest              f32a97de94e1        7 months ago        25.8MB

使用这个镜像启动一个容器:

[root@k8s-master /data]# docker run -d --name docker-registry -p 5000:5000 registry
c9ab1fae9c2a3dc8da48cbce5ec3cd0e9e8f227af6aa1395448db88a17cbc7bb
[root@k8s-master /data]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                          NAMES
c9ab1fae9c2a        registry            "/entrypoint.sh /etc…"   8 seconds ago       Up 5 seconds        0.0.0.0:5000->5000/tcp                         docker-registry

创建配置文件 daemon.json(该文件默认不存在),添加私有仓库地址:

vim /etc/docker/daemon.json

配置如下:

{
"registry-mirrors":["https://tdimi5ql.mirror.aliyuncs.com"],
"insecure-registries":["http://192.168.100.151:5000"]
}

重启 docker 服务:

systemctl daemon-reload
systemctl restart docker

查看发现重启服务后仓库(容器 registry)并没有自动启动,需要手动启动:

docker start docker-registry

给想要提交到仓库的镜像打个标签:

[root@k8s-master /data]# docker tag app/jenkins 192.168.100.151:5000/app/jenkins
[root@k8s-master /data]# docker images
REPOSITORY                         TAG                 IMAGE ID            CREATED             SIZE
192.168.100.151:5000/app/jenkins   latest              83644f08fe7a        6 hours ago         660MB
app/jenkins                        latest              83644f08fe7a        6 hours ago         660MB

实操发现,打标签的效果就是在镜像列表中多了一个存储库 REPOSITORY 或者说镜像名称不相同的的镜像,两个镜像的 id 完全一样,说明是同一个镜像

接下来提交镜像到远程镜像仓库:

[root@k8s-master /data]# docker push 192.168.100.151:5000/app/jenkins
The push refers to repository [192.168.100.151:5000/app/jenkins]
559f4ae0e0c6: Pushed 
fd685e9a5043: Pushed 
cfdb476660b4: Pushed 
318ee23a3d4a: Pushed 
8cb2f467d731: Pushed 
2cfb7a3b4cb2: Pushed 
8fd6d61d388e: Pushed 
aeb2f97d8c7a: Pushed 
fcbb61c1c668: Pushed 
b55cd0cf7e45: Pushed 
917d7f6fc72d: Pushed 
b4a4159a6c4a: Pushed 
8bf46d2e80f6: Pushed 
877b494a9f30: Pushed 
latest: digest: sha256:fc58c06b0ff6ab1cbe895aa48ab8921e7fac1629aabba442fc9e3f4a7d818561 size: 3247

接下来验证是否能够从远程仓库下载镜像。

先将已经提交到仓库的本地镜像删除。注意此时删除应该使用镜像名。如果使用 id 进行删除会报错,因为同一个 id 对应了两个镜像名(或者存在两个不同标签的镜像指向了同一个 id):

[root@k8s-master /data]# docker rmi 83644f08fe7a
Error response from daemon: conflict: unable to delete 83644f08fe7a (must be forced) - image is referenced in multiple repositories

正确操作:
第一次删除应该使用镜像名(REPOSITORY),此时可以看到该返回的提示是取消了标签,并未真正的删除该镜像。原因上面刚刚说过,是因为还有另一个标签指向了该镜像 id:

[root@k8s-master /etc/docker]# docker rmi app/jenkins
Untagged: app/jenkins:latest

第二次删除就可以使用 id 了,当然使用镜像名也是可以的,此时就真正的删除了镜像:

[root@k8s-master /data]# docker rmi 192.168.100.151:5000/app/jenkins
Untagged: 192.168.100.151:5000/app/jenkins:latest
Untagged: 192.168.100.151:5000/app/jenkins@sha256:fc58c06b0ff6ab1cbe895aa48ab8921e7fac1629aabba442fc9e3f4a7d818561
Deleted: sha256:83644f08fe7a5b137269dcde4132bd1ddd3cebb785cdc63ca018d8b88c33847d
Deleted: sha256:50f47b580b31fb1ec8b2e36ed19230d7cc5518ea26559e2364630b3e3b202864
Deleted: sha256:50eea350fc0f273c8f900deb83141d965ce8289eba7c3840795261e47d3d4323
Deleted: sha256:89602543224d192f0d224755a8b5c63ff9c3928ab3495d680c218cd76cddf48a
Deleted: sha256:cbdbf0f481551f918ad0a235f0ecf6897022493b759fc9ff564c2b33db7dfede

尝试从仓库拉取刚刚删除掉的镜像并查看:

[root@k8s-master /data]# docker pull 192.168.100.151:5000/app/jenkins
Using default tag: latest
latest: Pulling from app/jenkins
d8d02d457314: Already exists 
5ae2c4acbc0f: Already exists 
9d30c820b1f2: Already exists 
5c50e57b98c7: Already exists 
eebefd700a88: Already exists 
c0a6323f718e: Already exists 
07fd7191c06d: Already exists 
146c19a74bc8: Already exists 
c015e8b53603: Already exists 
cf7384c89894: Already exists 
da96a327dec8: Already exists 
b79fb209b1a1: Already exists 
9dac0f3f7b2b: Already exists 
8478f3dea98b: Pull complete 
Digest: sha256:fc58c06b0ff6ab1cbe895aa48ab8921e7fac1629aabba442fc9e3f4a7d818561
Status: Downloaded newer image for 192.168.100.151:5000/app/jenkins:latest
[root@k8s-master /data]# 
[root@k8s-master /data]# 
[root@k8s-master /data]# docker images
REPOSITORY                         TAG                 IMAGE ID            CREATED             SIZE
192.168.100.151:5000/app/jenkins   latest              83644f08fe7a        7 hours ago         660MB

你可能感兴趣的:(Docker 入门私人笔记(十二)构建 Docker 私有仓库之 Docker 官方的仓库)