|
| 1 | +## Nexus3.x 的私有仓库 |
| 2 | + |
| 3 | +使用 Docker 官方的 Registry 创建的仓库面临一些维护问题。比如某些镜像删除以后空间默认是不会回收的,需要一些命令去回收空间然后重启 Registry 程序。在企业中把内部的一些工具包放入 Nexus 中是比较常见的做法,最新版本 `Nexus3.x` 全面支持 Docker 的私有镜像。所以使用 [`Nexus3.x`](https://www.sonatype.com/download-oss-sonatype/) 一个软件来管理 `Docker` , `Maven` , `Yum` , `PyPI` 等是一个明智的选择。 |
| 4 | + |
| 5 | +### 启动 Nexus 容器 |
| 6 | + |
| 7 | +```bash |
| 8 | +$ docker run -d --name nexus3 --restart=always \ |
| 9 | + -p 8081:8081 \ |
| 10 | + --mount src=nexus-data,target=/nexus-data \ |
| 11 | + sonatype/nexus3 |
| 12 | +``` |
| 13 | + |
| 14 | +等待 3-5 分钟,如果 `nexus3` 容器没有异常退出,那么你可以使用浏览器打开 `http://YourIP:8081` 访问 Nexus 了。 |
| 15 | + |
| 16 | +第一次启动 Nexus 的默认帐号是 `admin` 密码是 `admin123` 登录以后点击页面上方的齿轮按钮进行设置。 |
| 17 | + |
| 18 | +### 创建仓库 |
| 19 | + |
| 20 | +创建一个私有仓库的方法: `Repository->Repositories` 点击右边菜单 `Create repository` 选择 `docker (hosted)` |
| 21 | + |
| 22 | +* Name: 仓库的名称 |
| 23 | +* HTTP: 仓库单独的访问端口 |
| 24 | +* Enable Docker V1 API: 如果需要同时支持 V1 版本请勾选此项(不建议勾选)。 |
| 25 | +* Hosted -> Deployment pollcy: 请选择 Allow redeploy 否则无法上传 Docker 镜像。 |
| 26 | + |
| 27 | +其它的仓库创建方法请各位自己摸索,还可以创建一个 docker (proxy) 类型的仓库链接到 DockerHub 上。再创建一个 docker (group) 类型的仓库把刚才的 hosted 与 proxy 添加在一起。主机在访问的时候默认下载私有仓库中的镜像,如果没有将链接到 DockerHub 中下载并缓存到 Nexus 中。 |
| 28 | + |
| 29 | +### 添加访问权限 |
| 30 | + |
| 31 | +菜单 `Security->Realms` 把 Docker Bearer Token Realm 移到右边的框中保存。 |
| 32 | + |
| 33 | +添加用户规则:菜单 `Security->Roles`->`Create role` 在 `Privlleges` 选项搜索 docker 把相应的规则移动到右边的框中然后保存。 |
| 34 | + |
| 35 | +添加用户:菜单 `Security->Users`->`Create local user` 在 `Roles` 选项中选中刚才创建的规则移动到右边的窗口保存。 |
| 36 | + |
| 37 | +### NGINX 加密代理 |
| 38 | + |
| 39 | +证书的生成请参见 [`私有仓库高级配置`](registry_auth.md) 里面证书生成一节。 |
| 40 | + |
| 41 | +NGINX 示例配置如下 |
| 42 | + |
| 43 | +```nginx |
| 44 | +upstream register |
| 45 | +{ |
| 46 | + server "YourHostName OR IP":5001; #端口为上面添加的私有镜像仓库是设置的 HTTP 选项的端口号 |
| 47 | + check interval=3000 rise=2 fall=10 timeout=1000 type=http; |
| 48 | + check_http_send "HEAD / HTTP/1.0\r\n\r\n"; |
| 49 | + check_http_expect_alive http_4xx; |
| 50 | +} |
| 51 | +
|
| 52 | +server { |
| 53 | + server_name YourDomainName;#如果没有 DNS 服务器做解析,请删除此选项使用本机 IP 地址访问 |
| 54 | + listen 443 ssl; |
| 55 | + |
| 56 | + ssl_certificate key/example.crt; |
| 57 | + ssl_certificate_key key/example.key; |
| 58 | + |
| 59 | + ssl_session_timeout 5m; |
| 60 | + ssl_protocols TLSv1 TLSv1.1 TLSv1.2; |
| 61 | + ssl_ciphers HIGH:!aNULL:!MD5; |
| 62 | + ssl_prefer_server_ciphers on; |
| 63 | + large_client_header_buffers 4 32k; |
| 64 | + client_max_body_size 300m; |
| 65 | + client_body_buffer_size 512k; |
| 66 | + proxy_connect_timeout 600; |
| 67 | + proxy_read_timeout 600; |
| 68 | + proxy_send_timeout 600; |
| 69 | + proxy_buffer_size 128k; |
| 70 | + proxy_buffers 4 64k; |
| 71 | + proxy_busy_buffers_size 128k; |
| 72 | + proxy_temp_file_write_size 512k; |
| 73 | +
|
| 74 | + location / { |
| 75 | + proxy_set_header Host $host; |
| 76 | + proxy_set_header X-Forwarded-Proto $scheme; |
| 77 | + proxy_set_header X-Forwarded-Port $server_port; |
| 78 | + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |
| 79 | + proxy_http_version 1.1; |
| 80 | + proxy_set_header Upgrade $http_upgrade; |
| 81 | + proxy_set_header Connection $connection_upgrade; |
| 82 | + proxy_redirect off; |
| 83 | + proxy_set_header X-Real-IP $remote_addr; |
| 84 | + proxy_pass http://register; |
| 85 | + proxy_read_timeout 900s; |
| 86 | +
|
| 87 | + } |
| 88 | + error_page 500 502 503 504 /50x.html; |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +### Docker 主机访问镜像仓库 |
| 93 | + |
| 94 | +如果不启用 SSL 加密可以通过前面章节的方法添加信任地址到 Docker 的配置文件中然后重启 Docker |
| 95 | + |
| 96 | +使用 SSL 加密以后程序需要访问就不能采用修改配置的访问了。具体方法如下: |
| 97 | + |
| 98 | +```bash |
| 99 | +$ openssl s_client -showcerts -connect YourDomainName OR HostIP:443 </dev/null 2>/dev/null|openssl x509 -outform PEM >ca.crt |
| 100 | +$ cat ca.crt | sudo tee -a /etc/ssl/certs/ca-certificates.crt |
| 101 | +$ systemctl restart docker |
| 102 | +``` |
| 103 | + |
| 104 | +使用 `docker login YourDomainName OR HostIP` 进行测试,用户名密码填写上面 Nexus 中生成的。 |
0 commit comments