将 Docker 图片推送到 Nexus 3

Push Docker image to Nexus 3

启动 Sonatype Nexus 3 图像(命令 1)后,我尝试创建一个存储库并将一个测试图像(命令 2)推送到该存储库,但出现错误 405(错误 1)

命令 1

$ docker run -d -p 8081:8081 --name nexus sonatype/nexus3:3.14.0

命令 2

$ docker push 127.0.0.1:8081/repository/test2/image-test:0.1

错误 1

error parsing HTTP 405 response body: invalid character '<' looking for beginning of value: "\n<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n  <title>405 - Nexus Repository Manager</title>\n  <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>\n\n\n  <!--[if lt IE 9]>\n  <script>(new Image).src=\"http://127.0.0.1:8081/favicon.ico?3.14.0-04\"</script>\n  <![endif]-->\n  <link rel=\"icon\" type=\"image/png\" href=\"http://127.0.0.1:8081/favicon-32x32.png?3.14.0-04\" sizes=\"32x32\">\n  <link rel=\"mask-icon\" href=\"http://127.0.0.1:8081/safari-pinned-tab.svg?3.14.0-04\" color=\"#5bbad5\">\n  <link rel=\"icon\" type=\"image/png\" href=\"http://127.0.0.1:8081/favicon-16x16.png?3.14.0-04\" sizes=\"16x16\">\n  <link rel=\"shortcut icon\" href=\"http://127.0.0.1:8081/favicon.ico?3.14.0-04\">\n  <meta name=\"msapplication-TileImage\" content=\"http://127.0.0.1:8081/mstile-144x144.png?3.14.0-04\">\n  <meta name=\"msapplication-TileColor\" content=\"#00a300\">\n\n  <link rel=\"stylesheet\" type=\"text/css\" href=\"http://127.0.0.1:8081/static/css/nexus-content.css?3.14.0-04\"/>\n</head>\n<body>\n<div class=\"nexus-header\">\n  <a href=\"http://127.0.0.1:8081\">\n    <div class=\"product-logo\">\n      <img src=\"http://127.0.0.1:8081/static/images/nexus.png?3.14.0-04\" alt=\"Product logo\"/>\n    </div>\n    <div class=\"product-id\">\n      <div class=\"product-id__line-1\">\n        <span class=\"product-name\">Nexus Repository Manager</span>\n      </div>\n      <div class=\"product-id__line-2\">\n        <span class=\"product-spec\">OSS 3.14.0-04</span>\n      </div>\n    </div>\n  </a>\n</div>\n\n<div class=\"nexus-body\">\n  <div class=\"content-header\">\n    <img src=\"http://127.0.0.1:8081/static/rapture/resources/icons/x32/exclamation.png?3.14.0-04\" alt=\"Exclamation point\" aria-role=\"presentation\"/>\n    <span class=\"title\">Error 405</span>\n    <span class=\"description\">Method Not Allowed</span>\n  </div>\n  <div class=\"content-body\">\n    <div class=\"content-section\">\n      HTTP method POST is not supported by this URL\n    </div>\n      </div>\n</div>\n</body>\n</html>\n\n"

说明

经过一些研究,我发现 nexus3 docker 存储库设计为使用每个存储库(托管、组或代理)的单独端口。

https://issues.sonatype.org/browse/NEXUS-9960

解决方案

所以我销毁了我以前的 docker 容器,因为我没有关于它的任何相关信息并启动了相同的命令,但启用了一个额外的端口。

$ docker run -d -p 8081:8081 --name nexus sonatype/nexus3:3.14.0

已更新:需要为 docker

打开端口 8082
$ docker run -d -p 8081:8081 -p 8082:8082 --name nexus sonatype/nexus3:3.14.0

因此,当您创建一个新的 docker 存储库时,您至少需要定义一个 http 连接器端口,我在图中定义为 8082。

之后您必须使用默认管理员帐户 (admin admin123) 登录服务

$ docker login 127.0.0.1:8082
Username: admin
Password: 
WARNING! Your password will be stored unencrypted in         /home/user/.docker/config.json.
Configure a credential helper to remove this warning. See

https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

然后尝试将新标签上传到 url 并且成功了。

$ docker push 127.0.0.1:8082/repository/test2/image-test:0.1
The push refers to repository [127.0.0.1:8082/repository/test2/image-test]
cd76d43ec36e: Pushed 
8ad8344c7fe3: Pushed 
b28ef0b6fef8: Pushed 
0.1: digest: sha256:315f00bd7986508cb0984130bbe3f7f26b2ec477122c9bf7459b0b64e443a232 size: 948

额外 - Dockerfile

因此,因为我需要为我的生产环境创建自定义 nexus3 docker 映像,所以我启动了 Dockerfile,如下所示:

FROM sonatype/nexus3:3.14.0

ENV NEXUS_DATA = /nexus-data/

EXPOSE 8090-8099

我将使用从 8090 到 8099 的端口来指定不同的 docker 图像存储库,而不是 8022,但如果我需要更多端口,我可以更改 valors 或添加新的端口范围。

希望有用!!

Nexus 文档说:

共享图像可以通过将图像发布到托管存储库来实现。这是完全私密的,需要您标记和推送图像。标记图像时,可以使用图像标识符 (imageId)。它在显示具有 docker 个图像的所有图像列表时列出。创建标签的语法和示例(使用 imageId)是:

docker tag <imageId or imageName> <nexus-hostname>:<repository-port>/<image>:<tag>
docker tag af340544ed62 nexus.example.com:18444/hello-world:mytag

等同于一个版本的标签创建成功后,你可以用docker张图片确认它的创建,并使用语法发出推送:

docker push <nexus-hostname>:<repository-port>/<image>:<tag>

请注意,端口必须是为要推送到的托管存储库配置的存储库连接器端口。您不能推送到存储库组或代理存储库。

希望对你有帮助!