搭建私人docker仓库harbor

前言

dockerhub虽然很好,但是对公司的一些集体项目,不方便放上去,而且传输拉取时间慢等因素,便想到搭建本地docker仓库。正好docker官方有docker-registry支持本地仓库服务,不过可惜,只有服务后端,没有开源的ui。

使用docker-registry

仓库服务

docker run -itd --rm -p 5000:5000 -v /opt/data/registry:/tmp/registry registry 开启docker服务,仓库服务运行在镜像里,并和宿主机通过5000联通

上传镜像

以alpine为例,
docker image ls 发现镜像列表里有个alpine,想要把他传到本地仓库里

docker tag ba58 localhost:5000/fwhezfwhez/alpine:latest 将alpine tag一下,tag的格式是:仓库url/用户名/镜像名:标签

docker push localhost:5000/fwhezfwhez/alpine 这样就推送完了

查询和拉取都得走api,也是蛮麻烦的。

web ui

选用了harbor,使用前需要确认以下条件:

  • linux
  • 40GB硬盘,双核,4GB MEM
  • 网络可达postgres数据库源(因为我在部署时,官方的postgres容器总是出问题,我定位不到呢)
  • 需要配置域名以及支持https(因为docker push走443)
  • 具备docker,docker-compose环境

下面就以上条件,列出我这里是怎么解决的。

  • 购买了一台腾讯云服务器,并成功登入控制台:
    https://console.cloud.tencent.com/cvm?backfrom=cvmdetai
    购买后设计到了一些控制台上面的基本操作,这些初次入云的同学,可以选择一天,或者一个星期,半个月等短周期购买体验。
    购买后需要解决以下:
  1. 添加sudo用户,当然也可以直接使用root进行所有操作.
  2. 在控制台里,增加安全组,暴露服务器端口(22:文件传输scp,80:http,443:https, 5432:postgres, 6379:redis),短周期购买的可以直接暴露所有端口。
  • 安装docker,docker-compose,redis,并通过docker快捷安装postgres
    docker: https://docs.docker.com/install/linux/docker-ce/centos/
    docker-compose: https://github.com/docker/compose/releases
    redis:
    yum install redis ,
    systemctl enable redis,
    systemctl start redis
    postgres:
    docker pull postgres:latest,
    docker run -tid --restart=on-failure:20 -p 5432:5432 postgres:latest
    docker exec -it <容器id> sh
    然后自行百度如何创建数据库,创建一个叫harbor的数据库作为数据源

  • 购买域名并添加解析,并申请免费的https证书
    域名: https://console.cloud.tencent.com/domain
    证书和解析: https://console.cloud.tencent.com/ssl
    得到证书以后,证书内的叫Nginx文件夹里面的2个文件(key,crt)通过scp进服务器
    scp -P 22 xxx.key xxx@host:/path
    scp -P 22 xxx.crt xxx@host:/path

  • 安装harbor并部署至该服务器上,并通过域名可达
    官方参考:https://github.com/goharbor/harbor/blob/master/docs/installation_guide.md
    大致上是,下载offline版的harbor压缩文件,通过scp传到服务器上,再tar解压,修改解压后的文件夹里的harbor.yml,下面是我作了修改后的配置(数据库的host:xx.xxx.xx.xx需要写成你们自己的,hostname也是, certification和private_key分别是之前scp过去的证书路径),供参考:

# Configuration file of Harbor

# The IP address or hostname to access admin UI and registry service.
# DO NOT use localhost or 127.0.0.1, because Harbor needs to be accessed by external clients.
hostname: ftcrone.com

# http related config
http:
  # port for http, default is 80. If https enabled, this port will redirect to https port
  port: 80

# https related config
https:
  # https port for harbor, default is 443
  port: 443
  # The path of cert and key files for nginx
  certificate: /crtpath
  private_key: /keypath

# Uncomment external_url if you want to enable external proxy
# And when it enabled the hostname will no longer used
# external_url: https://reg.mydomain.com:8433

# The initial password of Harbor admin
# It only works in first time to install harbor
# Remember Change the admin password from UI after launching Harbor.
harbor_admin_password: Harbor12345

# Harbor DB configuration
database:
  # The password for the root user of Harbor DB. Change this before any production use.
  password: Harbor12345

# The default data volume
data_volume: /data

# Harbor Storage settings by default is using /data dir on local filesystem
# Uncomment storage_service setting If you want to using external storage
# storage_service:
#   # ca_bundle is the path to the custom root ca certificate, which will be injected into the truststore
#   # of registry's and chart repository's containers.  This is usually needed when the user hosts a internal storage with self signed certificate.
#   ca_bundle:

#   # storage backend, default is filesystem, options include filesystem, azure, gcs, s3, swift and oss
#   # for more info about this configuration please refer https://docs.docker.com/registry/configuration/
#   filesystem:
#     maxthreads: 100
#   # set disable to true when you want to disable registry redirect
#   redirect:
#     disabled: false

# Clair configuration
clair: 
  # The interval of clair updaters, the unit is hour, set to 0 to disable the updaters.
  updaters_interval: 12

  # Config http proxy for Clair, e.g. http://my.proxy.com:3128
  # Clair doesn't need to connect to harbor internal components via http proxy.
  http_proxy:
  https_proxy:
  no_proxy: 127.0.0.1,localhost,core,registry

jobservice:
  # Maximum number of job workers in job service  
  max_job_workers: 10

chart:
  # Change the value of absolute_url to enabled can enable absolute url in chart
  absolute_url: disabled

# Log configurations
log:
  # options are debug, info, warning, error, fatal
  level: info
  # Log files are rotated log_rotate_count times before being removed. If count is 0, old versions are removed rather than rotated.
  rotate_count: 50
  # Log files are rotated only if they grow bigger than log_rotate_size bytes. If size is followed by k, the size is assumed to be in kilobytes. 
  # If the M is used, the size is in megabytes, and if G is used, the size is in gigabytes. So size 100, size 100k, size 100M and size 100G 
  # are all valid.
  rotate_size: 200M
  # The directory on your host that store log
  location: /var/log/harbor

#This attribute is for migrator to detect the version of the .cfg file, DO NOT MODIFY!
_version: 1.8.0

# Uncomment external_database if using external database.
external_database:
  harbor:
    host: xxx.xx.xx.xx
    port: 5432
    db_name: harbor
    username: postgres
    password:
    ssl_mode: disable
  clair:
    host: xx.xx.xx.xx
    port: 5432
    db_name: harbor
    username: postgres
    password: 
    ssl_mode: disable
  notary_signer:
    host: xx.xx.xx.xx
    port: 5432
    db_name: harbor
    username: postgres
    password: 
    ssl_mode: disable
  notary_server:
    host: xx.xx.xx.xx
    port: 5432
    db_name: harbor
    username: postgres
    password: 
    ssl_mode: disable

# Uncomment external_redis if using external Redis server
external_redis:
  host: redis
  port: 6379
  password: xxxx
#   # db_index 0 is for core, it's unchangeable
#   registry_db_index: 1
#   jobservice_db_index: 2
#   chartmuseum_db_index: 3

# Uncomment uaa for trusting the certificate of uaa instance that is hosted via self-signed cert.
# uaa:
#   ca_file: /path/to/ca

在同级harbor.yml,执行 install.sh, ./install.sh,结束。

可以体验一下:
https://ftcrone.com
不保证一直开放,可以去体验一下:
注册一个账号,添加一个项目,傻瓜操作,这个不示范。

  • 推送一个镜像进去
    docker login ftcrone.com 用注册的账户登陆本harbor
    docker tag alpine:latest ftcrone.com/zonst/alpine:latest 标签一下本地某个镜像
    docker push ftcrone.com/zonst/alpine 推过去
    在这里插入图片描述
    搭建私人docker仓库harbor_第1张图片

你可能感兴趣的:(搭建私人docker仓库harbor)