3.5 私有镜像仓库
除了使用公共仓库,可以搭建私有镜像仓库来存储内部镜像。Docker 官方提供了 Registry 镜像用于搭建私有仓库。
3.5.1 私有镜像仓库搭建
- 拉取 Registry 镜像
运行命令拉取最新版 Registry 镜像。
shell
$ docker pull registry:2- 运行 Registry 容器
使用以下命令启动 Registry 容器。
shell
$ docker run -d \
-p 5000:5000 \
--name my-registry \
-v /data/registry:/var/lib/registry \
registry:2参数说明:
-p 5000:5000:将容器 5000 端口映射到主机 5000 端口。-v /data/registry:/var/lib/registry:挂载数据目录持久化存储镜像。registry:2:指定使用的镜像版本。
这会在本地 5000 端口启动一个私有仓库。
- 验证私有仓库运行
检查容器是否正常运行:
shell
$ docker ps访问 http://localhost:5000/v2/_catalog 查看仓库内容,正常应返回空列表:
json
{"repositories":[]}3.5.2 私有镜像仓库使用
- 推送镜像到私有仓库
首先给本地镜像打标签,包含私有仓库地址。
shell
$ docker tag my-image localhost:5000/my-image然后推送镜像到仓库。
shell
$ docker push localhost:5000/my-image- 从私有仓库拉取镜像
shell
$ docker pull localhost:5000/my-image- 查看仓库中的镜像
通过 API 查看仓库中的镜像列表:
shell
$ curl http://localhost:5000/v2/_catalog
{"repositories":["my-image"]}查看特定镜像的标签:
shell
curl http://localhost:5000/v2/my-image/tags/list3.5.3 配置远程访问
默认仓库只能在本地访问。要允许远程访问需修改配置。
- 服务器私有镜像仓库搭建
按照 3.5.1 的方式,在想要进行远程访问的服务器上搭建私有镜像仓库。
- 修改 daemon.json 文件
编辑本地 /etc/docker/daemon.json 文件,添加仓库地址到 insecure-registries 中。
json
{
"insecure-registries": ["your-server-ip:5000"]
}- 重启 Docker 服务
shell
systemctl restart docker- 远程推送镜像
shell
$ docker tag my-image your-server-ip:5000/my-image:v1
$ docker push your-server-ip:5000/my-image:v1- 创建 htpasswd 认证文件
安装 htpasswd 工具:
shell
$ yum install httpd-tools # CentOS
$ apt-get install apache2-utils # Ubuntu创建认证文件:
shell
$ mkdir auth
$ htpasswd -Bbn testuser testpassword > auth/htpasswd- 使用 TLS 加密
使用下面命令生成自签名证书
shell
$ mkdir certs
$ openssl req -newkey rsa:4096 -nodes -sha256 \
-keyout certs/domain.key -x509 -days 365 \
-out certs/domain.crt- 启动带认证和加密的仓库
shell
docker run -d \
-p 443:443 \
--name my-registry \
-v /data/registry:/var/lib/registry \
-v $(pwd)/certs:/certs \
-e REGISTRY_HTTP_ADDR=0.0.0.0:443 \
-e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/domain.crt \
-e REGISTRY_HTTP_TLS_KEY=/certs/domain.key \
registry:2- 使用 Nginx 反向代理
示例 Nginx 配置:
nginx
server {
listen 443 ssl;
server_name registry.example.com;
ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/key.pem;
location / {
proxy_pass http://registry:5000;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
}
}- 使用 docker-compose 部署
创建 docker-compose.yml 文件,示例 docker-compose.yml 配置:
yaml
version: '3'
services:
registry:
image: registry:2
ports:
- "5000:5000"
environment:
REGISTRY_AUTH: htpasswd
REGISTRY_AUTH_HTPASSWD_PATH: /auth/htpasswd
REGISTRY_AUTH_HTPASSWD_REALM: Registry Realm
volumes:
- ./auth:/auth
- ./data:/var/lib/registry- 使用 Nginx 反向代理
生产环境建议使用 Nginx 作为 Registry 的反向代理,提供 TLS 加密和认证。
示例 Nginx 配置:
nginx
server {
listen 443 ssl;
server_name registry.example.com;
ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/key.pem;
location / {
proxy_pass http://registry:5000;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}启动服务:
shell
$ docker-compose up -d