扩展1-docker私有仓库的创建和使用

本示例主要通过docker-registry工具实现本地镜像仓库搭建和镜像上传下载。

—学习笔记,方便后期查阅—

1 下载运行registry镜像

#运行如下命令,从镜像源下载registry镜像
[root@bogon docker]# docker run -d -p 5000:5000 --restart=always --name registry registry
#指定-d选项,镜像在后台运行
#指定-p 5000:5000选项,指定宿主机和镜像之间端口流量转发,实际是在iptables里增加一条策略
#指定--restart=always选项,总是启动
#指定--name registry选项,镜像启动后命名为registry
#registry 镜像名称,也可以换成镜像ID

2 镜像服务器本地上传镜像至镜像仓库

从registry镜像仓库宿主机上将镜像上传至registry镜像仓库,示例如下:

1 查看宿主机本地镜像列表
[root@bogon ~]# docker image ls
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
nginx        latest    b692a91e4e15   4 days ago      142MB
httpd        latest    f2a976f932ec   4 days ago      145MB
registry     latest    d1fe2eaf6101   2 weeks ago     24.1MB
centos       latest    5d0da3dc9764   10 months ago   231MB
2 以httpd镜像为例,先为该镜像增加一个tag

[root@bogon ~]# docker tag httpd:latest 127.0.0.1:5000/httpd:latest
#docker tag 创建一个标签tag
#作用镜像名和版本  httpd:latest
#tag名称  127.0.0.1:5000/httpd:latest

#通过image ID可以查看,增加tag后,127.0.0.1:5000/httpd和httpd实际是同一个镜像
[root@bogon ~]# docker image ls
REPOSITORY             TAG       IMAGE ID       CREATED         SIZE
nginx                  latest    b692a91e4e15   4 days ago      142MB
127.0.0.1:5000/httpd   latest    f2a976f932ec   4 days ago      145MB
httpd                  latest    f2a976f932ec   4 days ago      145MB
registry               latest    d1fe2eaf6101   2 weeks ago     24.1MB
centos                 latest    5d0da3dc9764   10 months ago   231MB
3 上传127.0.0.1:5000/httpd镜像
从registry服务器本地上传镜像
[root@bogon home]# docker push 127.0.0.1:5000/httpd
Using default tag: latest
The push refers to repository [127.0.0.1:5000/httpd]
0c2dead5c030: Pushed 
54fa52c69e00: Pushed 
28a53545632f: Pushed 
eea65516ea3b: Pushed 
92a4e8a3140f: Pushed 
latest: digest: sha256:98778663b10c3952e9d7dd8a10e1ca2a8ce31f11b5f0ff9d7b3b36ddb8201db8 size: 1366
3.1 从其他服务器向registry服务器上传镜像,
示例1的远端服务器使用的是操作系统自带的podman工具。
首先还是像第2步一样,示例中将远端服务器上的centos镜像做一个tag,命名为192.168.236.145:5000/centos
[root@localhost ~]# podman tag quay.io/centos/centos:latest 192.168.236.145:5000/centos:latest
[root@localhost ~]# podman image ls
REPOSITORY                   TAG         IMAGE ID      CREATED        SIZE
192.168.236.145:5000/centos  latest      300e315adb2f  20 months ago  217 MB

然后通过push上传
[root@localhost ~]# podman push 192.168.236.145:5000/centos:latest
Getting image source signatures
Error: trying to reuse blob sha256:2653d992f4ef2bfd27f94db643815aa567240c37732cae1405ad1c1309ee9859 at destination: pinging container registry 192.168.236.145:5000: Get "https://192.168.236.145:5000/v2/": http: server gave HTTP response to HTTPS client
#此处会报一个错误,大意是不支持http,需要使用HTTPS,需要在/etc/containers/registries.conf中增加如下配置:
(注:podman版本是4.0)
[[registry]]
prefix = "192.168.236.145:5000"  #prefix不指定的话,默认与location一样。
location = "192.168.236.145:5000"
insecure = true
(以下是podman3.0及以前的版本,未测试过)
[registries.search]
registries = ['192.168.236.145:5000']
[registries.insecure]
registries = ['192.168.236.145:5000']

添加完registry参数配置后,重新执行podman push 192.168.236.145:5000/centos:latest
[root@localhost ~]# podman push 192.168.236.145:5000/centos:latest
Getting image source signatures
Copying blob 2653d992f4ef done  
Copying config 300e315adb done  
Writing manifest to image destination
Storing signatures
3.2 从其他服务器向registry服务器上传镜像,
示例2的远端服务器使用的是docker工具。
其他步骤与3.1podman的步骤一样,不同的是需要在/etc/docker/目录下增加daemon.json文件,增加
[root@localhost docker]# cat daemon.json 
{
  "insecure-registries": [
    "192.168.236.145:5000"
  ]
}
4 本地上传镜像后查看
[root@bogon ~]# curl -XGET 127.0.0.1:5000/v2/_catalog            
{"repositories":["httpd","nginx"]}

以httpd镜像为例,查看httpd镜像的版本
[root@bogon ~]# curl -XGET 127.0.0.1:5000/v2/httpd/tags/list     
{"name":"httpd","tags":["latest"]}

4.1 从远端服务器上传镜像后查看
[root@localhost docker]# curl 127.0.0.1:5000/v2/_catalog
{"repositories":["centos","httpd","nginx"]}
查看从远端上传的centos镜像
[root@localhost docker]# curl 127.0.0.1:5000/v2/centos/tags/list
{"name":"centos","tags":["latest"]}
5 下载镜像
先在远端服务器上查看镜像列表
[greg@localhost ~]$ podman image ls
REPOSITORY               TAG         IMAGE ID      CREATED      SIZE
docker.io/library/httpd  latest      444f7df01ce9  3 weeks ago  149 MB

从本地镜像仓库下载镜像(此处使用的是非root用户)
[greg@localhost ~]$ podman pull 192.168.236.145:5000/centos:latest
Trying to pull 192.168.236.145:5000/centos:latest...
Getting image source signatures
Copying blob a83a1b2d3c56 done  
Copying config 300e315adb done  
Writing manifest to image destination
Storing signatures
300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55
可以看到centos镜像已经被下载下来
[greg@localhost ~]$ podman image ls
REPOSITORY                   TAG         IMAGE ID      CREATED        SIZE
docker.io/library/httpd      latest      444f7df01ce9  3 weeks ago    149 MB
192.168.236.145:5000/centos  latest      300e315adb2f  20 months ago  217 MB
6 运行镜像
#运行容器
[greg@localhost ~]$ podman run -itd --name centos-latest 192.168.236.145:5000/centos
189c9b98896fd5deb0ae2fa57bb3c6f223efe08ad949f1d8758b4be4594261f8
#查看运行的容器
[greg@localhost ~]$ podman ps -a
CONTAINER ID  IMAGE                               COMMAND     CREATED         STATUS             PORTS       NAMES
189c9b98896f  192.168.236.145:5000/centos:latest  /bin/bash   26 seconds ago  Up 26 seconds ago              centos-latest
#登录到容器中执行ls命令
[greg@localhost ~]$ podman exec -it 189c9b98896f /bin/bash
[root@189c9b98896f /]# ls
bin  etc   lib    lost+found  mnt  proc  run   srv  tmp  var
dev  home  lib64  media       opt  root  sbin  sys  usr