Docker搭建私有化仓库的几种方法
搭建 Docker 私有化仓库是一个非常重要的实践,它能够帮助你安全地存储和管理 Docker 镜像,而无需将其发布到公共 Docker Hub。通过使用私有化仓库,你可以:
提高安全性:镜像存储在受控的环境中。提升效率:在公司网络内传输镜像,速度更快。实现自动化:配合 CI/CD 系统实现自动镜像管理。本文将详细介绍如何在不同环境下搭建 Docker 私有化仓库,并提供配置和优化建议。
Docker 私有化仓库(也称为 Docker Registry)是一种允许用户存储和分发 Docker 镜像的软件应用。Docker 官方提供了一个开源的 Docker Registry 镜像,可以在本地或服务器上轻松运行。
nginx。nginx:latest。Docker 官方提供了一个开源的 Docker Registry 镜像,可以直接使用。
首先,拉取官方 Registry 镜像并运行:
docker pull registry:2
运行私有化仓库:
docker run -d -p 5000:5000 --name my-registry registry:2-d:后台运行容器。-p 5000:5000:将主机的 5000 端口映射到容器的 5000 端口。--name my-registry:为容器命名为 my-registry。
将本地镜像推送到私有化仓库:
给镜像打标签:
docker tag IMAGE_ID localhost:5000/my-image:latest
例如:
docker tag ubuntu:latest localhost:5000/ubuntu:latest
推送镜像到私有仓库:
docker push localhost:5000/ubuntu:latest
在其他 Docker 主机上,从私有化仓库拉取镜像:
docker pull localhost:5000/ubuntu:latest
注意:在其他机器上访问私有仓库时,需要将 localhost 替换为私有仓库所在机器的 IP 地址或域名。
使用 curl 验证仓库中存储的镜像:
curl http://localhost:5000/v2/_catalog
返回结果:
{ "repositories": [ "ubuntu" ]}
你可以使用 docker-compose 来简化启动过程:
version: '3'services: registry: image: registry:2 ports: - "5000:5000" volumes: - ./data:/var/lib/registry
运行 docker-compose:
docker-compose up -d
Harbor 是一个开源的企业级 Docker Registry,提供更丰富的功能,如用户管理、访问控制、镜像复制等。
确保你的系统上已经安装了 Docker 和 Docker Compose。
访问 Harbor 官方下载页面 下载最新版本。
wget https://github.com/goharbor/harbor/releases/download/v2.9.0/harbor-online-installer-v2.9.0.tgz
解压安装包:
tar xzvf harbor-online-installer-v2.9.0.tgzcd harbor
在 harbor 目录下,有一个 harbor.yml 配置文件。根据需要进行配置:
hostname: reg.mycompany.com # 修改为你的域名或IPhttps: port: 443 certificate: /your/certificate/path private_key: /your/private/key/pathharbor_admin_password: Harbor12345 # 设置管理员密码
注意:确保使用正确的域名和证书路径。可以通过 Let s Encrypt 获取免费 SSL 证书。
执行以下命令安装和启动 Harbor:
./install.sh
启动 Harbor 服务:
docker-compose up -d
在浏览器中访问 https://reg.mycompany.com,使用配置的管理员账户登录,默认用户名为 admin,密码为配置文件中设置的 harbor_admin_password。
登录 Harbor:
docker login reg.mycompany.com
给镜像打标签:
docker tag IMAGE_ID reg.mycompany.com/myproject/my-image:latest
推送镜像到 Harbor:
docker push reg.mycompany.com/myproject/my-image:latest
在其他机器上,登录 Harbor 并拉取镜像:
docker login reg.mycompany.comdocker pull reg.mycompany.com/myproject/my-image:latest
GitLab 提供了集成的 Container Registry,可以作为私有化 Docker 仓库使用。
参考 GitLab 官方文档 安装 GitLab。
编辑 GitLab 配置文件 /etc/gitlab/gitlab.rb,启用 Registry:
registry_external_url 'https://registry.mycompany.com'
重新配置 GitLab:
gitlab-ctl reconfigure
docker login registry.mycompany.com
给镜像打标签:
docker tag IMAGE_ID registry.mycompany.com/mygroup/myproject/my-image:latest
推送镜像:
docker push registry.mycompany.com/mygroup/myproject/my-image:latest
docker pull registry.mycompany.com/mygroup/myproject/my-image:latest
在生产环境中,强烈建议为 Docker 私有化仓库配置 HTTPS,以保护数据传输安全。
使用 openssl 生成自签名证书:
mkdir -p certsopenssl req -newkey rsa:4096 -nodes -sha256 -keyout certs/domain.key -x509 -days 365 -out certs/domain.crt
生成过程会提示输入信息,如下所示:
Country Name (2 letter code) [AU]:CNState or Province Name (full name) [Some-State]:GuangdongLocality Name (eg, city) []:ShenzhenOrganization Name (eg, company) [Internet Widgits Pty Ltd]:My CompanyOrganizational Unit Name (eg, section) []:ITCommon Name (e.g. server FQDN or YOUR name) []:localhostEmail Address []:admin@example.com
将证书挂载到容器中:
docker run -d -p 5000:5000 --name my-registry \ -v `pwd`/certs:/certs \ -e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/domain.crt \ -e REGISTRY_HTTP_TLS_KEY=/certs/domain.key \ registry:2
Docker 私有化仓库支持使用基础认证(Basic Auth)保护访问。
创建用户和密码文件
使用 htpasswd 创建用户和密码文件:
apt-get install apache2-utils -yhtpasswd -cB htpasswd myusermyuser:用户名。你会被提示输入密码并确认。
启动带认证的 Registry
将认证文件挂载到容器中:
docker run -d -p 5000:5000 --name my-registry \ -v `pwd`/certs:/certs \ -v `pwd`/auth:/auth \ -e "REGISTRY_AUTH=htpasswd" \ -e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" \ -e REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd \ -e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/domain.crt \ -e REGISTRY_HTTP_TLS_KEY=/certs/domain.key \ registry:2
为确保私有化仓库的安全性,建议配置防火墙规则,仅允许特定的 IP 或子网访问仓库。
# 允许 192.168.1.0/24 子网访问iptables -A INPUT -p tcp -s 192.168.1.0/24 --dport 5000 -j ACCEPT# 拒绝其他 IP 访问iptables -A INPUT -p tcp --dport 5000 -j DROP
使用 curl 查看仓库中的镜像:
curl -u myuser:mypassword https://localhost:5000/v2/_catalog
查看指定镜像的标签:
curl -u myuser:mypassword https://localhost:5000/v2/ repository /tags/list
例如:
curl -u myuser:mypassword https://localhost:5000/v2/ubuntu/tags/list
删除指定镜像的标签:
curl -X DELETE -u myuser:mypassword https://localhost:5000/v2/ repository /manifests/ digest
获取 digest:
curl -u myuser:mypassword -I -H "Accept: application/vnd.docker.distribution.manifest.v2+json" \https://localhost:5000/v2/ repository /manifests/ tag
例如:
curl -u myuser:mypassword -I -H "Accept: application/vnd.docker.distribution.manifest.v2+json" \https://localhost:5000/v2/ubuntu/manifests/latest
定期清理不再使用的镜像和标签,节省存储空间。可以使用 Registry GC 工具。
停止 Registry:
docker stop my-registry
运行垃圾回收:
docker run --rm -v /var/lib/registry:/var/lib/registry registry:2 garbage-collect /etc/docker/registry/config.yml
重启 Registry:
docker start my-registry
可以使用 Prometheus 和 Grafana 监控 Registry 的性能和使用情况。
通过本文的介绍,你应该已经掌握了搭建 Docker 私有化仓库的基本步骤和配置方法。无论是使用官方的 Docker Registry 镜像,还是更强大的 Harbor 或 GitLab Container Registry,私有化仓库都能为你的镜像管理带来更高的安全性和效率。
Docker搭建私有化仓库的几种方法