docker创建私有仓库及存储image

来源:互联网 发布:技术导航源码 编辑:程序博客网 时间:2024/04/29 23:23

       Docker官方的Docker hub虽然提供了有很多image,也基本上包含了我们需要使用的,但是其访问起来比较慢,如果自己要定制image,多台服务器之间的共享使用此image非常不方便。这个时候我们就迫切需要一个本地的私有仓库了。下面的图说明了Docker私有仓库的作用。


      Docker私有仓库可以通过docker-registry项目来实现,通过http服务来上传下载。docker-registry在Docker hub上已有现成的image。

  $ docker search registry

     选择第1个,将其从Docker hub上down下来(最好带tag,不然会将所有的tag都down下来)。

  $ docker pull registry
     这个下载的过程有点长,耐心等待一下,O(∩_∩)O哈哈~。

  $ docker images
     registry镜像下下来后,通过下面的命令启动,并将registry的容器存储images的目录映射到宿主服务器的/opt/docker/registry目录。
  $ docker run -d -p 5000:5000 -v /opt/docker/registry:/tmp/registry registry

     启动以后在浏览器中输入http://主机IP:5000或者http://主机IP:5000/v1/search,如果有显示就说明Docker私有仓库已经建好了。下一步将本地的images push到Docker私有仓库

     现将本地的image打一个tag,新image名称必须带有"主机IP:5000",如下(我这里用127.0.0.1只是举例)。

REPOSITORY               TAG                 IMAGE ID            CREATED             VIRTUAL SIZEregistry                 latest              5562556b14f9        8 days ago          422.9 MB127.0.0.1:5000/registry  latest              5562556b14f9        8 days ago          422.9 MB
     如果缺少上面这一步,直接push image,会出现如下错误。

[docker@docker1 ~]$ docker push registryThe push refers to a repository [registry] (len: 1)Sending image list2014/10/27 15:48:25 Error: Status 403 trying to push repository registry: Account is not Active

       执行如下push命令后,可以在http://主机IP:5000/v1/search看到存储到Docker私有仓库的信息。

  $ docker push 127.0.0.1:5000/registry
      下次可以通过pull命令下载到其他服务器上,那可就快多了。pull的时候记得带"主机IP:5000",不然还是去Docker hub上下载而不是私有仓库下载,如下命令。

  $ docker pull 127.0.0.1:5000/registry

      到此,私有仓库存取image就可以了。


      如果是save/load的image,会出现下面的错误,对于这样的image,最好重新build相应的Dockerfile。

[docker@docker1 ~] $docker push 127.0.0.1:5000/nexusThe push refers to a repository [127.0.0.1:5000/nexus] (len: 1)Sending image listPushing repository 127.0.0.1:5000/nexus (1 tags)2014/10/27 14:50:37 HTTP code 400 while uploading metadata: {"error": "Missing key `id' in JSON"}


      docker1.3.0以后的版本,加入了认证机制,docker pull & push会报如下错误:

[docker@docker1 ~]$ docker push 127.0.0.1:5000/registry2014/12/02 11:33:04 Error: Invalid registry endpoint https://127.0.0.1:5000/v1/: Get https://127.0.0.1:5000/v1/_ping: EOF. If this private registry supports only HTTP or HTTPS with an unknown CA certificate, please add `--insecure-registry 127.0.0.1:5000` to the daemon's arguments. In the case of HTTPS, if you have access to the registry's CA certificate, no need for the flag; simply place the CA certificate at /etc/docker/certs.d/127.0.0.1:5000/ca.crt
      使用registry的服务器和客户端启动docker时都需要加上"--insecure-registry"参数,pull和push才可正常操作。如下命令(ip都是registry所在服务器的ip地址)。

[docker@docker1 ~]$ service docker stop[docker@docker1 ~]$ nohup docker -H unix:///var/run/docker.sock --insecure-registry 127.0.0.1:5000 -d &

0 0