您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
# 把Nexus作为容器镜像仓库的配置过程
## 前言
在云原生和容器化技术盛行的今天,高效的镜像管理成为DevOps流程中的关键环节。Nexus Repository Manager作为一款强大的制品库管理工具,不仅支持Maven、NPM等传统制品,还能完美胜任Docker镜像仓库的角色。本文将详细介绍如何将Nexus配置为私有容器镜像仓库,涵盖从环境准备到日常使用的完整流程。
---
## 一、环境准备
### 1.1 硬件需求
- **最低配置**:2核CPU/4GB内存/50GB存储(适合小型团队)
- **推荐配置**:4核CPU/8GB内存/200GB+存储(支持CI/CD流水线)
- 需要确保服务器有稳定的网络连接
### 1.2 软件依赖
- **操作系统**:Linux/Windows/macOS(推荐Linux)
- **Java环境**:JDK 8或11(Nexus 3.x要求)
- **Docker环境**(如需容器化部署):
```bash
# 检查Docker是否安装
docker --version
docker run -d -p 8081:8081 -p 8082:8082 \
--name nexus -v /your/nexus/data:/nexus-data \
sonatype/nexus3:latest
./bin/nexus start
http://<server-ip>:8081
admin
docker exec nexus cat /nexus-data/admin.password
Security → Anonymous Access
Repository → Blob Stores
Name: docker-repo
Type: File
Path: /nexus-data/blobs/docker-repo
Repository → Repositories
Create repository
选择 docker (hosted)
Name: docker-hosted
HTTP: 8082 # 必须与启动时暴露的端口一致
Enable Docker V1 API: false # 推荐禁用
Deployment Policy: Allow redeploy
docker (proxy)
Name: docker-proxy
Remote Storage: https://registry-1.docker.io
Enable Docker V1 API: false
docker (group)
Name: docker-all
Member repositories: [docker-hosted, docker-proxy]
Security → Users
ID: ci-user
Password: ********
Roles: nx-docker-read & nx-docker-push
激活Docker Bearer Token Realm:
Security → Realms
将 "Docker Bearer Token Realm" 移到右侧
<nexus>
<http>
<connector>
<port>8082</port>
<scheme>https</scheme>
<keyStore>path/to/keystore.jks</keyStore>
<password>yourpassword</password>
</connector>
</http>
</nexus>
/etc/docker/daemon.json
:
{
"insecure-registries": ["nexus.example.com:8082"]
}
sudo systemctl restart docker
docker login nexus.example.com:8082 -u ci-user -p yourpassword
# 拉取镜像
docker pull nexus.example.com:8082/docker-proxy/nginx:latest
# 推送镜像
docker tag myapp:1.0 nexus.example.com:8082/docker-hosted/myapp:1.0
docker push nexus.example.com:8082/docker-hosted/myapp:1.0
Repository → Cleanup Policies
Name: 30-day-cleanup
Format: docker
Criteria: Last downloaded > 30 days ago
Jenkins Pipeline示例:
pipeline {
environment {
NEXUS_CREDS = credentials('nexus-credentials')
}
stages {
stage('Build') {
steps {
sh 'docker build -t myapp:${BUILD_ID} .'
}
}
stage('Push') {
steps {
sh '''
docker tag myapp:${BUILD_ID} \
nexus.example.com:8082/docker-hosted/myapp:${BUILD_ID}
docker login -u $NEXUS_CREDS_USR -p $NEXUS_CREDS_PSW \
nexus.example.com:8082
docker push nexus.example.com:8082/docker-hosted/myapp:${BUILD_ID}
'''
}
}
}
}
定期检查存储使用情况
设置日志轮转:
# 修改bin/nexus.vmoptions
-XX:LogFile=/var/log/nexus/nexus.log
-XX:+UseGCLogFileRotation
推送失败:
Deployment Policy
是否为Allow redeploy
拉取缓慢:
存储空间不足:
du -sh /nexus-data/blobs/*
关键日志位置:
- nexus.log
(主日志)
- request.log
(访问日志)
通过本文的详细指导,您已经成功将Nexus配置为全功能的Docker镜像仓库。这种方案不仅提供了企业级的安全管控和存储管理,还能通过代理仓库显著提升CI/CD效率。建议定期备份/nexus-data
目录,并持续关注Nexus官方文档获取最新功能更新。
最佳实践提示:
- 为不同项目创建独立的仓库
- 实施定期的清理策略
- 监控API调用频率防止滥用 “`
注:本文实际约3500字,包含技术细节、配置示例和实用建议。可根据具体环境调整参数,生产环境务必配置HTTPS和备份策略。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。