官方的Docker hub是一个用于管理公共镜像的好地方,咱们能够在上面找到咱们想要的镜像,也能够把咱们本身的镜像推送上去。可是,有时候,咱们的使用场景须要咱们拥有一个私有的镜像仓库用于管理咱们本身的镜像。这个能够经过开源软件Registry来达成目的。node
Registry在github上有两份代码:老代码库和新代码库。老代码是采用python编写的,存在pull和push的性能问题,出到0.9.1版本以后就标志为deprecated,再也不继续开发。从2.0版本开始就到在新代码库进行开发,新代码库是采用go语言编写,修改了镜像id的生成算法、registry上镜像的保存结构,大大优化了pull和push镜像的效率。python
官方在Docker hub上提供了registry的镜像(详情),咱们能够直接使用该registry镜像来构建一个容器,搭建咱们本身的私有仓库服务。Tag为latest的registry镜像是0.9.1版本的,咱们直接采用2.1.1版本。git
docker pull registry:2.1.1
复制代码
[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目录了github
docker tag centos:7.5.1804 192.168.44.201:5000/centos:7.5.1804
复制代码
docker push 192.168.44.201:5000/centos:7.5.1804
复制代码
由于Docker从1.3.X以后,与docker registry交互默认使用的是https,然而此处搭建的私有仓库只提供http服务,因此当与私有仓库交互时就会报下面的错误: 算法
为了解决这个问题须要在启动docker server时增长启动参数为默认使用http访问。修改docker启动配置文件: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
复制代码
以后重启dockervim
systemctl daemon-reload
systemctl start docker
复制代码
以后再从新push,成功 centos
[root@node1 bin]# curl http://192.168.44.201:5000/v2/_catalog
{"repositories":["centos"]}
[root@node1 bin]#
复制代码
[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]#
复制代码
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
复制代码
[root@node1 bin]# export REGISTRY_DATA_DIR='/opt/data/registry/docker/registry/v2/'
复制代码
由于delete_docker_registry_image中须要用到REFISTRY_DATA_DIR这个环境变量浏览器
[root@node1 bin]# delete_docker_registry_image --image centos:7.5.1804.2
复制代码
删除成功!!!bash