您的位置:首页 > 运维架构 > Docker

docker学习(8) 在mac机上搭建私有仓库

2016-02-04 23:22 721 查看
docker的私有仓库类似maven的私服,一般用于公司内部搭建一个类似docker hub的环境,这样上传、下载镜像速度较快,本文将演示如何在mac上利用docker-machine搭建无需SSL证书的私有仓库。

一、查看docker-machine虚拟机IP

docker-machine ip default


默认情况下docker-toolbox创建的虚拟机名称为default,如果您的虚拟机名字不是这个,上面命令最后的default换成真实的虚拟机名字,假设default分配的IP为192.168.99.100

二、修改虚拟机中的docker启动配置

由于docker最新版本默认访问私服时,强制采用SSL安全连接,但一般内部应用时不需要这么高的安全级别,参考下面的做法降低安全设置:

docker-machine ssh default
sudo vi /var/lib/boot2docker/profile


在profile文件最后加上:

EXTRA_ARGS="--insecure-registry 192.168.99.100:5000"


然后exit退出default,输入以下命令重启虚拟机

docker-machine restart default


三、创建私服容器

dao pull registry

docker run -d -p 5000:5000 --restart=always -h registry \
--name registry \
-v /Users/yjmyzz/data/registry:/tmp/registry \
registry


第1行的dao pull registry表示将从daocloud.io上拉取registry镜像,如果本机已经有该镜像,可以省略。

-v 后面的路径,大家改成实际路径,这个目录用于存放push到私有仓库的images文件。

四、测试上传、下载

4.1 先从daocloud.io上拉一个hello-world

hello-world这个镜像只有960b,可以拿这个练手

dao pull hello-world


4.2 将hello-world打标签成私服镜像

docker tag hello-world 192.168.99.100:5000/hello-world


上面的ip要换真实的虚拟机ip,执行完以后,本机镜像文件应该能看到这个images,见下图:



注:原始镜像hello-world与打tag后的镜像具有相同的IMAGE ID,说明这二个镜像就是同一个,只是tag不同而已。

4.3 上传到私有仓库

docker push 192.168.99.100:5000/hello-world


顺利的话,应该很快就能上传完:

➜  ~  docker push 192.168.99.100:5000/hello-worldThe push refers to a repository [192.168.99.100:5000/hello-world] (len: 1)
Sending image list
Pushing repository 192.168.99.100:5000/hello-world (1 tags)
3f12c794407e: Image successfully pushed
975b84d108f1: Image successfully pushed
Pushing tag for rev [975b84d108f1] on {http://192.168.99.100:5000/v1/repositories/hello-world/tags/latest}


可以直接在浏览器里访问:http://192.168.99.100:5000/v1/search,如果能看到

{
"num_results": ​1,
"query": "",
"results":
[
{
"description": "",
"name": "library/hello-world"
}
]
}


说明上传成功

4.4 从私有仓库下载

因为本机已经有hello-world的镜像了,为了方便验证,先把它删除:

docker rmi -f hello-world 192.168.99.100:5000/hello-world
#或
#docker rmi -f 975b84d108f1 #即:hello-world的IMAGE ID


然后下载:

docker pull 192.168.99.100:5000/hello-world


内网环境,应该很快就能下载完成:

➜  ~  docker pull 192.168.99.100:5000/hello-worldUsing default tag: latest
Pulling repository 192.168.99.100:5000/hello-world
975b84d108f1: Download complete
3f12c794407e: Download complete
Status: Downloaded newer image for 192.168.99.100:5000/hello-world:latest
192.168.99.100:5000/hello-world: this image was pulled from a legacy registry.
Important: This registry version will not be supported in future versions of docker.


注:如果私有仓库要放置在公网上,建议还是按官方推荐的做法,设置SSL证书,强制走https协议,否则将有安全风险。

参考文章:
1. Docker私有Registry在CentOS6.X下安装指南
2. 搭建私有 Docker 仓库服务器
3. Use private docker registry in OS-X
4. Deploying a registry server
5. allow insecure registry in host provisioned with docker-machine
6. Adding trusted root certificates to the server
7. How To Set Up a Private Docker Registry on Ubuntu 14.04
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: