zoukankan      html  css  js  c++  java
  • Docker基础内容之仓库

    前言

    Docker提供了开放的中央仓库dockerhub,同时也允许我们使用registry搭建本地私有仓库。搭建私有仓库有如下的优点:

    • 节省网络带宽,提升Docker部署速度,不用每个镜像从DockerHub上去下载,只需从私有仓库下载就可;
    • 私有镜像,包含公司敏感信息,不方便公开对外,只在公司内部使用。

    私有仓库基本部署

    部署步骤

    下载镜像仓库

      要想部署docker私有仓库必须使用官方给定的镜像来进行构造。

    docker pull registry

    创建容器

    docker run -d -p 5000:5000 --restart=always --name=registry-srv -v /mydata/dockerRegistry:/var/lib/registry registry
    
    -d:后台运行
    -p:将容器的5000端口映射到宿主机的5000端口--name:容器的名称
    -v:将容器内的/var/lib/registry映射到宿主机的/mydata/dockerRegistry目录,默认情况下,会将仓库存放于容器的/tmp/registry目录下;

    搭建容器相关的web服务

    docker pull hyper/docker-registry-web
    docker run -it -p 8080:8080 --restart=always --name registry-web --link registry-srv -e REGISTRY_URL=http://registry-srv:5000/v2 -e REGISTRY_NAME=localhost:5000 hyper/docker-registry-web
    
    -it: 以交互模式运行
    --link:链接其它容器(registry-srv),在此容器中,使用registry-srv等同于registry-srv容器的局域网地址
    -e:设置环境变量

    上传测试

    docker push 192.168.1.87:5000/cbbing/hcharts

    下载测试

    docker pull 192.168.1.87:5000/hcharts

    注意

    如果你不想使用 127.0.0.1:5000 作为仓库地址,比如想让本网段的其他主机也能把镜像推送到私有仓库。你就得把例如 192.168.199.100:5000 这样的内网地址作为私有仓库地址,这时你会发现无法成功推送镜像。

    这是因为 Docker 默认不允许非 HTTPS 方式推送镜像。我们可以通过 Docker 的配置选项来取消这个限制,或者查看下一节配置能够通过 HTTPS 访问的私有仓库。

    对于使用 systemd 的系统,请在 /etc/docker/daemon.json 中写入如下内容(如果文件不存在请新建该文件);修改完配置之后记得重启docker。

    {
      "registry-mirror": [
        "https://registry.docker-cn.com"
      ],
      "insecure-registries": [
        "192.168.199.100:5000"
      ]
    }

    web端查看私有仓库镜像

      访问:http://192.168.1.87:8080/,网页上呈现:

    私有仓库高级部署

    环境准备

      新建一个文件夹,以下步骤均在该文件夹中进行

    [root@host-3 ~]# mkdir /opt/ssl
    [root@host-3 ssl]# cd /opt/ssl/

    准备站点证书

      这里假设我们将要搭建的私有仓库地址为 docker.domain.com,下面我们介绍使用 openssl 自行签发 docker.domain.com 的站点 SSL 证书。

    创建 CA 私钥

    [root@host-3 ssl]# openssl genrsa -out "root-ca.key" 4096
    Generating RSA private key, 4096 bit long modulus
    ...........................++
    ................++
    e is 65537 (0x10001)

    创建 CA 根证书请求文件 

    [root@host-3 ssl]# openssl req -new -key "root-ca.key" -out "root-ca.csr" -sha256 -subj '/C=CN/ST=beijing/L=chaoyang/O=emaxdata/CN=docker.domain.com'
    # 以上命令中 -subj 参数里的 /C 表示国家,如 CN;/ST 表示省;/L 表示城市或者地区;/O 表示组织名;/CN 通用名称。

    配置 CA 根证书

    [root@host-3 ssl]# touch root-ca.cnf
    [root@host-3 ssl]# vi root-ca.cnf
    [root_ca]
    basicConstraints = critical,CA:TRUE,pathlen:1
    keyUsage = critical, nonRepudiation, cRLSign, keyCertSign
    subjectKeyIdentifier=hash

    签发根证书

    [root@host-3 ssl]# openssl x509 -req  -days 3650  -in "root-ca.csr" -signkey "root-ca.key" -sha256 -out "root-ca.crt" -extfile "root-ca.cnf" -extensions root_ca
    Signature ok
    subject=/C=CN/ST=beijing/L=chaoyang/O=emaxdata/CN=docker.domain.com
    Getting Private key

    生成站点 SSL 私钥

    [root@host-3 ssl]# openssl genrsa -out "docker.domain.com.key" 4096
    Generating RSA private key, 4096 bit long modulus
    ................................................................................................++
    .......................................................................................................................................++
    e is 65537 (0x10001)

    生成证书请求文件

    [root@host-3 ssl]# openssl req -new -key "docker.domain.com.key" -out "site.csr" -sha256 -subj '/C=CN/ST=beijing/L=chaoyang/O=emaxdata/CN=docker.domain.com'

    配置证书

    [root@host-3 ssl]# touch site.cnf
    [root@host-3 ssl]# vi site.cnf
    
    [server]
    authorityKeyIdentifier=keyid,issuer
    basicConstraints = critical,CA:FALSE
    extendedKeyUsage=serverAuth
    keyUsage = critical, digitalSignature, keyEncipherment
    subjectAltName = DNS:docker.domain.com, IP:127.0.0.1
    subjectKeyIdentifier=hash

    签署站点 SSL 证书

    root@host-3 ssl]# openssl x509 -req -days 750 -in "site.csr" -sha256 -CA "root-ca.crt" -CAkey "root-ca.key"  -CAcreateserial -out "docker.domain.com.crt" -extfile "site.cnf" -extensions server
    Signature ok
    subject=/C=CN/ST=beijing/L=chaoyang/O=emaxdata/CN=docker.domain.com
    Getting CA Private Key
    
    # 这样已经拥有了 docker.domain.com 的网站 SSL 私钥 docker.domain.com.key 和 SSL 证书 docker.domain.com.crt 及 CA 根证书 root-ca.crt。保留docker.domain.com.key docker.domain.com.crt root-ca.crt这三个文件,删除其他文件

    将证书相关内容迁移到docker配置中

    [root@host-3 ssl]# mkdir -p /etc/docker/registry
    [root@host-3 ssl]# cd /etc/docker/registry/
    [root@host-3 registry]# mv /opt/ssl /etc/docker/registry/

    配置私有仓库信息

    [root@host-3 registry]# vi /etc/docker/registry/config.yml
    version: 0.1
    log:
      accesslog:
        disabled: true
      level: debug
      formatter: text
      fields:
        service: registry
        environment: staging
    storage:
      delete:
        enabled: true
      cache:
        blobdescriptor: inmemory
      filesystem:
        rootdirectory: /var/lib/registry
    auth:
      htpasswd:
        realm: basic-realm
        path: /etc/docker/registry/auth/nginx.htpasswd
    http:
      addr: :443
      host: https://docker.domain.com
      headers:
        X-Content-Type-Options: [nosniff]
      http2:
        disabled: false
      tls:
        certificate: /etc/docker/registry/ssl/docker.domain.com.crt
        key: /etc/docker/registry/ssl/docker.domain.com.key
    health:
      storagedriver:
        enabled: true
        interval: 10s
    threshold: 3

    生成http认证文件

    [root@host-3 registry]# mkdir -p /etc/docker/registry/auth/
    [root@host-3 registry]# docker run --rm --entrypoint htpasswd registry -Bbn admin admin > /etc/docker/registry/auth/nginx.htpasswd

    生成compose文件

    [root@host-3 registry]# vi  /etc/docker/registry/docker-compose.yml
    version: '3'
    
    services:
      registry:
        image: registry
        ports:
          - "443:443"
        volumes:
          - ./:/etc/docker/registry
          - registry-data:/var/lib/registry     # 这里的存储路劲就是容器存储中映射到宿主机的挂载点,挂载点在/var/lib/docker下;详情请参考volume的使用
    
    volumes:
      registry-data:

    修改hosts

    vi /etc/hosts
    127.0.0.1 docker.domain.com

    启动容器

    [root@host-3 registry]# cd /etc/docker/registry/
    [root@host-3 registry]# curl -L https://github.com/docker/compose/releases/download/1.24.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
    [root@host-3 registry]# chmod a+x /usr/local/bin/docker-compose
    [root@host-3 registry]# docker-compose up -d

    这样我们就搭建好了一个具有权限认证、TLS 的私有仓库,接下来我们测试其功能是否正常。

    测试功能

      由于自行签发的 CA 根证书不被系统信任,所以我们需要将 CA 根证书 ssl/root-ca.crt 移入 /etc/docker/certs.d/docker.domain.com 文件夹中。

    [root@host-3 registry]# mkdir -p /etc/docker/certs.d/docker.domain.com
    [root@host-3 registry]# cp /etc/docker/registry/ssl/root-ca.crt /etc/docker/certs.d/docker.domain.com/ca.crt

    登录私有仓库

    docker login docker.domain.com          # 输入上面创建的用户与密码

    尝试推送、拉取镜像

    [root@host-3 centos]# docker tag centos:latest docker.domain.com/admin/centos:latest
    [root@host-3 docker]# docker push docker.domain.com/admin/centos
    [root@host-3 centos]# docker rmi -f 9f38484d220f
    [root@host-3 centos]# docker pull docker.domain.com/admin/centos

    文章来自转载:https://yeasy.gitbooks.io/docker_practice/content/repository/registry_auth.html

  • 相关阅读:
    [codevs]失恋28天题目系列
    [NOIP1998]最大数
    [codevs4247]奇特的生物
    [codevs1380]没有上司的舞会
    [codevs2152]滑雪
    [codevs2171]棋盘覆盖
    [codevs2170]悠闲的漫步
    [codevs1557]热浪
    [codevs1554]最佳课题选择
    nodejs建站+github page 建站问题总结
  • 原文地址:https://www.cnblogs.com/guge-94/p/11066332.html
Copyright © 2011-2022 走看看