1. Docker Registry 分类

Docker Registry 有两部分组成

  • Repostitory

    • 由特定的一组镜像组成的仓库称为 Repostitory
    • 一个 Docker Registry 可以包含多个 Repostitory
    • Repostitory 可以分为两个大类


      • 官方的仓库,格式 仓库名:标签,如 nginx:latest
      • 其它类,如用户仓库、第三方仓库、私有仓库等等

    • 一个镜像可以有多个标签
  • Index

提供用户认证、镜像检索功能

2. docker-registry 安装使用

docker-registry 是 Docker 官方提供的镜像仓库管理软件,可以提供 docker pull 和 docker push 等操作,但是没有web 界面,镜像管理非常不方便,一般不作为私有仓库使用。

[root@centos-82 ~]# yum install -y docker-registry ## 实际安装软件包是 docker distribution

[root@centos-82 ~]# rpm -ql docker-distribution

  1. /etc/docker-distribution/registry/config.yml ## Configuration
  2. /usr/bin/registry
  3. /usr/lib/systemd/system/docker-distribution.service ## Service
  4. ......

[root@centos-82 ~]# vim /etc/docker-distribution/registry/config.yml ## 配置端口和镜像存放目录

  1. version: 0.1
  2. log:
  3. fields:
  4. service: registry
  5. storage:
  6. cache:
  7. layerinfo: inmemory
  8. filesystem:
  9. rootdirectory: /data/docker-registry
  10. http:
  11. addr: :5000

[root@centos-82 ~]# mkdir /data/docker-registry

[root@centos-82 ~]# systemctl start docker-distribution.service

[root@centos-82 ~]# ss -lntp | grep 5000

  1. LISTEN 0 128 :::5000 :::* users:(("registry",pid=9789,fd=3)

[root@centos-81 ~]# docker tag httpd:v0.2 hub.docker.reg:5000/httpd:v0.2 ## /etc/hosts 配置域名映射

[root@centos-81 ~]# docker push hub.docker.reg:5000/httpd:v0.2 ## docker 默认使用https协议

  1. The push refers to a repository [hub.docker.reg:5000/httpd]
  2. Get https://hub.docker.reg:5000/v1/_ping: http: server gave HTTP response to HTTPS client

[root@centos-81 ~]# vim /etc/docker/daemon.json ##添加http协议的registry

  1. {
  2. "registry-mirrors":["https://registry.docker-cn.com"],
  3. "insecure-registries":["hub.docker.reg:5000"]
  4. }

[root@centos-81 ~]# systemctl restart docker

[root@centos-81 ~]# docker push hub.docker.reg:5000/httpd:v0.2

[root@centos-81 ~]# curl -s http://192.168.1.82:5000/v2/httpd/tags/list | python -mjson.tool ## 根据仓库名获取tags

  1. {
  2. "name": "httpd",
  3. "tags": [
  4. "v0.2",
  5. "v0.1"
  6. ]
  7. }

[root@centos-82 ~]# tree /data/docker-registry/

  1. /data/docker-registry/
  2. └── docker
  3. └── registry
  4. └── v2
  5. ├── blobs
  6. └── sha256
  7. ├── 3f
  8. └── 3f22152f75b71784bfb6946248858ace5fad0e1a0db4a208f20607d88810e60c
  9. └── data
  10. ├── 6d
  11. └── 6df52055d83b6b866af9d4907574421aedfdf3f8c27c1ba45e7d1c9236000f80
  12. └── data
  13. └── 95
  14. └── 95090e1ca932d43e6791e981e310d67d50af9ef7fe4ec1fda859064e6533a21c
  15. └── data
  16. └── repositories
  17. └── httpd
  18. ├── _layers
  19. └── sha256
  20. ├── 3f22152f75b71784bfb6946248858ace5fad0e1a0db4a208f20607d88810e60c
  21. └── link
  22. └── 6df52055d83b6b866af9d4907574421aedfdf3f8c27c1ba45e7d1c9236000f80
  23. └── link
  24. ├── _manifests
  25. ├── revisions
  26. └── sha256
  27. └── 95090e1ca932d43e6791e981e310d67d50af9ef7fe4ec1fda859064e6533a21c
  28. └── link
  29. └── tags
  30. └── v0.2
  31. ├── current
  32. └── link
  33. └── index
  34. └── sha256
  35. └── 95090e1ca932d43e6791e981e310d67d50af9ef7fe4ec1fda859064e6533a21c
  36. └── link
  37. └── _uploads

[root@centos-81 ~]# docker image ls | grep hub

[root@centos-81 ~]# docker pull hub.docker.reg:5000/httpd:v0.1 ## 从私有registry拉取镜像

  1. Trying to pull repository hub.docker.reg:5000/httpd ...
  2. v0.1: Pulling from hub.docker.reg:5000/httpd
  3. Digest: sha256:ec0aa9e4aff0ab1a001f87277324df24281a00513637f0c8045c59c2667f3eb8
  4. Status: Downloaded newer image for hub.docker.reg:5000/httpd:v0.1

[root@centos-81 ~]# docker image ls | grep hub

  1. hub.docker.reg:5000/httpd v0.1 562ec613ec3a 4 weeks ago 1.2 MB

3. Harbor 安装使用

3.1. Harbor 介绍

Docker-distribution 虽然能实现私有镜像仓库,但是管理复杂,且没有web界面,不支持搜索等。VMware基于docker distribution二次开发了Harbor,实现了web界面管理仓库,功能性极大的增强。Harbor 的特性:

  • 基于多用户,多项目的访问控制
  • 镜像复制
  • 可以在WEB界面管理镜像仓库,并且支持中文
  • 日志审计

3.2. Harbor 安装

3.2.1. 依赖

Resource Capacity Description
CPU minimal 2 CPU 4 CPU is preferred
Mem minimal 4GB 8GB is preferred
Disk minimal 40GB 160GB is preferred
Software Version Description
Python version 2.7 or higher Note that you may have to install Python on Linux distributions (Gentoo, Arch) that do not come with a Python interpreter installed by default
Docker engine version 1.10 or higher For installation instructions, please refer to: https://docs.docker.com/engine/installation/
Docker Compose version 1.6.0 or higher For installation instructions, please refer to: https://docs.docker.com/compose/install/
Openssl latest is preferred Generate certificate and keys for Harbor
Port Protocol Description
443 HTTPS Harbor portal and core API will accept requests on this port for https protocol
4443 HTTPS Connections to the Docker Content Trust service for Harbor, only needed when Notary is enabled
80 HTTP Harbor portal and core API will accept requests on this port for http protocol

3.2.2. 安装和配置

[root@centos-82 ~]# wget https://storage.googleapis.com/harbor-releases/release-1.7.0/harbor-offline-installer-v1.7.4.tgz

[root@centos-82 ~]# tar -xf harbor-offline-installer-v1.7.4.tgz -C /usr/local/ && cd /usr/local/harbor

[root@centos-82 harbor]# readlink -f docker-compose.yml ## 镜像配置文件,一般只需要配置路径即可

  1. /usr/local/harbor/docker-compose.yml

[root@centos-82 harbor]# grep -Ev “^$|#” harbor.cfg

  1. _version = 1.7.0
  2. hostname = hub.docker.reg ## docker推拉镜像时需要指定的服务端地址
  3. ui_url_protocol = http ## 使用的协议
  4. max_job_workers = 6 ## 最大工作进程数
  5. customize_crt = on ## ssl密钥相关,仅在https模式下生效
  6. ssl_cert = /data/cert/server.crt
  7. ssl_cert_key = /data/cert/server.key
  8. secretkey_path = /data
  9. admiral_url = NA
  10. log_rotate_count = 50 ## 日志切割配置
  11. log_rotate_size = 200M
  12. http_proxy =
  13. https_proxy =
  14. no_proxy = 127.0.0.1,localhost,core,registry
  15. email_identity = ## Email配置项
  16. email_server = smtp.mydomain.com
  17. email_server_port = 25
  18. email_username = sample_admin@mydomain.com
  19. email_password = abc
  20. email_from = admin <sample_admin@mydomain.com>
  21. email_ssl = false
  22. email_insecure = false
  23. harbor_admin_password = Harbor12345 ## admin初始密码,可登陆到WEB页面修改
  24. auth_mode = db_auth
  25. ldap_url = ldaps://ldap.mydomain.com ## 若关联ldap可以配置该项目
  26. ldap_basedn = ou=people,dc=mydomain,dc=com
  27. ldap_uid = uid
  28. ldap_scope = 2
  29. ldap_timeout = 5
  30. ldap_verify_cert = true
  31. ldap_group_basedn = ou=group,dc=mydomain,dc=com
  32. ldap_group_filter = objectclass=group
  33. ldap_group_gid = cn
  34. ldap_group_scope = 2
  35. self_registration = on
  36. token_expiration = 30
  37. project_creation_restriction = everyone
  38. db_host = postgresql
  39. db_password = Database.harbor.123 ## 数据库密码
  40. db_port = 5432
  41. db_user = postgres
  42. redis_host = redis
  43. redis_port = 6379
  44. redis_password =
  45. redis_db_index = 1,2,3
  46. clair_db_host = postgresql
  47. clair_db_password = root123
  48. clair_db_port = 5432
  49. clair_db_username = postgres
  50. clair_db = postgres
  51. clair_updaters_interval = 12
  52. uaa_endpoint = uaa.mydomain.org
  53. uaa_clientid = id
  54. uaa_clientsecret = secret
  55. uaa_verify_cert = true
  56. uaa_ca_cert = /path/to/ca.pem
  57. registry_storage_provider_name = filesystem
  58. registry_storage_provider_config =
  59. registry_custom_ca_bundle =

[root@centos-82 harbor]# ./install.sh ## 执行安装脚本,依赖(docker-compose,epel源)

[root@centos-82 harbor]# docker image ls ## 涉及到的镜像

  1. REPOSITORY TAG IMAGE ID CREATED SIZE
  2. goharbor/chartmuseum-photon v0.8.1-v1.7.4 7e2272c02339 3 weeks ago 113MB
  3. goharbor/harbor-migrator v1.7.4 968c31d07d2f 3 weeks ago 678MB
  4. goharbor/redis-photon v1.7.4 611d1ead0a28 3 weeks ago 99.7MB
  5. goharbor/clair-photon v2.0.7-v1.7.4 01090529ab14 3 weeks ago 165MB
  6. goharbor/notary-server-photon v0.6.1-v1.7.4 737518b1b943 3 weeks ago 135MB
  7. goharbor/notary-signer-photon v0.6.1-v1.7.4 495dc3326120 3 weeks ago 132MB
  8. goharbor/harbor-registryctl v1.7.4 723aed7bbf8d 3 weeks ago 102MB
  9. goharbor/registry-photon v2.6.2-v1.7.4 f4743bd7b0d9 3 weeks ago 86.7MB
  10. goharbor/nginx-photon v1.7.4 dda34e6afafe 3 weeks ago 35.9MB
  11. goharbor/harbor-log v1.7.4 bf4916eef530 3 weeks ago 81.4MB
  12. goharbor/harbor-jobservice v1.7.4 1b6a0445ae9c 3 weeks ago 84.1MB
  13. goharbor/harbor-core v1.7.4 e603b8750d26 3 weeks ago 95.6MB
  14. goharbor/harbor-portal v1.7.4 2ca1d845cafa 3 weeks ago 40.6MB
  15. goharbor/harbor-adminserver v1.7.4 5706c65d65dc 3 weeks ago 72.3MB
  16. goharbor/harbor-db v1.7.4 08d163f732f3 3 weeks ago 136MB

[root@centos-82 harbor]# docker container ls

  1. CONTAINER ID IMAGE COMMAND ......
  2. d5c7eeca50fb goharbor/nginx-photon:v1.7.4 "nginx -g 'daemon of…" ......
  3. 6ec59a513f9a goharbor/harbor-portal:v1.7.4 "nginx -g 'daemon of…" ......
  4. 201267927c06 goharbor/harbor-jobservice:v1.7.4 "/harbor/start.sh" ......
  5. 3711d9548a8b goharbor/harbor-core:v1.7.4 "/harbor/start.sh" ......
  6. 1c092514ca13 goharbor/harbor-registryctl:v1.7.4 "/harbor/start.sh" ......
  7. 264c26f4d27c goharbor/harbor-adminserver:v1.7.4 "/harbor/start.sh" ......
  8. 527ceb179532 goharbor/registry-photon:v2.6.2-v1.7.4 "/entrypoint.sh /etc…" ......
  9. 127009add4f7 goharbor/harbor-db:v1.7.4 "/entrypoint.sh post…" ......
  10. ca8dcdd36bc1 goharbor/redis-photon:v1.7.4 "docker-entrypoint.s…" ......
  11. a850c81e3abb goharbor/harbor-log:v1.7.4 "/bin/sh -c /usr/loc…" ......

[root@centos-82 harbor]# netstat -lntp

  1. Active Internet connections (only servers)
  2. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
  3. tcp 0 0 127.0.0.1:1514 0.0.0.0:* LISTEN 11409/docker-proxy
  4. tcp6 0 0 :::80 :::* LISTEN 12326/docker-proxy
  5. tcp6 0 0 :::443 :::* LISTEN 12312/docker-proxy
  6. tcp6 0 0 :::4443 :::* LISTEN 12299/docker-proxy

[root@centos-82 ~]# cat /etc/docker/daemon.json

  1. {
  2. "registry-mirrors":["http://hub-mirror.c.163.com","https://registry.docker-cn.com"],
  3. "insecure-registries":["hub.docker.reg"]
  4. }

[root@centos-82 ~]# cat /etc/hosts

  1. 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
  2. ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
  3. 192.168.1.82 hub.docker.reg

3.2.3. 启停Harbor

  • 停止Harbor

[root@centos-82 ~]# cd /usr/local/harbor/ ## change woker directory to harbor root directory.

[root@centos-82 harbor]# docker-compose stop

  1. Stopping nginx ... done
  2. Stopping harbor-portal ... done
  3. Stopping harbor-jobservice ... done
  4. Stopping harbor-core ... done
  5. Stopping registryctl ... done
  6. Stopping harbor-adminserver ... done
  7. Stopping registry ... done
  8. Stopping harbor-db ... done
  9. Stopping redis ... done
  10. Stopping harbor-log ... done
  • 启动Harbor

[root@centos-82 ~]# cd /usr/local/harbor/

[root@centos-82 harbor]# docker-compose start

  1. Starting log ... done
  2. Starting registry ... done
  3. Starting adminserver ... done
  4. Starting core ... done
  5. Starting redis ... done
  6. Starting jobservice ... done
  7. Starting registryctl ... done
  8. Starting postgresql ... done
  9. Starting portal ... done
  10. Starting proxy ... done

3.3. Harbor 使用

registry - 图1

registry - 图2

[root@centos-81 ~]# docker tag nginx:latest hub.docker.reg/devops/nginx:latest

[root@centos-81 ~]# docker login -u admin hub.docker.reg ## Login

  1. Password:
  2. Login Succeeded

[root@centos-81 ~]# docker push hub.docker.reg/devops/nginx:latest ## Docker push

  1. The push refers to a repository [hub.docker.reg/devops/nginx]
  2. 6b5e2ed60418: Pushed
  3. 92c15149e23b: Pushed
  4. 0a07e81f5da3: Pushed
  5. latest: digest: sha256:5b49c8e2c890fbb0a35f6050ed3c5109c5bb47b9e774264f4f3aa85bb69e2033 size: 948

[root@centos-81 ~]# docker logout hub.docker.reg ## Logout

[root@centos-81 ~]# docker image pull hub.docker.reg/devops/nginx:latest ## Docker pull

  1. Trying to pull repository hub.docker.reg/devops/nginx ...
  2. latest: Pulling from hub.docker.reg/devops/nginx
  3. Digest: sha256:5b49c8e2c890fbb0a35f6050ed3c5109c5bb47b9e774264f4f3aa85bb69e2033
  4. Status: Downloaded newer image for hub.docker.reg/devops/nginx:latest

[root@centos-81 ~]# docker image ls | grep hub.docker.reg

  1. hub.docker.reg/devops/nginx latest f09fe80eb0e7 7 weeks ago 109 MB