1.安装docker shell脚本

#!/bin/bash

hub_docker="hub.dy.com"

yum install -y epel-release docker

sed -i "s#--selinux-enabled --log-driver=journald#--selinux-enabled=false --insecure-registry $hub_docker:5000 --log-driver=json-file --storage-driver=overlay2#g"  /etc/sysconfig/docker

# 开机自启
/usr/bin/systemctl daemon-reload
/usr/bin/systemctl enable docker
/usr/bin/systemctl start docker

2.私服的搭建 shell脚本

#!/bin/bash

docker pull registry

[ -d /home/registry ] || mkdir -p /home/registry
cat <<EOF >/home/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
EOF

docker run -itd -v /home/registry/:/var/lib/registry/ -v /home/registry/config.yml:/etc/docker/registry/config.yml -p 5000:5000 --restart=always --privileged=true --name registry registry

3.删除私库里面的镜像 shell脚本

#!/bin/bash

docker_name="$1"
docker_tag="$2"
docker_hub_url="hub.dy.com:5000"
docker_registry_container_id=`docker ps |grep registry |awk '{print $1}'`

if [ "$docker_tag" = "" ]; then
	docker_tag="latest"
fi

#获取image的sha256
function get_docker_sha256(){
	docker pull $docker_hub_url/$docker_name:$docker_tag > /dev/null 2>&1    #先下载下来 获取sha256
	sha256=`docker inspect $docker_hub_url/$docker_name:$docker_tag | grep RepoDigests -A 5 | grep $docker_hub_url| grep sha256|awk -F '@' '{print $2}'|awk -F'"' '{print $1}'`
	#docker rmi  $docker_hub_url/$docker_name:$docker_tag		#然后删除本地镜像  是否删除
	echo $sha256
}

#删除镜像索引
function delete_images_index(){
    sha256=`get_docker_sha256`
	echo $sha256
	curl -I -X DELETE http://$docker_hub_url/v2/$docker_name/manifests/$sha256
}

#删除镜像数据
function delete_images_data(){
	docker exec -it $docker_registry_container_id registry garbage-collect /etc/docker/registry/config.yml 
}

function main()
{
	delete_images_index
	delete_images_data
	systemctl restart docker	#重启docker  否则再次push 已经删除的镜像会出错
}

main

4.打印私库里面的所有镜像 shell脚本

#!/bin/bash

#本脚本可以打印私有镜像库中所有的镜像
#私有镜像库地址
HUB_URL=hub.dy.com:5000
#取得所有私有镜像库的所有镜像
IMAGESLIST=$(curl -s http://${HUB_URL}/v2/_catalog)
#echo ${IMAGESLIST}
#删除后2个字符
IMAGESLIST=${IMAGESLIST::-2}
#删除前面17个字符
IMAGESLIST=${IMAGESLIST:17}
#echo ${IMAGESLIST}
#要将$a分割开,先存储旧的分隔符
OLD_IFS="$IFS"
#设置分隔符
IFS="," 
#如下会自动分隔
arr=($IMAGESLIST)

#遍历数组
for IMAGENAME in ${arr[@]}
do
	#去掉引号
	IMAGENAME=${IMAGENAME:1}
	IMAGENAME=${IMAGENAME::-1}
	#echo $IMAGENAME
	S1=$(curl -s http://${HUB_URL}/v2/${IMAGENAME}/tags/list)
	
	#取得[]中间的版本列表
	S1=${S1#*[}
	S1=${S1%]*} 
	#echo $S1
	
	arr1=($S1)
	#遍历数组
	for VAR in ${arr1[@]}
	do
		#去掉引号
		VAR=${VAR:1}
		VAR=${VAR::-1}
		echo $HUB_URL/$IMAGENAME:$VAR
	done
done

#恢复原来的分隔符
IFS="$OLD_IFS"
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐