
```yaml

version: '3.8'
services:
  registry-server:
    image: registry:2.8.2
    container_name: registry-server
    restart: unless-stopped
    ports:
      - "5000:5000"
    environment:
      REGISTRY_STORAGE_FILESYSTEM_ROOTDIRECTORY: /var/lib/registry
      REGISTRY_STORAGE_DELETE_ENABLED: 'true'
    volumes:
      - /shumengya/docker/docker-registry/registry-data:/var/lib/registry
    networks:
      - registry-network

  registry-ui:
    image: joxit/docker-registry-ui:main
    container_name: registry-ui
    restart: unless-stopped
    ports:
      - "9191:80"
    environment:
      - SINGLE_REGISTRY=true
      - REGISTRY_TITLE=Docker Registry UI
      - DELETE_IMAGES=true
      - SHOW_CONTENT_DIGEST=true
      - NGINX_PROXY_PASS_URL=http://registry-server:5000
      - REGISTRY_SECURED=false
    depends_on:
      - registry-server
    networks:
      - registry-network
  
networks:
  registry-network:
    driver: bridge
    
```

# Docker Registry 常用管理命令

### 1. 基本镜像操作
```bash
# 查看所有镜像仓库
curl http://localhost:5000/v2/_catalog

# 查看特定镜像的所有标签
curl http://localhost:5000/v2/<image-name>/tags/list

# 查看镜像manifest
curl -H "Accept: application/vnd.docker.distribution.manifest.v2+json" \
  http://localhost:5000/v2/<image-name>/manifests/<tag>

# 查看镜像layer信息
curl http://localhost:5000/v2/<image-name>/manifests/<tag> | jq .
```

### 2. 推送/拉取镜像
```bash
# 标记镜像
docker tag <source-image> localhost:5000/<image-name>:<tag>

# 推送镜像到私有Registry
docker push localhost:5000/<image-name>:<tag>

# 从私有Registry拉取镜像
docker pull localhost:5000/<image-name>:<tag>

# 批量推送镜像
docker images | grep localhost:5000 | awk '{print $1":"$2}' | xargs -I {} docker push {}

# 批量拉取镜像
docker pull localhost:5000/alpine:latest
docker pull localhost:5000/nginx:latest
docker pull localhost:5000/ubuntu:latest
```

## 三、镜像删除命令

### 1. 启用删除功能
```bash
# 在docker-compose.yml中添加
environment:
  REGISTRY_STORAGE_DELETE_ENABLED: 'true'
```

### 2. 删除镜像步骤
```bash
# 1. 获取镜像digest
curl -H "Accept: application/vnd.docker.distribution.manifest.v2+json" \
  -I http://localhost:5000/v2/<image-name>/manifests/<tag>

# 返回结果中包含：Docker-Content-Digest: sha256:xxxx

# 2. 删除manifest
curl -X DELETE http://localhost:5000/v2/<image-name>/manifests/sha256:xxxx

# 3. 执行垃圾回收释放存储空间
docker-compose exec registry-server bin/registry garbage-collect /etc/docker/registry/config.yml

# 强制垃圾回收（dry-run先测试）
docker-compose exec registry-server bin/registry garbage-collect --dry-run /etc/docker/registry/config.yml
```


## 四、存储管理命令

### 1. 查看存储使用情况
```bash
# 查看数据目录大小
du -sh /shumengya/docker/docker-registry/registry-data/

# 查看详细存储使用
du -h --max-depth=2 /shumengya/docker/docker-registry/registry-data/

# 查看文件数量
find /shumengya/docker/docker-registry/registry-data/ -type f | wc -l

# 查看Registry存储统计
docker-compose exec registry-server registry garbage-collect --dry-run /etc/docker/registry/config.yml | grep "blobs eligible for deletion"
```

### 2. 清理存储
```bash
# 执行垃圾回收（清理已删除的blobs）
docker-compose exec registry-server bin/registry garbage-collect /etc/docker/registry/config.yml

# 清理未使用的层
docker system prune -af

# 清理所有未使用的Docker对象
docker system prune -a --volumes
```

## 五、备份与恢复命令

### 1. 备份Registry数据
```bash
# 备份整个数据目录
tar -czf registry-backup-$(date +%Y%m%d_%H%M%S).tar.gz -C /shumengya/docker/docker-registry registry-data/

# 增量备份（如果支持）
rsync -avz /shumengya/docker/docker-registry/registry-data/ /backup/registry-data/

# 导出所有镜像
docker-compose down
tar -czf registry-full-backup.tar.gz /shumengya/docker/docker-registry/
docker-compose up -d
```

### 2. 恢复Registry数据
```bash
# 停止服务
docker-compose down

# 恢复备份
tar -xzf registry-backup-20231201.tar.gz -C /shumengya/docker/docker-registry/

# 启动服务
docker-compose up -d

# 验证恢复
curl http://localhost:5000/v2/_catalog
```

## 六、配置管理命令

### 1. 查看配置
```bash
# 查看容器内配置文件
docker-compose exec registry-server cat /etc/docker/registry/config.yml

# 查看环境变量
docker-compose exec registry-server env

# 查看Registry版本
docker-compose exec registry-server registry --version
```

### 2. 修改配置
```bash
# 1. 修改docker-compose.yml
vim docker-compose.yml

# 2. 重新部署
docker-compose down
docker-compose up -d

# 或者热重载
docker-compose restart
```






## 常用命令速查表

| 功能 | 命令 |
|------|------|
| 启动服务 | `docker-compose up -d` |
| 停止服务 | `docker-compose down` |
| 查看日志 | `docker-compose logs -f` |
| 查看镜像列表 | `curl http://localhost:5000/v2/_catalog` |
| 推送镜像 | `docker push localhost:5000/镜像:标签` |
| 拉取镜像 | `docker pull localhost:5000/镜像:标签` |
| 删除镜像 | `curl -X DELETE http://localhost:5000/v2/镜像/manifests/sha256:xxx` |
| 垃圾回收 | `docker-compose exec registry-server registry garbage-collect /path/to/config.yml` |
| 查看存储 | `du -sh /shumengya/docker/docker-registry/registry-data/` |
| 备份数据 | `tar -czf backup.tar.gz /shumengya/docker/docker-registry/registry-data/` |

