利用docker registry搭建私有仓库(自签发证书+登陆认证)

1.利用registry镜像搭建私有仓库

1.配置阿里云镜像加速器

(1)访问阿里云并登陆

在这里插入图片描述
在这里插入图片描述
(2)获取镜像加速器
在这里插入图片描述

(3).配置镜像加速器

[root@foundation66 Desktop]# cd /etc/docker/
[root@foundation66 docker]# ls
certs.d  key.json
#1.修改配置文件
[root@foundation66 docker]# vim daemon.json
###########################
{
  "registry-mirrors": ["https://2izot27h.mirror.aliyuncs.com"]
}

#2.重载守护进程
[root@foundation66 docker]# systemctl daemon-reload
#3.重启docker服务
[root@foundation66 docker]# systemctl restart docker

2.从阿里云拉取registry镜像

[root@foundation66 ~]# docker pull registry

在这里插入图片描述3.创建私有仓库(容器)

#1.创建私有仓库(容器),-d表示打入后台,-p表示端口映射,-v表示挂载数据卷
[root@foundation66 ~]# docker run -d --name registry -p 5000:5000 -v /opt/registry:/var/lib/registry registry
270b16b732b06ae4c6745a5873141dc7fe79c324ba54f11b86bf251bffbacb05
#2.查看容器
[root@foundation66 ~]# docker ps

在这里插入图片描述测试:

1.上传自定义镜像到私有仓库

#查看镜像

[root@foundation66 ~]# docker images

在这里插入图片描述#tag表示重命名

 docker tag busybox:v1 localhost:5000/busybox:v1
 docker images 

上传镜像到私有仓库

docker push localhost:5000/busybox:v1

在这里插入图片描述

 cd /opt/registry/
 ls
 cd docker/
 tree .

在这里插入图片描述2.删除原有的busybox镜像

#查看镜像
docker images

在这里插入图片描述#删除原有的busybox镜像

[root@foundation66 docker]# docker rmi localhost:5000/busybox:v1
[root@foundation66 docker]# docker rmi busybox:v1
[root@foundation66 docker]# docker rmi busybox:v2
[root@foundation66 docker]# docker rmi busybox:v3
[root@foundation66 docker]# docker rmi busybox:v4

#查看镜像

[root@foundation66 docker]# docker images busybox
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
busybox             latest              59788edf1f3e        5 months ago        1.15MB

3.从私有仓库中拉取镜像

从私有仓库中拉取镜像
[root@foundation66 docker]# docker pull localhost:5000/busybox:v1

在这里插入图片描述

2.使用自签发证书搭建私有仓库

1.制作证书

[root@foundation66 docker]# cd /opt/docker/
[root@foundation66 docker]# ls
Dockerfile  dvd.repo  test  webdata
[root@foundation66 docker]# mkdir -p certs
[root@foundation66 docker]# openssl req \
-newkey rsa:4096 -nodes -sha256 -keyout certs/westos.org.key \
-x509 -days 365 -out certs/westos.org.crt

在这里插入图片描述

[root@foundation66 docker]# ls certs/
westos.org.crt  westos.org.key

2.添加解析

[root@foundation66 docker]# vim /etc/hosts
##########################
172.25.254.66 westos.org

在这里插入图片描述4.使用自签发证书重新创建私有仓库

#证书加密的方式创建私有仓库(容器)
[root@foundation66 docker]# docker run -d \
 --restart=always \
 --name registry \
 -v `pwd`/certs:/certs \
-e REGISTRY_HTTP_ADDR=0.0.0.0:443 \
 -e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/westos.org.crt \
 -e REGISTRY_HTTP_TLS_KEY=/certs/westos.org.key \
 -p 443:443 \
 -v /opt/registry:/var/lib/registry \ registry
[root@foundation66 docker]# docker ps

在这里插入图片描述
5.配置远端结点

(1).下载并安装docker服务
(2).开启docker服务
(3).添加解析

6.拷贝证书

#证书:westos.org.crt
[root@foundation66 docker]# cd /opt/docker/certs/
[root@foundation66 certs]# ls
westos.org.crt  westos.org.key
#拷贝证书
[root@foundation66 ~]# cd /etc/docker/certs.d/westos.org/
[root@foundation66 westos.org]# ls
ca.crt
[root@foundation66 westos.org]# cp /opt/docker/certs/westos.org.crt ca.crt 
cp: overwrite ‘ca.crt’? y

测试:
在服务端:上传镜像到私有仓库

[root@foundation66 ~]# docker images在这里插入图片描述

#1.更改镜像名称;便于区分
[root@foundation66 ~]# docker tag game2048 westos.org/game2048

在这里插入图片描述在远端:从私有仓库中拉取镜像

#拉取镜像,发现报错,因为没有证书
[root@server1 docker]# docker pull westos.org/game2048

在这里插入图片描述
解决方案:

#1.创建目录(与服务端存放证书的路径相同)
[root@server1 docker]# mkdir -p /etc/docker/certs.d/westos.org 
[root@server1 docker]# cd /etc/docker/certs.d/westos.org/
[root@server1 westos.org]# ls


#2.发送证书
[root@foundation66 ~]# cd /etc/docker/certs.d/westos.org
[root@foundation66 westos.org]# ls
ca.crt
[root@foundation66 westos.org]# scp ca.crt 172.25.66.1:/etc/docker/certs.d/westos.org/
root@172.25.66.1's password: 
ca.crt                                          100% 2098    70.9KB/s   00:00  
#3.查看证书
[root@server1 westos.org]# pwd
/etc/docker/certs.d/westos.org
[root@server1 westos.org]# ls
ca.crt

测试:

#1.拉取镜像成功
[root@server1 ~]# docker pull westos.org/game2048

在这里插入图片描述

#使用westos.org/game2048镜像创建并运行vm1容器
[root@server1 ~]# docker run -d -p 80:80 --name vm1 westos.org/game2048
097c24ef5ba9a843c9270d2ca215ab65919b7869b6817ee8570b9548186dcd14
[root@server1 ~]# docker ps

在这里插入图片描述

3.登陆认证方式搭建私有仓库

1.创建认证用户和密码
mkdir /opt/docker
[root@foundation66 ~]# cd /opt/docker/
[root@foundation66 docker]# ls
certs  Dockerfile  dvd.repo  test  webdata
#1.创建目录
[root@foundation66 docker]# mkdir auth
#2.创建认证用户和密码,第一次执行用 >
[root@foundation66 docker]# docker run --entrypoint htpasswd registry -Bbn admin westos > auth/htpasswd
#3.     ;-rm表示创建之后自动删除,>>表示追加;如果继续用>会覆盖之前的内容
[root@foundation66 docker]# docker run --rm --entrypoint htpasswd registry -Bbn studdent redhat >> auth/htpasswd
[root@foundation66 docker]# docker ps -a

在这里插入图片描述

[root@foundation66 docker]# docker rm hungry_villani
hungry_villani
#4.查看认证用户和密码
[root@foundation66 docker]# cat auth/htpasswd 

在这里插入图片描述2.删除之前创建的私有仓库

[root@foundation66 docker]# docker ps

在这里插入图片描述删除私有仓库(容器)

[root@foundation66 docker]# docker rm -f registry 
registry

3.登陆认证方式重新创建私有仓库
登陆认证方式重新创建私有仓库

[root@foundation66 docker]# docker run -d
–restart=always
–name registry
-v pwd/certs:/certs
-e REGISTRY_HTTP_ADDR=0.0.0.0:443
-e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/westos.org.crt
-e REGISTRY_HTTP_TLS_KEY=/certs/westos.org.key
-v /opt/registry:/var/lib/registry
-v pwd/auth:/auth -e “REGISTRY_AUTH=htpasswd”
-e “REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm”
-e REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd
-p 443:443
registry
793665688f9e2397fd1898145f575c838a881a053b0932c023fce80c3db54474
#2.查看容器
[root@foundation66 docker]# docker ps
在这里插入图片描述测试:

[root@foundation66 docker]# docker images在这里插入图片描述#1.重命名

[root@foundation66 docker]# docker tag rhel7 westos.org/rhel7
[root@foundation66 docker]# docker images

在这里插入图片描述#上传镜像,报错

[root@foundation66 docker]# docker push westos.org/rhel7

在这里插入图片描述#2.登陆认证

[root@foundation66 docker]# docker login westos.org

在这里插入图片描述

[root@foundation66 ~]# cd /root/.docker
#认证成功后认证信息会自动记录在该文件中,即第一次认证之后再上传镜像便无需再做认证
[root@foundation66 .docker]# cat config.json 

在这里插入图片描述#3.上传镜像

[root@foundation66 ~]# docker push westos.org/rhel7

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值