Mac 环境部署Docker私有仓库

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

一、查看docker-machine虚拟机IP

1
docker-machine ip default

 注:如果docker-machine 没有启动,执行 docker-machine start, 会自动启动一个default的虚拟机。

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

 

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

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

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

在profile文件最后加上:

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

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

1
docker-machine restart default

 

三、创建私服容器

1
2
3
4
5
6
dao pull registry  // 也可以在其他地方下载
 
docker run -d -p 5000:5000 --restart=always -h registry \
 --name registry \
 -v /Users/edward/data/registry:/tmp/registry \
 registry

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

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

四、测试上传、下载

4.1 先从daocloud.io上拉一个httpd

1
dao pull httpd

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

1
docker tag httpd localhost:5000/httpd

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

 

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

4.3 上传到私有仓库

1
docker push localhost:5000/httpd

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

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

4.4 从私有仓库下载

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

1
2
3
docker rmi -f httpd localhost:5000/httpd
#或
#docker rmi -f 975b84d108f1 #即:httpd的IMAGE ID

然后下载:

1
docker pull localhost:5000/httpd

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

1
2
3
4
5
6
7
➜  ~  docker pull localhost:5000/httpd
Using default tag: latest
Pulling repository localhost:5000/httpd
975b84d108f1: Download complete
3f12c794407e: Download complete
Status: Downloaded newer image for localhost:5000/httpd:latest
localhost:5000/httpd: this image was pulled from a legacy registry.  <br>Important: This registry version will not be supported in future versions of docker.

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

5 验证:

docker run -d -p 80:80 localhost:5000/httpd 

OK!

 

注:如果registry仓库是在局域网的其它服务器(如:192.168.1.14:5000)上运行,mac做为一个docker 客户端从仓库下载镜像,可以这样配置后,再重启。

 

posted @ 2018-01-12 19:59  丹江流  阅读(1052)  评论(0编辑  收藏  举报