第10章:企业级镜像仓库

📅 2026/7/2 20:23:12
第10章:企业级镜像仓库
第10章企业级镜像仓库本章目标理解镜像仓库的作用学会搭建和管理 Docker Hub、Harbor 等企业级私有镜像仓库。10.1 镜像仓库概览10.1.1 什么是镜像仓库镜像仓库Registry是用于存储和分发 Docker 镜像的服务。它是容器化工作流的核心组件负责镜像的存储、版本管理和分发。镜像仓库的工作流程 开发者 仓库服务 部署环境 ┌──────────┐ ┌──────────────┐ ┌──────────┐ │ docker │ push │ │ pull │ 生产服务器│ │ build │ ──────────► │ Registry │ ──────────► │ │ │ │ │ (镜像存储) │ │ │ └──────────┘ └──────────────┘ └──────────┘10.1.2 公有仓库 vs 私有仓库特性公有仓库Docker Hub私有仓库Harbor 等访问控制公开或受限完全自主控制数据安全数据存储在第三方数据自主可控网络速度受国际带宽限制内网高速访问镜像扫描有限功能完整安全扫描访问审计基础日志完整审计日志成本免费/付费自建成本适用场景开源项目、个人企业生产环境10.2 Docker Hub10.2.1 Docker Hub 简介Docker Hub 是 Docker 官方的公共镜像仓库也是最大的容器镜像社区。# 注册 Docker Hub 账号# 访问: https://hub.docker.com# 登录dockerlogin# Username: your-username# Password: your-password# 搜索镜像dockersearch nginx# 拉取镜像dockerpull nginx:latest# 推送镜像dockertag myapp:latest your-username/myapp:v1.0dockerpush your-username/myapp:v1.010.2.2 Docker Hub 的限制免费账户付费账户1 个私有仓库无限私有仓库100次/6小时 pull无限 pull无自动构建自动构建无组织管理组织管理10.3 Harbor 私有仓库10.3.1 Harbor 是什么Harbor 是由 VMware 开源的企业级容器镜像仓库提供了镜像存储、访问控制、安全扫描、审计日志等功能。Harbor 架构 ┌──────────────────────────────────────────────────────────┐ │ Harbor 架构 │ │ │ │ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │ │ │ Web UI │ │ Core API │ │ Registry │ │ │ │ (管理界面) │ │ (核心服务) │ │ (镜像存储) │ │ │ └──────┬───────┘ └──────┬───────┘ └──────┬───────┘ │ │ │ │ │ │ │ ┌──────┴─────────────────┴──────────────────┴───────┐ │ │ │ Nginx (反向代理) │ │ │ └──────────────────────────────────────────────────┘ │ │ │ │ ┌──────────┐ ┌──────────┐ ┌──────────┐ │ │ │ Harbor DB│ │ Redis │ │ Trivy │ │ │ │ (PostgreSQL) │ (缓存) │ │ (漏洞扫描)│ │ │ └──────────┘ └──────────┘ └──────────┘ │ └──────────────────────────────────────────────────────────┘10.3.2 Harbor 安装部署# 下载 Harbor 安装包wgethttps://github.com/goharbor/harbor/releases/download/v2.10.0/harbor-offline-installer-v2.10.0.tgz# 解压tarxzf harbor-offline-installer-v2.10.0.tgzcdharbor# 复制配置文件cpharbor.yml.tmpl harbor.yml# 编辑配置文件catharbor.ymlEOF # Harbor 配置 hostname: harbor.example.com # HTTP 配置 http: port: 80 # HTTPS 配置生产环境必须 https: port: 443 certificate: /data/cert/harbor.crt private_key: /data/cert/harbor.key # 管理员初始密码 harbor_admin_password: Harbor12345 # 数据库配置 database: password: root123 max_idle_conns: 100 max_open_conns: 900 # 数据存储路径 data_volume: /data/harbor # 镜像存储配置 storage_service: s3: # 如果使用 S3 存储 accesskey: your-access-key secretkey: your-secret-key region: us-east-1 bucket: harbor-images # Trivy 漏洞扫描 trivy: ignore_unfixed: false security_check: vuln insecure: false EOF# 生成自签名证书生产环境使用正式证书mkdir-p/data/cert openssl req-x509-newkeyrsa:4096-sha256-days3650\-nodes-keyout/data/cert/harbor.key\-out/data/cert/harbor.crt\-subj/CNharbor.example.com# 安装 Harbor./install.sh --with-trivy --with-chartmuseum# 验证安装dockercomposeps10.3.3 Harbor 管理# 访问 Harbor Web UI# http://harbor.example.com# 用户名: admin# 密码: Harbor12345# 停止 Harbordockercompose down# 启动 Harbordockercompose up-d# 查看日志dockercompose logs-f# 修改配置后重新安装./preparedockercompose downdockercompose up-d10.3.4 使用 Harbor 推送镜像# 1. 登录 Harbordockerlogin harbor.example.com# Username: admin# Password: Harbor12345# 2. 给镜像打标签dockertag nginx:latest harbor.example.com/library/nginx:v1.0# 3. 推送镜像dockerpush harbor.example.com/library/nginx:v1.0# 4. 从 Harbor 拉取镜像dockerpull harbor.example.com/library/nginx:v1.010.3.5 Harbor 项目管理Harbor 项目管理最佳实践 项目结构 ├── library 公共基础镜像 ├── dev 开发环境镜像 ├── staging 测试环境镜像 ├── production 生产环境镜像 └── team-a 团队 A 的镜像 权限管理 ├── 系统管理员 完全控制 ├── 项目管理员 项目级管理 ├── 开发人员 推送/拉取 └── 访客 仅拉取 访问控制 ├── 公开项目 所有人可拉取 └── 私有项目 需要授权10.4 Docker Registry轻量级方案10.4.1 使用官方 Registry# 启动官方 Registrydockerrun-d\--nameregistry\-p5000:5000\-vregistry-data:/var/lib/registry\registry:2# 使用 Registrydockertag nginx:latest localhost:5000/nginx:v1.0dockerpush localhost:5000/nginx:v1.0dockerpull localhost:5000/nginx:v1.0# 查看仓库中的镜像curlhttp://localhost:5000/v2/_catalog# {repositories:[nginx]}# 查看镜像标签curlhttp://localhost:5000/v2/nginx/tags/list# {name:nginx,tags:[v1.0]}10.4.2 Registry 配置# docker-compose.ymlversion:3.8services:registry:image:registry:2container_name:docker-registryrestart:unless-stoppedports:-5000:5000volumes:-registry-data:/var/lib/registry-./config.yml:/etc/docker/registry/config.ymlenvironment:REGISTRY_AUTH:htpasswdREGISTRY_AUTH_HTPASSWD_PATH:/auth/htpasswdREGISTRY_AUTH_HTPASSWD_REALM:Registry Realmvolumes:registry-data:# config.ymlversion:0.1log:fields:service:registrystorage:cache:blobdescriptor:inmemoryfilesystem:rootdirectory:/var/lib/registrydelete:enabled:truehttp:addr::5000headers:X-Content-Type-Options:[nosniff]tls:certificate:/certs/registry.crtkey:/certs/registry.key10.5 镜像安全扫描10.5.1 Docker Scout# Docker Scout 是 Docker 官方的安全扫描工具dockerscout cves nginx:latestdockerscout recommendations nginx:latest# 扫描本地镜像dockerbuild-tmyapp:latest.dockerscout cves myapp:latest10.5.2 Trivy开源方案# 安装 Trivy# macOSbrewinstalltrivy# Linuxsudoapt-getinstalltrivy# 扫描镜像漏洞trivy image nginx:latest# 只显示高危和严重漏洞trivy image--severityHIGH,CRITICAL nginx:latest# 输出 JSON 报告trivy image-fjson-oreport.json nginx:latest# 扫描并修复trivy image --exit-code1--severityHIGH,CRITICAL nginx:latest10.5.3 Clair开源方案# Clair 是 CoreOS 开源的漏洞扫描器# 通常与 Harbor 集成使用# 单独使用 Clairdockerrun-d--nameclair\-p6060:6060\quay.io/coreos/clair:latest# 使用 Clair 扫描clair-scanner--clairhttp://localhost:6060\--thresholdLOW\nginx:latest10.6 镜像签名与信任10.6.1 Docker Content TrustDCT# 启用 Docker Content TrustexportDOCKER_CONTENT_TRUST1# 推送时自动签名dockerpush myregistry.com/myapp:v1.0# 会自动生成签名密钥# 拉取时自动验证签名dockerpull myregistry.com/myapp:v1.0# 禁用 DCT临时exportDOCKER_CONTENT_TRUST010.6.2 Cosign更现代的方案# 安装 Cosignbrewinstallcosign# 生成密钥对cosign generate-key-pair# 签名镜像cosign sign--keycosign.key myregistry.com/myapp:v1.0# 验证签名cosign verify--keycosign.pub myregistry.com/myapp:v1.010.7 镜像治理策略10.7.1 镜像命名规范镜像命名最佳实践 registry/namespace/repository:tag 示例 # Docker Hub docker.io/mycompany/webapp:v1.2.3 # Harbor harbor.example.com/production/webapp:v1.2.3 # 阿里云 registry.cn-hangzhou.aliyuncs.com/mycompany/webapp:v1.2.3 # 标签规范 webapp:1.2.3 语义化版本 webapp:commit-a1b2c3d Git SHA webapp:2024.06.15 日期标签 webapp:staging 环境标签10.7.2 镜像生命周期管理# 自动化镜像清理策略# 1. 保留最近 N 个版本# 2. 保留最近 N 天的镜像# 3. 保留特定标签如 production# Harbor 中配置保留策略# Project → Configuration → Tag Retention# 自定义清理脚本#!/bin/bash# 清理保留策略REGISTRYharbor.example.comPROJECTproductionKEEP_TAGS10KEEP_DAYS30# 删除超过保留数量的标签# (使用 Harbor API)10.7.3 镜像缓存策略# 使用代理缓存加速拉取# 配置 Docker 使用代理# /etc/docker/daemon.json{registry-mirrors:[https://mirror.ccs.tencentyun.com]}# 使用 Squid 作为本地缓存dockerrun-d--namesquid\-p3128:3128\-vsquid-data:/var/cache/squid\squid# 配置 Docker 使用 Squid 代理# /etc/systemd/system/docker.service.d/proxy.conf[Service]EnvironmentHTTP_PROXYhttp://squid:3128EnvironmentHTTPS_PROXYhttp://squid:312810.8 动手实验实验 10.1搭建私有 Registry# 1. 启动 Registrydockerrun-d\--nameregistry\-p5000:5000\-vregistry-data:/var/lib/registry\registry:2# 2. 推送镜像dockertag nginx:latest localhost:5000/nginx:v1.0dockerpush localhost:5000/nginx:v1.0# 3. 验证curlhttp://localhost:5000/v2/_catalogcurlhttp://localhost:5000/v2/nginx/tags/list# 4. 删除本地镜像dockerrmi localhost:5000/nginx:v1.0# 5. 从 Registry 拉取dockerpull localhost:5000/nginx:v1.0# 6. 清理dockerrm-fregistrydockervolumermregistry-data实验 10.2使用 Trivy 扫描镜像# 1. 扫描官方镜像trivy image nginx:latest# 2. 只显示高危漏洞trivy image--severityHIGH,CRITICAL nginx:latest# 3. 扫描你自己的镜像dockerbuild-tmyapp:latest.trivy image myapp:latest# 4. 输出报告trivy image-fjson-otrivy-report.json nginx:latest10.9 本章小结方案特点适用场景Docker Hub公有、免费、简单开源项目、个人Harbor企业级、功能完整企业生产环境Registry轻量级、简单小型团队、测试10.10 课后练习基础题使用 Docker Registry 搭建私有镜像仓库。进阶题使用 Trivy 扫描你的镜像分析安全漏洞。实践题选做使用 Docker Compose 部署 Harbor。 下一章Docker 安全加固 —— 深入理解 Docker 安全风险和防护措施