centos7搭建Docker私有库及删除库内镜像
环境准备
系统:
cat /etc/redhat-release centos linux release 7.4.1708 (core)
主机两台,分别是docker私有库服务器(ip 192.168.121.121)和用户开发机(ip 192.168.121.122),开发机从私有库服务器拉取镜像。
1、配置软件源并安装安装docker
两台主机安装docker
yum install docker-ce
2、搭建私有镜像仓库
登陆私有库服务器
创建docker管理账户并设置密码
useradd dkuser passwd dkuser
把账户加入docker组,这一步是为了可以使用普通账户管理docker,而不用使用sudo命令
usermod -g docker dkuser
为账户配置sudo权限
visudo
添加下面一行
dkuser all=(all) nopasswd:all
切换账户
su dkuser
启动docker服务
sudo systemctl start docker
可以通过官方提供的registry镜像来搭建本地的私有仓库,所以拉取官方镜像
docker pull registry
创建存放镜像和docker配置文件的目录
sudo mkdir /data/docker
创建配置文件,storage配置中的delete=true配置项,是为了允许删除镜像。默认的镜像是没有这个参数
sudo vi /data/docker/config.yml
1 version: 0.1 2 3 log: 4 fields: 5 service: registry 6 storage: 7 delete: 8 enabled: true 9 cache: 10 blobdescriptor: inmemory 11 filesystem: 12 rootdirectory: /var/lib/registry 13 http: 14 addr: :5000 15 headers: 16 x-content-type-options: [nosniff] 17 health: 18 storagedriver: 19 enabled: true 20 interval: 10s 21 threshold: 3
运行registry容器
docker run -d -p 5000:5000 -v /data/docker/registry:/var/lib/registry -v /data/docker/config.yml:/etc/docker/registry/config.yml --restart always --name registry registry
说明:
-d -p 5000:5000 端口映射
-v /data/docker/registry:/var/lib/registry 默认情况下,会将仓库存放于容器内的/var/lib/registry目录下,指定本地目录挂载到容器
--restart always 在容器退出时总是重启容器,主要应用在生产环境
--name registry 指定容器的名称
查看容器
docker ps
container id image command created status ports names
071105c54db3 registry "/entrypoint.sh /etc…" 3 minutes ago up about a minute 0.0.0.0:5000->5000/tcp registry
3、把本地镜像上传到私有仓库
在私有库服务器上
这里以busybox镜像为例,因为比较小
首先把镜像下载到本地
docker pull busybox
为镜像打标签
docker tag busybox 192.168.121.121:5000/busybox
查看镜像
docker images
repository tag image id created size
192.168.121.121:5000/busybox latest db8ee88ad75f 7 hours ago 1.22mb
busybox latest db8ee88ad75f 7 hours ago 1.22mb
编辑配置文件,添加配置是为了能够在本地上传镜像。
sudo vi /usr/lib/systemd/system/docker.service
在 execstart=/usr/bin/dockerd 后边添加
--insecure-registry 192.168.121.121:5000
重启docker服务
sudo systemctl daemon-reload sudo systemctl restart docker
推送镜像到私有库
docker push 192.168.121.121:5000/busybox the push refers to repository [192.168.121.121:5000/busybox] 0d315111b484: pushed latest: digest: sha256:895ab622e92e18d6b461d671081757af7dbaa3b00e3e28e12505af7817f73649 size: 527
报错解决:
docker push 192.168.121.121:5000/busybox the push refers to repository [192.168.121.121:5000/busybox] get https://192.168.121.121:5000/v2/: http: server gave http response to https client
因为docker从1.3.x之后,与docker registry交互默认使用的是https,然而此处搭建的私有仓库只提供http服务,所以当与私有仓库交互时就会报上面的错误。为了解决这个问题需要在启动docker server时增加启动参数为默认使用http访问
sudo vi /usr/lib/systemd/system/docker.service
execstart=/usr/bin/dockerd 后边添加
--insecure-registry 192.168.121.121:5000
4、开发机从私有库载入镜像
同样的也需要添加配置
vi /usr/lib/systemd/system/docker.service
execstart=/usr/bin/dockerd 后边添加
--insecure-registry 192.168.121.121:5000
启动docker服务
systemctl start docker
从私有库载入镜像
docker pull 192.168.121.121:5000/busybox using default tag: latest latest: pulling from busybox ee153a04d683: pull complete digest: sha256:895ab622e92e18d6b461d671081757af7dbaa3b00e3e28e12505af7817f73649 status: downloaded newer image for 192.168.121.121:5000/busybox:latest
查看镜像
docker images repository tag image id created size 192.168.121.121:5000/busybox latest db8ee88ad75f 7 hours ago 1.22mb
5、删除镜像
在私有库服务器上
相应的参考命令:
#查询镜像
curl <仓库地址>/v2/_catalog
#查询镜像tag(版本)
curl <仓库地址>/v2/<镜像名>/tags/list
#查询镜像digest_hash
curl --header "accept:application/vnd.docker.distribution.manifest.v2+json" -i -xget <仓库地址>/v2/<镜像名>/manifests/<tag>
#删除镜像api
curl -i -x delete "<仓库地址>/v2/<镜像名>/manifests/<镜像digest_hash>"
在服务器执行下面命令
查询镜像
curl -xget http://192.168.121.121:5000/v2/_catalog {"repositories":["busybox"]}
查询镜像tag
curl http://192.168.121.121:5000/v2/busybox/tags/list {"name":"busybox","tags":["latest"]}
查询镜像digest_hash,删除命令里边要填写的 镜像digest_hash 就是 查询结果里边 docker-content-digest: 后边的内容
curl --header "accept:application/vnd.docker.distribution.manifest.v2+json" -i -xget http://192.168.121.121:5000/v2/busybox/manifests/latest http/1.1 200 ok content-length: 527 content-type: application/vnd.docker.distribution.manifest.v2+json docker-content-digest: sha256:895ab622e92e18d6b461d671081757af7dbaa3b00e3e28e12505af7817f73649 docker-distribution-api-version: registry/2.0 etag: "sha256:895ab622e92e18d6b461d671081757af7dbaa3b00e3e28e12505af7817f73649" x-content-type-options: nosniff date: fri, 19 jul 2019 06:09:44 gmt
删除私有库镜像
curl -i -xdelete http://192.168.121.121:5000/v2/busybox/manifests/sha256:895ab622e92e18d6b461d671081757af7dbaa3b00e3e28e12505af7817f73649 http/1.1 202 accepted docker-distribution-api-version: registry/2.0 x-content-type-options: nosniff date: fri, 19 jul 2019 06:10:56 gmt content-length: 0
查看镜像信息可以看到镜像的标签显示为空 null
curl http://192.168.121.121:5000/v2/busybox/tags/list {"name":"busybox","tags":null}
这里虽然删除了,但是实际上硬盘地址还没有释放,是因为docker删除p_w_picpath只是删除的p_w_picpath的元数据信息。层数据并没有删除。现在进入registry中进行垃圾回收。
进入registry容器
docker exec -it 071105c54db3 /bin/sh / # cd /var/lib/registry/
查看镜像大小
/var/lib/registry # du -sch 756.0k . 756.0k total
执行回收命令
/var/lib/registry # registry garbage-collect /etc/docker/registry/config.yml busybox 0 blobs marked, 3 blobs and 0 manifests eligible for deletion blob eligible for deletion: sha256:895ab622e92e18d6b461d671081757af7dbaa3b00e3e28e12505af7817f73649 info[0000] deleting blob: /docker/registry/v2/blobs/sha256/89/895ab622e92e18d6b461d671081757af7dbaa3b00e3e28e12505af7817f73649 go.version=go1.11.2 instance.id=7c37e39d-6ad1-4139-b9d1-592a900b0902 service=registry blob eligible for deletion: sha256:db8ee88ad75f6bdc74663f4992a185e2722fa29573abcc1a19186cc5ec09dceb info[0000] deleting blob: /docker/registry/v2/blobs/sha256/db/db8ee88ad75f6bdc74663f4992a185e2722fa29573abcc1a19186cc5ec09dceb go.version=go1.11.2 instance.id=7c37e39d-6ad1-4139-b9d1-592a900b0902 service=registry blob eligible for deletion: sha256:ee153a04d6837058642958836062f20badf39f558be3e6c7c7773ef7d8301d90 info[0000] deleting blob: /docker/registry/v2/blobs/sha256/ee/ee153a04d6837058642958836062f20badf39f558be3e6c7c7773ef7d8301d90 go.version=go1.11.2 instance.id=7c37e39d-6ad1-4139-b9d1-592a900b0902 service=registry
再次查询,发现回收资源执行成功
/var/lib/registry # du -sch 0 . 0 total
参考:
https://www.cnblogs.com/tempted/p/7768694.html
https://blog.csdn.net/jiangeeq/article/details/81056055
https://www.cnblogs.com/hwystudy/p/10483163.html
https://blog.51cto.com/302876016/1966816
上一篇: 笔记本电脑小键盘数字打开与关闭方法图解
下一篇: 不准说我老婆是驴