官方的Docker hub是一个用于管理公共镜像的好地方,咱们能够在上面找到咱们想要的镜像,也能够把咱们本身的镜像推送上去。可是,有时候,咱们的使用场景须要咱们拥有一个私有的镜像仓库用于管理咱们本身的镜像。这个能够经过开源软件Registry来达成目的。python
Registry在github上有两份代码:老代码库和新代码库。老代码是采用python编写的,存在pull和push的性能问题,出到0.9.1版本以后就标志为deprecated,再也不继续开发。从2.0版本开始就到在新代码库进行开发,新代码库是采用go语言编写,修改了镜像id的生成算法、registry上镜像的保存结构,大大优化了pull和push镜像的效率。git
官方在Docker hub上提供了registry的镜像(详情),咱们能够直接使用该registry镜像来构建一个容器,搭建咱们本身的私有仓库服务。Tag为latest的registry镜像是0.9.1版本的,咱们直接采用2.1.1版本。github
运行下面命令获取registry镜像,算法
$ sudo docker pull registry:2.1.1
而后启动一个容器,docker
$ sudo docker run -d -v /opt/registry:/var/lib/registry -p 5000:5000 --restart=always --name registry registry:2.1.1
Registry服务默认会将上传的镜像保存在容器的/var/lib/registry,咱们将主机的/opt/registry目录挂载到该目录,便可实现将镜像保存到主机的/opt/registry目录了。json
运行docker ps看一下容器状况,ubuntu
lienhua34@lienhua34-Compaq-Presario-CQ35-Notebook-PC ~ $ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f3766397a458 registry:2.1.1 "/bin/registry /etc/d" 46 seconds ago Up 45 seconds 0.0.0.0:5000->5000/tcp registry
说明咱们已经启动了registry服务,打开浏览器输入http://127.0.0.1:5000/v2,出现下面状况说明registry运行正常,vim
如今咱们经过将镜像push到registry来验证一下。浏览器
个人机器上有个hello-world的镜像,咱们要经过docker tag将该镜像标志为要推送到私有仓库,tcp
$ sudo docker tag hello-world 127.0.0.1:5000/hello-world
而后查看如下本地的镜像,
lienhua34@lienhua34-Compaq-Presario-CQ35-Notebook-PC ~ $ sudo docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE registry 2.1.1 b91f745cd233 5 days ago 220.1 MB ubuntu 14.04 a5a467fddcb8 6 days ago 187.9 MB hello-world latest 975b84d108f1 2 weeks ago 960 B 127.0.0.1:5000/hello-world latest 975b84d108f1 2 weeks ago 960 B
接下来,咱们运行docker push将hello-world镜像push到咱们的私有仓库中,
lienhua34@lienhua34-Compaq-Presario-CQ35-Notebook-PC ~ $ sudo docker push 127.0.0.1:5000/hello-world The push refers to a repository [127.0.0.1:5000/hello-world] (len: 1) 975b84d108f1: Image successfully pushed 3f12c794407e: Image successfully pushed latest: digest: sha256:1c7adb1ac65df0bebb40cd4a84533f787148b102684b74cb27a1982967008e4b size: 2744
如今咱们能够查看咱们本地/opt/registry目录下已经有了刚推送上来的hello-world。咱们也在浏览器中输入http://127.0.0.1:5000/v2/_catalog,以下图所示,
如今咱们能够先将咱们本地的127.0.0.1:5000/hello-world和hello-world先删除掉,
$ sudo docker rmi hello-world $ sudo docker rmi 127.0.0.1:5000/hello-world
而后使用docker pull从咱们的私有仓库中获取hello-lienhua34@lienhua34-Compaq-Presario-CQ35-Notebook-PC ~ $ sudo docker pull 127.0.0.1:5000/hello-world
Using default tag: latest latest: Pulling from hello-world b901d36b6f2f: Pull complete 0a6ba66e537a: Pull complete Digest: sha256:1c7adb1ac65df0bebb40cd4a84533f787148b102684b74cb27a1982967008e4b Status: Downloaded newer image for 127.0.0.1:5000/hello-world:latest lienhua34@lienhua34-Compaq-Presario-CQ35-Notebook-PC ~ $ sudo docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE registry 2.1.1 b91f745cd233 5 days ago 220.1 MB ubuntu 14.04 a5a467fddcb8 6 days ago 187.9 MB 127.0.0.1:5000/hello-world latest 0a6ba66e537a 2 weeks ago 960 B
须要添加的选项
vim /etc/docker/daemon.json