docker 私有仓库简易搭建

概要

docker hub 使用非常方便,而且上面有大量的镜像可以使用。 但是,每次都远程下载镜像速度非常慢,如果能在本地做一个 docker 的仓库,多人协作开发的时候更新效率就会提高很多。

所以,下面我们尝试搭建自己的 docker 仓库。

本地私有仓库

首先搭建一个只有自己本机能够使用的 docker 仓库,目的是为了尝试私有仓库的最简化方法。

  1. 安装 registry

    $ docker pull registry
    Using default tag: latest
    latest: Pulling from library/registry
    709515475419: Pull complete 
    df6e278d8f96: Pull complete 
    4b0b08c1b8f7: Pull complete 
    80119f43a01e: Pull complete 
    acf34ba23c50: Pull complete 
    Digest: sha256:412e3b6494f623a9f03f7f9f8b8118844deaecfea19e3a5f1ce54eed4f400296
    Status: Downloaded newer image for registry:latest
    
  2. 运行本地的 registry

    docker run -d -p 5000:5000 --restart=always --name registry 
        -v /your/path/to/registry-images:/var/lib/registry 
        registry:latest
    
  3. 将某个已有的 image 存入 registry

    docker tag jdeathe/centos-ssh:centos-7 localhost:5000/ssh:7     
    docker push localhost:5000/ssh:7
    
    • jdeathe/centos-ssh:centos-7 是已有的 image
    • localhost:5000/ssh:7 是准备提交到 本地 registry 的 image,这里 localhost:5000 是本地 registry 的
  4. pull 已经提交到 registry 的镜像

    docker pull localhost:5000/ssh:7
    

局域网私有仓库

如上,搭建一个只有本机访问的私有镜像非常简单,不用对已有的 docker 服务进行任何配置。 如果要搭建一个能在局域网内访问的 docker 仓库,就需要对 docker 服务进行一些简单的配置。

首先,在主机 A 上:

  1. 安装 registry 镜像(同上)

  2. 将某个 image 导入 registry,这里使用主机 A 的 IP 而不是 localhost

    docker tag jdeathe/centos-ssh:centos-7 192.168.0.111:5000/ssh:7     
    docker push 192.168.0.111:5000/ssh:7
    
  3. 修改主机 A 的 docker 配置(/etc/docker/daemon.json 文件不存在则直接创建)

    {
        "insecure-registries": ["192.168.0.111:5000"]
    }
    
  4. 重启 docker 服务

    sudo systemctl restart docker
    

在同一局域网中的另一台主机 B 上

  1. 修改 docker 配置(/etc/docker/daemon.json 文件不存在则直接创建),使 docker daemon 能连上私有的 registry

    {
        "registry-mirrors": ["192.168.0.111:5000"],
        "insecure-registries": ["192.168.0.111:5000"]
    }
    
  2. 重启 docker 服务

    sudo systemctl restart docker
    
  3. 下载主机 A 中仓库的镜像,在局域网中速度非常快,以后开发过程中做部署测试就非常方便了。

    docker pull 192.168.0.111:5000/ssh:7
    

总结

上面只是简单的搭建了私有仓库,主要用在开发和测试环境中,如果要在生产环境中搭建 docker 私有仓库的话, 就不能使用上面 insercure-registry 的方式,需要配置 tls 的证书和认证,确保私有仓库的安全性。

原文地址:https://www.cnblogs.com/wang_yb/p/6855415.html