Docker Registry服务器部署配置

Docker Registry服务器部署非常简单,只需要合并运行镜像即可,由于Docker Registry服务器整合很多服务,配置的参数非常多,包括:存储,Redis,Auth,日志,中间件,监控,HTTP,通知,健康检查,代理,兼容性

4 min read
By myfreax
Docker Registry服务器部署配置

Docker Registry服务器部署非常简单,只需要合并运行镜像即可,由于Docker Registry服务器整合很多服务,配置的参数非常多,包括:存储,Redis,Auth,日志,中间件,监控,HTTP,通知,健康检查,代理,兼容性

部署Registry服务器

docker run -d -p 5000:5000 --restart=always --name registry \
  -v `pwd`/data:/var/lib/registry \
  registry:2

部署服务器非常简单,只需要一条命令,映射5000端口到容器,restart机制为总是,并分配当前目录下的data目录作为容器卷,存储镜像数据,你可以配置HTTPS方式的访问,如果需要配置TSL请参考Docker官方文档,如果是在局域网内访问这个方式即可满足需求

测试Registry服务

在测试之前必须要理解镜像的命名,理解下面的两条命令

docker pull ubuntu 

这个命令是从Docker官方的hub拉取ubuntu镜像,这是简写的方式,完整的命令是这样的docker pull docker.io/library/ubuntu

docker pull localhost:5000/foo/bar

这个命令是从localhost:5000拉取foo/bar镜像,接下来尝试从Docker Hub拉取镜像并推送到本地的Registry服务器,以Ubuntu为例

从Docker Hub拉取Ubuntu镜像并命名为localhost:5000/ubuntu

docker pull ubuntu && docker tag ubuntu localhost:5000/ubuntu

推送镜像到本地的Registry服务器

docker push localhost:5000/ubuntu

删除宿主机的localhost:5000/ubuntu并拉取Registry服务器的ubuntu镜像

docker rmi -f localhost:5000/ubuntu
docker pull localhost:5000/ubuntu

配置Registry服务器

配置Registry服务器的方式有两种,一种是在运行容器指定环境变量重写配置文件,另一种是直接映射yaml配置文件,建议使用配置文件的方式,这样迁移方便

环境变量重写配置选项

比如重写配置文件存储选项,文件配置如下所示

storage:
  filesystem:
    rootdirectory: /var/lib/registry

那么运行Registery容器时就需要指定环境变量REGISTRY_STORAGE_FILESYSTEM_ROOTDIRECTORY=/somewhere

docker run -d -p 5000:5000 --restart=always --name registry \
  -v `pwd`/data:/var/lib/registry \
  -e  REGISTRY_STORAGE_FILESYSTEM_ROOTDIRECTORY=/somewhere \
  registry:2

覆盖配置文件

可以使用挂载卷的方式覆盖配置,个人建议使用这种方式配置

docker run -d -p 5000:5000 --restart=always --name registry \
             -v `pwd`/config.yml:/etc/docker/registry/config.yml \
             registry:2

挂载当前目录下config.yml覆盖容器的/etc/docker/registry/config.yml文件,下面是整个配置文件,参数非常之多,解释一些常用的

version: 0.1
log: ## 日志选项
  level: debug
  formatter: text
  fields:
    service: registry
    environment: staging
  hooks:
    - type: mail
      disabled: true
      levels:
        - panic
      options:
        smtp:## 邮件通知配置
          addr: mail.example.com:25
          username: mailuser
          password: password
          insecure: true
        from: sender@example.com
        to:
          - errors@example.com
loglevel: debug # 即将弃用: 使用Log替代
storage:##存储选项,
  filesystem: ## 本地文件系统,也可以是云分布式存储,比如微软Azure,亚马逊S3,swift,OSS
    rootdirectory: /var/lib/registry
    maxthreads: 100 ##最大线程数
auth: #用户验证
  silly:
    realm: silly-realm
    service: silly-service
  token:
    realm: token-realm
    service: token-service
    issuer: registry-token-issuer
    rootcertbundle: /root/certs/bundle
  htpasswd:
    realm: basic-realm
    path: /path/to/htpasswd
middleware:##中间件类型有registry,repository,storage三种,每种中间件可以像下面的方式使用
  registry:
    - name: ARegistryMiddleware
      options:
        foo: bar
  storage:
    - name: ARegistryMiddleware
reporting:##监控,可以使用一些在线的监控工具包括bugsnag,newrelic
  newrelic:
    licensekey: newreliclicensekey
    name: newrelicname
    verbose: true
http:## 由Nginx提供的HTTP服务,可以把它理解成配置Nginx
  addr: localhost:5000
  prefix: /my/nested/registry/
  host: https://myregistryaddress.org:5000
  secret: asecretforlocaldevelopment
  relativeurls: false
  tls:
    certificate: /path/to/x509/public
    key: /path/to/x509/private
    clientcas:
      - /path/to/ca.pem
      - /path/to/another/ca.pem
    letsencrypt:
      cachefile: /path/to/cache-file
      email: emailused@letsencrypt.com
  debug:
    addr: localhost:5001
  headers:
    X-Content-Type-Options: [nosniff]
notifications:## 事件通知,当Registry服务器发生拉取,推送等时发送事件到endpoints
  endpoints:
    - name: alistener
      disabled: false
      url: https://my.listener.com/event
      headers: <http.Header>
      timeout: 500
      threshold: 5
      backoff: 1000
redis:##这个用过的都懂
  addr: localhost:6379
  password: asecret
  db: 0
  dialtimeout: 10ms
  readtimeout: 10ms
  writetimeout: 10ms
  pool:
    maxidle: 16
    maxactive: 64
    idletimeout: 300s
health:## 健康检查包括存储驱动,file,http服务,tcp的可用性检查
  storagedriver:
    enabled: true
    interval: 10s
    threshold: 3
  file:
    - file: /path/to/checked/file
      interval: 10s
  http:
    - uri: http://server.to.check/must/return/200
      headers:
        Authorization: [Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==]
      statuscode: 200
      timeout: 3s
      interval: 10s
      threshold: 3
  tcp:
    - addr: redis-server.domain.com:6379
      timeout: 3s
      interval: 10s
      threshold: 3
proxy:## 可以镜像Docker Hub的仓库
  remoteurl: https://registry-1.docker.io
  username: [username]
  password: [password]
compatibility: 
  schema1:
    signingkeyfile: /etc/registry/key.json

配置文件好长,请根据需求配置