docker私有仓库的创建

由于部分图片的问题,附上有道云笔记中的链接:http://note.youdao.com/noteshare?id=973cd2cd200dca3769bc9cd2a6b9b746&sub=71498BEACB1F42DE99EDF4B403145BC7

Docker私有仓库的安装

1.Registry

官方的Docker hub是一个用于管理公共镜像的好地方,我们可以在上面找到我们想要的镜像,也可以把我们自己的镜像推送上去。但是,有时候,我们的使用场景需要我们拥有一个私有的镜像仓库用于管理我们自己的镜像。这个可以通过开源软件Registry来达成目的。

Registry在github上有两份代码:老代码库和新代码库。老代码是采用python编写的,存在pull和push的性能问题,出到0.9.1版本之后就标志为deprecated,不再继续开发。从2.0版本开始就到在新代码库进行开发,新代码库是采用go语言编写,修改了镜像id的生成算法、registry上镜像的保存结构,大大优化了pull和push镜像的效率。

官方在Docker hub上提供了registry的镜像(详情),我们可以直接使用该registry镜像来构建一个容器,搭建我们自己的私有仓库服务。Tag为latest的registry镜像是0.9.1版本的,我们直接采用2.1.1版本。

2.Registry部署

2.1 获取registry镜像

docker pull registry:2.1.1

2.2 启动registry镜像

[root@node1 bin]# docker run -d -v /opt/data/registry:/var/lib/registry -p 5000:5000 --restart=always --name registry registry:2.1.1

默认情况下,registry会将上传的镜像保存在容器的/var/lib/registry,我们将主机的/opt/data/registry目录挂载到该目录,即可实现将镜像保存到主机的/opt/data/registry目录了

2.3查看启动情况

2.4 启动register服务之后,通过浏览器访问:192.168.44.201:5000/v2

浏览器访问

3.上传image到私有仓库

3.1 打tag(centos:7.5.1804是本地已经有的image)

docker tag centos:7.5.1804 192.168.44.201:5000/centos:7.5.1804

3.2 push

docker push 192.168.44.201:5000/centos:7.5.1804

因为Docker从1.3.X之后,与docker registry交互默认使用的是https,然而此处搭建的私有仓库只提供http服务,所以当与私有仓库交互时就会报下面的错误:

为了解决这个问题需要在启动docker server时增加启动参数为默认使用http访问。修改docker启动配置文件:

[root@node1 bin]# vim /usr/lib/systemd/system/docker.service

[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target

[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
#ExecStart=/usr/bin/dockerd --insecure-registry 192.168.44.201:5000 ##加上这一句
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
# Uncomment TasksMax if your systemd version supports it.
# Only systemd 226 and above support this version.
#TasksMax=infinity
TimeoutStartSec=0
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
# kill only the docker process, not all processes in the cgroup
KillMode=process
# restart the docker process if it exits prematurely
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s

[Install]
WantedBy=multi-user.target

之后重启docker

systemctl daemon-reload
systemctl start docker 

之后再重新push,成功

扫描二维码关注公众号,回复: 8462735 查看本文章

3.3 浏览器查看

4.查看相关私有仓库的信息

4.1 私有仓库的images

[root@node1 bin]# curl http://192.168.44.201:5000/v2/_catalog
{"repositories":["centos"]}
[root@node1 bin]# 

4.2 查看对应image的tag信息

[root@node1 bin]# curl http://192.168.44.201:5000/v2/centos/tags/list
{"name":"centos","tags":["7.5.1804","7.5.1804.2"]}
[root@node1 bin]# 

5.删除私有仓库的image

5.1 下载资源

curl https://raw.githubusercontent.com/burnettk/delete-docker-registry-image/master/delete_docker_registry_image.py | sudo tee /usr/local/bin/delete_docker_registry_image >/dev/null
chmod +x  /usr/local/bin/delete_docker_registry_image

5.2 设置相关环境变量

[root@node1 bin]# export REGISTRY_DATA_DIR='/opt/data/registry/docker/registry/v2/'

因为delete_docker_registry_image中需要用到REFISTRY_DATA_DIR这个环境变量

5.3 删除image

[root@node1 bin]# delete_docker_registry_image  --image centos:7.5.1804.2

删除成功!!!

猜你喜欢

转载自www.cnblogs.com/zhangjxblog/p/12168306.html