本文共 2436 字,大约阅读时间需要 8 分钟。
1. 背景
docker中要使用镜像,一般会从本地、docker Hup公共仓库和其它第三方公共仓库中下载镜像,一般出于安全和外网(墙)资源下载速率的原因考虑企业级上不会轻易使用。那么有没有一种办法可以存储自己的镜像的仓库呢? ----> 企业级环境中搭建自己的私有仓库。
2. 私有仓库有优势:
一、节省网络带宽,针对于每个镜像不用每个人都去中央仓库上面去下载,只需要从私有仓库中下载即可;
二、提供镜像资源利用,针对于公司内部使用的镜像,推送到本地的私有仓库中,以供公司内部相关人员使用。
3. 环境:
1 2 3 4 | [root@registry ~] # cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) [root@registry ~] # uname -r 3.10.0-327.36.3.el7.x86_64 |
4. 服务器Ip地址
192.168.60.150
5. 安装:
* 安装docker
1 | [root@registry ~] # yum install docker |
* 启动docker服务
1 | [root@registry ~] # systemctl start docker |
* 设置docker服务开机启动
1 | [root@registry ~] # systemctl enable docker |
* 拉取 registry镜像,例如在daocloud.io/registry这个私有镜像仓库
1 | [root@registry ~] # docker pull daocloud.io/registry |
* 创建本地镜像存储目录
1 | [root@registry ~] # mkdir /data/local_docker_registry -p |
* 运行容器,设置容器名称为local_docker_registry, 挂在镜像内docker镜像仓库/var/lib/registry 至本地/data/local_docker_registry目录,并曝光5000端口, --restart=always让其跟随docker启动时启动
1 | [root@registry ~] # docker run --name local_docker_registry --restart=always -d -v /data/local_docker_registry:/var/lib/registry -p 5000:5000 daocloud.io/registry |
* 测试 [ 有返回就表示成功 ]
1 2 | [root@registry ~] # curl 192.168.60.150:5000/v2 <a href= "/v2/" >Moved Permanently< /a >. |
6. 上传镜像至私有仓库测试
* 编写dockerfile文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | # Nginx # Version 1.0.1 # Author lisea # Url https://lisea.cn # Base imgae FROM centos # Maintainer MAINTAINER lisea lisea@126.com # Commands RUN rpm -ivh http: //mirrors .aliyun.com /epel/epel-release-latest-7 .noarch.rpm RUN yum install nginx -y RUN echo "daemon off;" >> /etc/nginx/nginx .conf RUN echo "this is test nginx image" > /usr/share/nginx/html/index .html EXPOSE 80 CMD [ "nginx" ] |
* 通过Dockerfile构建一个新镜像, 直接指明registry和标签
1 | [root@registry nginx] # docker build -t 192.168.60.150:5000/nginx:1.0.1 . |
* push 上传镜像到私有仓库
1 | [root@registry nginx] # docker push 192.168.60.150:5000/nginx:1.0.1 |
* 查看镜像是否上传成功
1 2 | [root@registry nginx] # curl 192.168.60.150:5000/v2/_catalog { "repositories" :[ "nginx" ]} |
* 其它服务器使用此镜像
1 | [root@registry nginx] # docker pull 192.168.60.150:5000/nginx:1.0.1 |
7. 相关问题
* 可能会出现无法push镜像到私有仓库的问题。这是因为我们启动的registry服务不是安全可信赖的。 解决:
1. 需要修改docker的配置文件 /etc/sysconfig/docker-network,在添加下面的内容,
1 | DOCKER_NETWORK_OPTIONS= "--insecure-registry 192.168.60.150:5000" |
2. 重启docker
1 | [root@registry nginx] # systemctl restart docker |
8. 总结
以需求驱动技术,技术本身没有优略之分,只有业务之分。
本文转自asd1123509133 51CTO博客,原文链接:http://blog.51cto.com/lisea/1934617,如需转载请自行联系原作者