一、环境准备
docker把registry直接做成了镜像,所以我们需要直接下载镜像导入运行即可
registry下载链接:
链接:https://pan.baidu.com/s/1ABT8YkjXmz6OYRxOvErWwA
提取码:s7zo
二、构建私有仓库registry
- 导入下载好的镜像
docker load -i registry.tar.gz
- 执行命令运行容器
docker run -d -p 5000:5000 --restart=always --name registry -v /opt/myregistry:/var/lib/registry registry
命令说明:
-d 后台运行
-p 5000:5000 端口5000映射到5000
--restart=always 只要docker启动registry就会自动启动
--name 指定容器名字
-v 创建共享数据卷与宿主机共享数据
三、上传镜像到私有仓库registry
既然仓库已构建,尝试把常用镜像上传仓库内
- 1.打标签
我本地存在busybox镜像,现以它作为测试打好标签(没有可以直接pull一个)
docker tag 83aa35aa1c79 10.0.0.10:5000/busybox:v1
- 2.docker push上传
[root@docker ~]# docker push 10.0.0.10:5000/busybox:v1
The push refers to repository [10.0.0.10:5000/busybox]
Get https://10.0.0.10:5000/v2/: http: server gave HTTP response to HTTPS client
第一次上传出现了报错
The push refers to repository [10.0.0.10:5000/busybox]
Get https://10.0.0.10:5000/v2/: http: server gave HTTP response to HTTPS client
解决方法:
把自己的私有镜像仓库加到信任的仓库列表中
vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://registry.docker-cn.com"],
"insecure-registries": ["10.0.0.10:5000"]
}
此文件之前做过镜像加速,再增加新内容在原操作码后加,
分隔
重启一下docker
systemctl restart docker
再次尝试上传:
docker push 10.0.0.10:5000/busybox:v1
[root@docker ~]# docker push 10.0.0.10:5000/busybox:v1
The push refers to repository [10.0.0.10:5000/busybox]
a6d503001157: Pushed
v1: digest: sha256:afe605d272837ce1732f390966166c2afff5391208ddd57de10942748694049d size: 527
验证:
[root@docker ~]# ls /opt/myregistry/docker/registry/v2/repositories/
busybox
四、加basic认证的registry
下载需要的工具包并创建好密码存放目录:
yum install httpd-tools -y
mkdir /opt/registry-var/auth/ -p
htpasswd -Bbn wsm 123456 >> /opt/registry-var/auth/htpasswd
直接运行以下命令:
docker run -d -p 5000:5000 --restart=always -v /opt/registry-var/auth/:/auth/ -v /opt/myregistry:/var/lib/registry -e "REGISTRY_AUTH=htpasswd" -e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" -e "REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd" registry
命令详解:
-v /opt/registry-var/auth/:/auth/ 添加密码目录的数据卷
-v /opt/myregistry:/var/lib/registry 添加仓库目录数据卷
-e "REGISTRY_AUTH=htpasswd" -e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" -e "REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd" 官方要求的环境变量
到这里带认证的仓库已启动,这次我们再想向私有仓库上传镜像时,需要先登录
账号为:wsm 密码:123456 #刚刚创建过的
[root@docker ~]# docker login 10.0.0.10:5000
Username: wsm
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
密码保留处:
[root@docker ~]# cat /root/.docker/config.json
{
"auths": {
"10.0.0.10:5000": {
"auth": "d3NtOjEyMzQ1Ng=="
}
},
"HttpHeaders": {
"User-Agent": "Docker-Client/19.03.8 (linux)"
}
再次尝试上传:
[root@docker ~]#docker push 10.0.0.10:5000/busybox:v1
The push refers to repository [10.0.0.10:5000/busybox]
a6d503001157: Layer already exists
v1: digest: sha256:afe605d272837ce1732f390966166c2afff5391208ddd57de10942748694049d size: 527
最后docker logout 10.0.0.10:5000
登出
在仓库满后执行删除镜像操作
直接进入仓库并删除repo