将 Docker 镜像发布到私库

Posted by Vito on May 24, 2022

数据卷

  • 概念: 将宿主机目录挂载到 docker 容器目录中
  • 语法: docker run --privileged=true -v /宿主机绝对路径:/容器内目录[:rw/ro] 镜像名
    • rw 可读可写; ro 限制容器只能读取挂载目录,不能写; 默认 rw
    • 可挂载多个,用多个 -v 参数即可
    • 查看数据卷是否挂载成功: docker inspect 容器ID
    • 继承其他容器的数据卷: docker run --privileged=true --volumes-from 父容器名 镜像名

发布镜像到私库

  • 启动私库服务 registry
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    
    [root@mydockerhost ~]# mkdir -p /docker/registry/
    # 创建私库容器(临时)
    [root@mydockerhost ~]# docker run -d --name registry registry:2.8.1
    # 从容器内复制配置文件到宿主机
    [root@mydockerhost ~]# docker cp registry:/etc/docker/registry/config.yml /home/vito/docker/volume/registry/config.yml
    # 删除容器
    [root@mydockerhost ~]# docker rm -f registry
      
    # 修改配置文件,增加配置 storage.delete.enabled = true ,支持镜像删除
    [root@mydockerhost ~]# vim /home/vito/docker/volume/registry/config.yml
    version: 0.1
    log:
      fields:
        service: registry
    storage:
      delete:
        enabled: true
      cache:
        blobdescriptor: inmemory
      filesystem:
        rootdirectory: /var/lib/registry
    http:
      addr: :5000
      headers:
        X-Content-Type-Options: [nosniff]
    health:
      storagedriver:
        enabled: true
        interval: 10s
        threshold: 3
    # 正式创建私库容器
    [root@mydockerhost ~]# docker run -d -p 5000:5000 --restart=always  \
                           -v /home/vito/docker/volume/registry/myregistry:/var/lib/registry  \
                           -v /home/vito/docker/volume/registry/config.yml:/etc/docker/registry/config.yml  \
                           --name registry registry:2.8.1
    
  • curl 验证私服库上有什么镜像( /v2/_catalog )
    • curl -X GET http://192.168.23.128:5000/v2/_catalog
  • 修改配置文件使私库服务支持 http 推送 修改配置文件,将 Registry 私服地址添加到 Docker 信任列表
    • vim /etc/docker/daemon.json ,在 json 文件中新增 "insecure-registries": ["192.168.23.128:5000"]
    • 重启 docker
  • 将待推送镜像修改为符合私服规范的 Tag (以 alpine:3.15.0、alpine:3.16.0 为例):
    1
    2
    3
    
    # 私库ip:port/<name>:<tag>
    docker tag alpine:3.15.0 192.168.23.128:5000/hczhch/alpine:1.0.1
    docker tag alpine:3.16.0 192.168.23.128:5000/hczhch/alpine:1.0.2
    
  • 推送镜像到私库
    • docker push 192.168.23.128:5000/hczhch/alpine:1.0.1
    • docker push 192.168.23.128:5000/hczhch/alpine:1.0.2
  • curl 再次验证私服库上有什么镜像:
    1
    2
    
    curl -X GET http://192.168.23.128:5000/v2/_catalog
    {"repositories":["hczhch/alpine"]}
    
  • curl 查询私服库上指定镜像的所有 tags ( /v2//tags/list ):
    1
    2
    
    curl -X GET http://192.168.23.128:5000/v2/hczhch/alpine/tags/list
    {"name":"hczhch/alpine","tags":["1.0.1","1.0.2"]}
    
  • 删除本地镜像
    • docker rmi -f alpine:3.15.0 alpine:3.16.0 192.168.23.128:5000/hczhch/alpine:1.0.1 192.168.23.128:5000/hczhch/alpine:1.0.2
  • 将镜像从私库 pull 到本地:
    • docker pull 192.168.23.128:5000/hczhch/alpine:1.0.1
    • docker pull 192.168.23.128:5000/hczhch/alpine:1.0.2
  • 查询本地仓库中的镜像:
    1
    2
    3
    4
    5
    
    [root@mydockerhost ~]# docker images --digests
    REPOSITORY                        TAG             DIGEST                                                                    IMAGE ID       CREATED         SIZE
    192.168.23.128:5000/hczhch/alpine   1.0.2           sha256:4ff3ca91275773af45cb4b0834e12b7eb47d1c18f770a0b151381cd227f4c253   e66264b98777   6 days ago      5.53MB
    192.168.23.128:5000/hczhch/alpine   1.0.1           sha256:e7d88de73db3d3fd9b2d63aa7f447a10fd0220b7cbf39803c803f2af9ba256b3   c059bfaa849c   6 months ago    5.59MB
    registry                          latest          sha256:169211e20e2f2d5d115674681eb79d21a217b296b43374b8e39f97fcf866b375   b8604a3fe854   6 months ago    26.2MB
    
  • 删除私有仓库中的镜像:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    
    # 进入私库容器,并查看仓库大小( du -sch )
    [root@mydockerhost ~]# docker exec -it registry /bin/sh
    / # du -sch /var/lib/registry
    5.4M	/var/lib/registry
    5.4M	total
    / # exit
      
    # 从私库中删除镜像 DELETE /v2/<name>/manifests/<reference>
    [root@mydockerhost ~]# curl -X DELETE http://192.168.23.128:5000/v2/hczhch/alpine/manifests/sha256:e7d88de73db3d3fd9b2d63aa7f447a10fd0220b7cbf39803c803f2af9ba256b3
    # 验证发现镜像已删除
    [root@mydockerhost ~]# curl -X GET http://192.168.23.128:5000/v2/_catalog
    {"repositories":["hczhch/alpine"]}
    [root@mydockerhost ~]# curl -X GET http://192.168.23.128:5000/v2/hczhch/alpine/tags/list
    {"name":"hczhch/alpine","tags":["1.0.2"]}
      
    # 进入私库容器,发现仓库大小未发生变化(只删除了元数据)
    [root@mydockerhost ~]# docker exec -it registry /bin/sh
    / # du -sch /var/lib/registry
    5.4M	/var/lib/registry
    5.4M	total
    # 垃圾回收,删除 blob
    / # registry garbage-collect /etc/docker/registry/config.yml
    hczhch/alpine
    hczhch/alpine: marking manifest sha256:4ff3ca91275773af45cb4b0834e12b7eb47d1c18f770a0b151381cd227f4c253 
    hczhch/alpine: marking blob sha256:e66264b98777e12192600bf9b4d663655c98a090072e1bab49e233d7531d1294
    hczhch/alpine: marking blob sha256:2408cc74d12b6cd092bb8b516ba7d5e290f485d3eb9672efc00f0583730179e8
      
    3 blobs marked, 3 blobs and 0 manifests eligible for deletion
    blob eligible for deletion: sha256:59bf1c3509f33515622619af21ed55bbe26d24913cedbca106468a5fb37a50c3
    INFO[0000] Deleting blob: /docker/registry/v2/blobs/sha256/59/59bf1c3509f33515622619af21ed55bbe26d24913cedbca106468a5fb37a50c3  go.version=go1.11.2 instance.id=a0a0c961-f402-4fe1-aa9d-aa1cf2b0bfb5 service=registry
    blob eligible for deletion: sha256:c059bfaa849c4d8e4aecaeb3a10c2d9b3d85f5165c66ad3a4d937758128c4d18
    INFO[0000] Deleting blob: /docker/registry/v2/blobs/sha256/c0/c059bfaa849c4d8e4aecaeb3a10c2d9b3d85f5165c66ad3a4d937758128c4d18  go.version=go1.11.2 instance.id=a0a0c961-f402-4fe1-aa9d-aa1cf2b0bfb5 service=registry
    blob eligible for deletion: sha256:e7d88de73db3d3fd9b2d63aa7f447a10fd0220b7cbf39803c803f2af9ba256b3
    INFO[0000] Deleting blob: /docker/registry/v2/blobs/sha256/e7/e7d88de73db3d3fd9b2d63aa7f447a10fd0220b7cbf39803c803f2af9ba256b3  go.version=go1.11.2 instance.id=a0a0c961-f402-4fe1-aa9d-aa1cf2b0bfb5 service=registry
    # 再次查看仓库大小,发现数据变小
    / # du -sch /var/lib/registry
    2.7M	/var/lib/registry
    2.7M	total
    / # exit
    [root@mydockerhost ~]#