Docker 已经提供了docker hub仓库,第三方也提供相应的仓库,比如网易,时速云,阿里云等,那为什么还要搭建自己私有的仓库呢?
1)网络有关,有的时候你在的环境可能突然就断网了,但是内部局域网还可以访问,那么你就可以去公司私有的仓库去下载
2)安全性,你自己或者公司的项目,一定不会直接暴露给外面的人看到,所以安全性很重要。
3)便于部署,开发环境,测试环境,正式环境,可以通过私有仓库拉取同一份镜像一步部署,即可完成。
1.下载registry镜像
[root@foundation45 Desktop]# docker search registry ##搜寻registry镜像
[root@foundation45 Desktop]# docker pull registry ##拉取镜像
2.生成容器
[root@foundation45 docker]# docker images registry
[root@foundation45 docker]# docker run -d -p 5000:5000 -v /opt/registry:/var/lib/registry registry
[root@foundation45 docker]# docker ps
[root@foundation45 docker]# vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://w8ujyhgj.mirror.aliyuncs.com"],
"insecure-registries": ["westos.org:5000"]
}
[root@foundation45 docker]# systemctl daemon-reload
[root@foundation45 docker]# vim /etc/hosts
3.修改镜像的tag,完成后把打了tag的镜像上传到本地镜像
[root@foundation45 docker]# docker tag rhel7:latest westos.org:5000/rhel7
[root@foundation45 docker]# docker push westos.org:5000/rhel7
[root@foundation45 ~]# cd /opt/registry/
[root@foundation45 registry]# ls
docker
[root@foundation45 registry]# tree docker/
4.删除本地的镜像文件,从仓库拉取测试仓库的搭建
[root@foundation45 registry]# docker images
[root@foundation45 registry]# docker rmi localhost:5000/nginx
[root@foundation45 registry]# docker pull localhost:5000/nginx
5.加ssl证书
创建证书文件夹
[root@foundation45 registry]# cd /tmp/docker/
[root@foundation45 docker]# mkdir certs
[root@foundation45 docker]# vim /etc/hosts
172.25.45.250 westos.org
[root@foundation45 docker]# ping westos.org
生成ssl证书
[root@foundation45 docker]# openssl req -newkey rsa:4096 -nodes -sha256
-keyout certs/domain.key -x509 -days 365 -out certs/domain.crt
将证书copy到上面创建的证书文件夹下
[root@foundation45 docker]# cd /etc/docker/
[root@foundation45 docker]# ls
daemon.json key.json
[root@foundation45 docker]# mkdir certs.d
[root@foundation45 docker]# cd certs.d/
[root@foundation45 certs.d]# mkdir westos.org
[root@foundation45 certs.d]# cd westos.org/
[root@foundation45 westos.org]# cp /tmp/docker/certs/domain.crt ./ca.crt
[root@foundation45 westos.org]# ls
ca.crt
5.建立私有仓库并指定证书文件
[root@foundation45 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/domain.crt -e REGISTRY_HTTP_TLS_KEY
=/certs/domain.key -p 443:443 registry
[root@foundation45 docker]# docker ps -a
查看443端口是否建立
6.上传镜像文件到私有仓库
[root@foundation45 docker]# docker tag rhel7:latest westos.org/rhel7
[root@foundation45 docker]# docker push westos.org/rhel7
将证书信息发送到其他主机,拉取测试
[root@foundation45 docker]# docker rmi westos.org/rhel7 ##删除镜像
[root@foundation45 docker]# docker pull westos.org/rhel7 ##拉取镜像
7.给私有仓库加入用户名和密码
[root@foundation45 docker]# docker rm -f `docker ps -aq` ##删除之前的容器
[root@foundation45 docker]# docker ps ##查看
建立auth目录生成用户名和密码
[root@foundation45 docker]# mkdir auth ###建立目录
[root@foundation45 docker]# docker run --entrypoint htpasswd registry -Bbn shi westos > auth/htpasswd
#生成用户名和密码
[root@foundation45 docker]# cat auth/htpasswd
[root@foundation45 docker]# docker run --entrypoint htpasswd registry -Bbn lala redhat >> auth/htpasswd
###再次添加用户时,记住用追加
[root@foundation45 docker]# cat auth/htpasswd
9.启动registry容器
[root@foundation45 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/domain.crt
-e REGISTRY_HTTP_TLS_KEY=/certs/domain.key -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
[root@foundation45 docker]# docker ps -a
10.上传镜像到仓库,失败,需要登陆
[root@foundation45 docker]# docker push westos.org/rhel7
输入用户名和密码登陆后上传镜像成功
[root@foundation45 docker]# docker login -u shi -p westos westos.org
[root@foundation45 docker]# ping westos.org
[root@foundation45 docker]# docker push westos.org/nginx
登录一次之后,数据会被建立在root下的.docker文件中
[root@foundation45 docker]# cd
[root@foundation45 ~]# cd .docker/
[root@foundation45 .docker]# cat config.json