我想将 VS2022 中的 docker 镜像发布到 GitHub ghcr.io 注册表,但我找不到设置正确标签的方法。
我有一个简单的
ghcr.io.pubxml
:
<Project>
<PropertyGroup>
<WebPublishMethod>Custom</WebPublishMethod>
<DockerPublish>true</DockerPublish>
<RegistryUrl>https://ghcr.io</RegistryUrl>
<UserName>USERNAME</UserName>
<PublishImageTag>0.0.1</PublishImageTag>
<PublishProvider>ContainerRegistry</PublishProvider>
<LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
<LastUsedPlatform>Any CPU</LastUsedPlatform>
<ProjectGuid>4c629800-XXXX-XXXX-XXXX-dd71619c5281</ProjectGuid>
<_TargetId>DockerCustomContainerRegistry</_TargetId>
</PropertyGroup>
</Project>
发布时,它会按预期构建图像
build -f ".\Dockerfile" --force-rm -t assemblyname --build-arg "BUILD_CONFIGURATION=Release" --label "com.microsoft.created-by=visual-studio" --label "com.microsoft.visual-studio.project-name=Assembly.Name" ".\src"
请注意,它使用程序集名称作为标签。构建后为图像设置一个新标签
naming to docker.io/library/assemblyname done
在发布之前更改标签以使用版本和注册表:
The push refers to repository [ghcr.io/assemblyname]
所以最终的标签是:
ghcr.io/assemblyname:0.0.1
- 这是 GitHub 的无效标签,它需要 docker push ghcr.io/NAMESPACE/IMAGE_NAME:2.5
所以我遇到了发布错误
Publish has encountered an error.
Running the docker.exe push command failed.
name invalid
A diagnostic log has been written to the following location:
...
有一种方法可以更改默认标签。我在项目文件中添加了
DockerfileTag
选项:
<PropertyGroup>
<Version>0.0.1</Version>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<DockerfileTag>xakpc-dev-labs/my-name</DockerfileTag>
</PropertyGroup>
新标签按预期在构建过程中使用:
docker build -f ".\Dockerfile" --force-rm -t xakpc-dev-labs/my-name --build-arg "BUILD_CONFIGURATION=Release" --label "com.microsoft.created-by=visual-studio" --label "com.microsoft.visual-studio.project-name=Assembly.Name" ".\src"
它会创建一个带有标签
docker.io/xakpc-dev-labs/my-name:latest
的图像
但随后它尝试更改标签并失败并出现错误
Failed to push docker image
Publish has encountered an error.
Running the docker.exe tag command failed.
Error response from daemon: No such image: my-name:latest
A diagnostic log has been written to the following location:
...
在写问题时,我实际上找到了解决方法:
我在项目文件中设置了纯图像名称
<DockerfileTag>my-name</DockerfileTag>
并在pubxml中添加命名空间
<RegistryUrl>https://ghcr.io/xakpc-dev-labs</RegistryUrl>
我不确定这是否是“正确”的方式,但它有效