docker仓库

docker仓库管理:初步了解仓库是如何管理的。
官方仓库地址:可以注册一个官方仓库来上传自己写好的镜像和拉取想要的镜像。
部署本地私有仓库:docker官方文档教你如何Deploy a registry server。
harbor:提供了一个可视化的仓库管理界面。

  • Docker 仓库是用来包含镜像的位置,Docker提供一个注册服务器(Register)来保存多个仓库,每个仓库又可以包含多个具备不同tag的镜像。Docker运行中使用的默认仓库是 Docker Hub 公共仓库。
  • registry的工作原理:一次docker pull 或 push背后发生的事情
    index服务主要提供镜像索引以及用户认证的功能。当下载一个镜像的时候,首先会去index服务上做认证,然后查找镜像所在的registry的地址并放回给docker客户端,docker客户端再从registry下载镜像,在下载过程中 registry会去index校验客户端token的合法性,不同镜像可以保存在不同的registry服务上,其索引信息都放在index服务上。
  • Docker Registry有三个角色,分别是index、registry和registry client。
  • index
    负责并维护有关用户帐户、镜像的校验以及公共命名空间的信息。
    Web UI
    元数据存储
    认证服务
    符号化
  • registry
    是镜像和图表的仓库,它不具有本地数据库以及不提供用户认证,通过Index Auth service的Token的方式进行认证。
  • Registry Client
    Docker充当registry客户端来维护推送和拉取,以及客户端的授权。

搭建一个私有仓库

##下载registry镜像
[root@server1 ~]# docker search registry
[root@server1 ~]# docker pull registry  
[root@server1 ~]# docker images registry
[root@server1 ~]# docker ps -a
[root@server1 ~]# docker stop webserver
webserver
[root@server1 ~]# docker rm webserver
[root@server1 ~]# docker history registry:latest 

##运行registry容器
[root@server1 ~]# docker run -d --name registry -p 5000:5000 -v /opt/registry:/var/lib/registry registry  
[root@server1 ~]# docker ps
[root@server1 ~]# netstat -antlp
[root@server1 ~]# ll -d /opt/registry/

上传镜像到本地仓库,本地镜像在命名时需要加上仓库的ip和端口
[root@server1 ~]# docker tag webserver:v4 localhost:5000/webserver:latest
[root@server1 ~]# docker images
REPOSITORY                        TAG       IMAGE ID       CREATED        SIZE
webserver                         v4        047ab2e35274   15 hours ago   31.7MB
localhost:5000/webserver          lastest   047ab2e35274   15 hours ago   31.7MB
[root@server1 ~]# docker push localhost:5000/webserver
[root@server1 ~]# tree /opt/registry/
[root@server1 ~]# curl localhost:5000/v2/_catalog
{"repositories":["webserver"]}

远程连接私有仓库

server2上首先安装docker-ce,并且开机自启。
[root@server2 yum.repos.d]# cd /etc/docker/
[root@server2 docker]# ls
key.json
[root@server2 docker]# vim daemon.json
{
  "insecure-registries" : ["192.168.0.1:5000"]
}
[root@server2 docker]# systemctl reload docker
[root@server2 docker]# docker pull 192.168.0.1:5000/webserver
[root@server2 docker]# docker images
[root@server2 docker]# docker tag 192.168.0.1:5000/webserver webserver
[root@server2 docker]# docker run -d webserver

签名加密

[root@server2 docker]# rm -fr daemon.json
[root@server2 docker]# systemctl reload docker
生成自己的证书
[root@server1 ~]# mkdir  -p  certs
[root@server1 ~]# openssl req \
> -newkey rsa:4096 -nodes -sha256 -keyout certs/westos.org.key \
> -x509 -days 365 -out certs/westos.org.crt
域名westos.org要求在主机上有解析
[root@server1 ~]# docker stop registry
[root@server1 ~]# docker rm registry

[root@server1 ~]# vim /etc/hosts      ##加上reg.westos.org解析,server2相同
将westos.org..crt文件复制到 /etc/docker/certs.d/reg.westos.org/ca.crt每个Docker主机上。您无需重启Docker。
[root@server1 ~]# mkdir  /etc/docker/certs.d/reg.westos.org -p
[root@server2 ~]# mkdir  /etc/docker/certs.d/reg.westos.org -p
[root@server1 ~]# cp certs/westos.org.crt /etc/docker/certs.d/reg.westos.org/ca.crt
拷贝证书到其他docker主机
[root@server1 ~]# cd /etc/docker/certs.d/reg.westos.org/
[root@server1 reg.westos.org]# scp ca.crt server2:/etc/docker/certs.d/reg.westos.org/
重新启动注册表,将其定向为使用TLS证书。此命令将certs/目录绑定安装到容器中的/certs/,并设置环境变量,该变量告诉容器在何处找到domain.crt anddomain.key文件。注册表在端口443(默认的HTTPS端口)上运行。
[root@server1 ~]# docker run -d --name registry -p 443:443 -v /opt/registry:/var/lib/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 registry

测试

server1上传,server2下载

[root@server1 ~]# docker tag busybox:latest reg.westos.org/busybox:latest
[root@server1 ~]# docker push reg.westos.org/busybox:latest
[root@server2 ~]# docker pull reg.westos.org/busybox:latest



本地认证

[root@server1 ~]# mkdir auth
[root@server1 ~]# yum provides */htpasswd
[root@server1 ~]# yum install -y httpd-tools
[root@server1 ~]# htpasswd -c -B auth/htpasswd linux
[root@server1 ~]# htpasswd -B auth/htpasswd admin
[root@server1 ~]# cat auth/htpasswd 
[root@server1 ~]# docker run -d --name registry -p 443:443 -v /opt/registry:/var/lib/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 "$(pwd)"/auth:/auth -e "REGISTRY_AUTH=htpasswd" -e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" -e REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd registry
[root@server1 ~]# ls certs/
westos.org.crt  westos.org.key
[root@server1 ~]# docker ps
[root@server1 ~]# docker tag ubuntu:latest reg.westos.org/ubuntu:latest

harbor仓库

harbor安装包下载地址.
部署:(依赖性 docker 17.06.0-ce+ and docker-compose 1.18.0+)

[root@server1 ~]# tar zxf harbor-offline-installer-v1.10.1.tgz 
[root@server1 ~]# cd harbor/
[root@server1 harbor]# mv docker-compose-Linux-x86_64-1.27.0 /usr/local/bin/docker-compose
[root@server1 harbor]# chmod +x /usr/local/bin/docker-compose
[root@server1 harbor]# docker rm -f registry
[root@server1 ~]# cp -r certs/ /      ##因为下面配置文件里我选择了放在/下,所以这里将证书复制到根下了。
[root@server1 harbor]# vim harbor.yml        ## 修改内容如下
hostname: reg.westos.org
  certificate: /certs/westos.org.crt
  private_key: /certs/westos.org.key
harbor_admin_password: westos

[root@server1 harbor]# ./install.sh --help
[root@server1 harbor]# ./install.sh 
[root@server1 harbor]# docker ps
[root@server1 harbor]# docker-compose ps






[root@server1 harbor]# docker logout reg.westos.org
Removing login credentials for reg.westos.org
[root@server1 harbor]# cat ~/.docker/config.json 
{
	"auths": {}
[root@server1 harbor]#docker login reg.westos.org
Username: admin
[root@server1 harbor]# docker tag busybox:latest reg.westos.org/library/busybox:latest
[root@server1 harbor]# docker push reg.westos.org/library/busybox:latest

[root@server2 ~]# cd /etc/docker/
[root@server2 docker]# vim daemon.json
{
  "registry-mirrors": ["https://reg.westos.org"]
}
[root@server2 docker]# systemctl reload docker
[root@server2 docker]# docker pull busybox

建立私有仓库并设置维护人员和访客

[root@server1 data]# docker logout reg.westos.org
[root@server1 data]# docker login reg.westos.org
Username: linux  ##维护人员
[root@server1 data]# docker tag ubuntu:latest reg.westos.org/westos/ubuntu:latest
[root@server1 data]# docker push reg.westos.org/westos/ubuntu:latest



[root@server2 docker]# docker logout reg.westos.org
Removing login credentials for reg.westos.org
[root@server2 docker]# docker login reg.westos.org
Usern[root@server2 docker]# docker tag webserver:latest reg.westos.org/westos/webserver:latest
[root@server2 docker]# docker push reg.westos.org/westos/webserver:latest
username: demo ##访客
[root@server2 docker]# docker tag webserver:latest reg.westos.org/westos/webserver:latest
[root@server2 docker]# docker push reg.westos.org/westos/webserver:latest


配置中加入镜像扫描、内容信任等选项

[root@server1 harbor]# docker-compose down 
[root@server1 harbor]# ./prepare  #清理
[root@server1 harbor]# ./install.sh --with-notary --with-clair --with-chartmuseum
[root@server1 harbor]# docker-compose ps
  • 配置中加入了漏洞扫描,内容信任以及helm charts等选项,helm charts是为了之后的k8s做准备。
手动进行扫描,扫描太耗费内存的,所以不建议开启扫描功能。
[root@server1 harbor]# docker login reg.westos.org
Username: admin
[root@server1 harbor]# docker push reg.westos.org/library/game2048:latest

  • 启用docker内容信任
部署根证书:
[root@server1 ~]# mkdir ~/.docker/tls/reg.westos.org:4443 -p
[root@server1 ~]# cd ~/.docker/tls/reg.westos.org:4443
[root@server1 reg.westos.org:4443]# cp /etc/docker/certs.d/reg.westos.org/ca.crt .
启用docker内容信任:
export DOCKER_CONTENT_TRUST=1
export DOCKER_CONTENT_TRUST_SERVER=https://reg.westos.org:4443
上传镜像:
[root@server1 ~]# docker tag yakexi007/game2048:latest reg.westos.org/library/game2048:latest
[root@server1 ~]# docker push reg.westos.org/library/game2048:latest
当我们修改镜像的名字的时候需要我们输入root key 当我们只修改标签的时候,我们只用输入repository key




为了不让扫描,使得占用的空间越来越大,所以重新修改一下选项
[root@server1 ~]# export DOCKER_CONTENT_TRUST=0
[root@server1 harbor]# docker-compose down
[root@server1 harbor]# ./prepare
[root@server1 harbor]# ./install.sh  --with-chartmuseum
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值