公共仓库

registry私有仓库

  • 下载官方提供的registry镜像来搭建本地私有仓库,推荐放在其他服务器上

    1
    [root@node1 docker]# docker run -d -p 5000:5000 --name registry registry:2
  • 该命令自动,默认情况下创建在容器的/var/lib/registry目录下,可以通过-v来指定路径

    1
    2
    [root@node1 docker]# mkdir -p /registry
    [root@node1 docker]# docker run -d -p 5000:5000 --name registry -v /registry:/var/lib/registry registry:2
  • 查看仓库运行情况 http://127.0.0.1:5000/v2/_catalog

    1
    2
    3
    4
    {
    # 表示现在仓库中,没有镜像images
    "repositories": []
    }

使用

  • 上传镜像

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    # 将该镜像修改tag
    [root@node1 docker]# docker tag hello-world:latest 127.0.0.1:5000/test
    # 上传标记镜像

    [root@node1 docker]# docker push 127.0.0.1:5000/test
    Using default tag: latest
    The push refers to repository [127.0.0.1:5000/test]
    e07ee1baac5f: Pushed
    latest: digest: sha256:f54a58bc1aac5ea1a25d796ae155dc228b3f0e11d046ae276b39c4bf2f13d8c4 size: 525

  • 成功会出现上述提示,表示本地的仓库默认使用的是https进行上传,那行是latest是重新上传出现的。

  • 如果你在push镜像的时候出现问题,可能是因为我们启动的registry服务不是安全可信赖的

    • docker客户端可以在配置中添加了insecure-registary配置,就不需要在docker 客户端配置上对应证书

    • 修改配置文件 vim /etc/docker/daemon.json, 再重启docker 服务

      1
      2
      3
      4
      5
      6
      7
      8
      {
      "registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"],
      "insecure-registries": ["127.0.0.1:5000"],
      "live-restore": true
      }

      # 再重启docker 服务
      systemctl restart docker
  • 删除客户端镜像,从本地仓库下载镜像

    1
    2
    [root@node1 docker]# docker rmi 127.0.0.1:5000/test
    [root@node1 docker]# docker pull 127.0.0.1:5000/test

管理register

  • 删除镜像仓库镜像

    1
    2
    3
    4
    5
    # 打开镜像的存储目录,如有-V操作打开挂载目录也可以,删除镜像文件夹
    # docker exec <容器名>
    [root@node1 docker]# docker exec -it registry /bin/sh

    # rm -rf /var/lib/registry/docker/registry/v2/repositories/<镜像名>
  • 执行垃圾回收操作,注意2.4版本以上的registry才有此功能

    1
    2
    [root@node1 docker]# docker exec -it registry /bin/sh
    # /bin/registry garbage-collect /etc/docker/registry/config.yml

Harbor私有镜像仓库

安装

  • 准备

  • 安装

    1
    2
    [root@node1 docker]# wget https://github.com/vmware/harbor/releases/download/v1.8.6/harbor-offline-installer-v1.8.6.tgz
    [root@harbor ~]# tar -xvf harbor-offline-installer-v1.8.6.tgz

配置

  • 修改harbor.yml配置文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    # cp harbor.yml.tmpl  harbor.yml
    # mkdir -p /opt/application/harbor //用于存放harbor的持久化数据
    harbor.yml配置文件主要修改参数如下:
    hostname: 192.168.0.8:9999 //设置访问地址,可以使用ip、域名,不可以设置为127.0.0.1或localhost。默认情况下,harbor使用的端口是80,若使用自定义的端口,除了要改docker-compose.yml文件中的配置外,这里的hostname也要加上自定义的端口,否则在docker login、push时会报错
    #http配置
    http:
    # port for http, default is 80. If https enabled, this port will redirect to https port
    port: 9999

    #https配置(如不需要可不配置,注释掉)
    # https related config
    #https:
    # https port for harbor, default is 443
    #port: 443
    # The path of cert and key files for nginx
    #certificate: /your/certificate/path
    #private_key: /your/private/key/path

    #external_url: https://reg.mydomain.com:8433 //如果要启用外部代理,比如外层的NGINX、LB等,请取消注释external_url,当它启用时,hostname将不再使用。

    harbor_admin_password: Harbor12345 //admin密码



    #数据库配置
    database:
    # The password for the root user of Harbor DB. Change this before any production use.
    password: root123
    # The maximum number of connections in the idle connection pool. If it <=0, no idle connections are retained.
    max_idle_conns: 50
    # The maximum number of open connections to the database. If it <= 0, then there is no limit on the number of open connections.
    # Note: the default number of connections is 100 for postgres.
    max_open_conns: 100


    #持久化数据目录

    data_volume: /opt/application/harbor

    ……

启动

  • 安装并启动Harbor

    1
    2
    # cd harbor
    ./install.sh
  • 安装完成后查看下正在运行的docker容器:

    1
    docker ps
  • http://127.0.0.1:9999 admin/Harbor12345

创建systemd服务

  • 创建systemd服务管理脚本

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    [Unit]
    Description=Harbor
    After=docker.service systemd-networkd.service systemd-resolved.service
    Requires=docker.service
    Documentation=http://github.com/vmware/harbor

    [Service]
    Type=simple
    Restart=on-failure
    RestartSec=5
    ExecStart=/usr/local/bin/docker-compose -f /opt/harbor/docker-compose.yml up
    ExecReload=/usr/local/bin/docker-compose -f /opt/harbor/docker-compose.yml restart
    ExecStop=/usr/local/bin/docker-compose -f /opt/harbor/docker-compose.yml down

    [Install]
    WantedBy=multi-user.target

Harbor的使用

  • 登录harbor

    1
    2
    3
    4
    # docker login 192.18.0.8:9999
    Username: admin
    Password:
    Error response from daemon: Get https://127.0.0.1:9999/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
  • 报错原因: Docker自从1.3.X之后docker registry交互默认使用的是HTTPS,但是我们搭建私有镜像默认使用的是HTTP服务,所以与私有镜像交时出现以上错误。

  • 报错解决: 修改Docker的配置文件/etc/docker/daemon.json :

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    # vim /etc/docker/daemon.json
    {
    "registry-mirrors": ["https://k728i8z5.mirror.aliyuncs.com"],
    "insecure-registries": ["127.0.0.1:5000"],
    "insecure-registries": ["127.0.0.1:9999"]
    }

    # 重新启动Docker:
    systemctl daemon-reload
    systemctl restart docker
  • 在Harbor上创建新项目供上传使用

    1
    2
    3
    [root@master ~]# docker tag hello-world 127.0.0.1/library/hello-world:v1
    [root@master ~]# docker push 192.168.10.103/library/hello