通过Docker API将私有映像推送到Docker Registry

问题描述 投票:1回答:1

我正在使用Docker SDK for Python将本地映像存储库推送到Docker Registry(在我的情况下是DockerHub)。

我在文档here上描述的“client.images”上使用“push”方法。

不幸的是,所有发布的存储库都是公开的似乎没有标志可以进入私有存储库或确保推送的存储库是私有的。 Docker Python API可以实现吗?

我以三种不同的方式尝试了这一点(所有这些都导致了公共回购):

  1. 方法一:单独登录(有效,但导致公共回购):
client = docker.from_env()   
auth_client = client.login(username = "kelly_peyton", 
                    password = "nightingale", 
                    email = "[email protected]", 
                    registry = "docker.io",
                    reauth = True)
# other code here, not shown, to validate login succeeded

cli = APIClient(base_url="unix:///var/run/docker.sock")
br = cli.build(path = temp_local, 
    dockerfile = f"{temp_local}/Dockerfile", 
    tag = docker_repo_reference_tagged)
# other code here, not shown, to validate build succeeded

push_res = cli.push(repository = f"{docker_repo_reference}", 
    tag = docker_repo_tag, 
    stream = False, 
     decode = False)
  1. 方法二:传递给推送调用的凭据(有效,但导致公共回购):
client = docker.from_env()   
cli = APIClient(base_url="unix:///var/run/docker.sock")
br = cli.build(path = temp_local, 
    dockerfile = f"{temp_local}/Dockerfile", 
    tag = docker_repo_reference_tagged)
# other code here, not shown, to validate build succeeded

push_res = cli.push(repository = f"{docker_repo_reference}", 
    tag = docker_repo_tag, 
    stream = False, 
    auth_config = {
        "username" : "kelly_peyton",
        "password" : "nightingale", 
        "email" : "[email protected]", 
        "registry" : "docker.io"
        }, 
    decode = False)
  1. 方法三:命令行登录(不通过代码)(工作,但导致公共回购):
client = docker.from_env()   
cli = APIClient(base_url="unix:///var/run/docker.sock")
br = cli.build(path = temp_local, 
    dockerfile = f"{temp_local}/Dockerfile", 
    tag = docker_repo_reference_tagged)
# other code here, not shown, to validate build succeeded

push_res = cli.push(repository = f"{docker_repo_reference}", 
    tag = docker_repo_tag, 
    stream = False, 
    decode = False)

所有这三种方法都有效,因为图像确实被推送到了注册表(在我的情况下是DockerHub),并且自从我推送到我的私有DockerHub帐户后,显然已经工作了。但是,图像总是公开的。

docker sdk docker-api
1个回答
1
投票

您不能通过将凭据设置为API来使repo成为私有。它使您能够将图像仅推送到您的仓库。

您应该创建或转换repo为私有。请阅读documentation以了解如何操作。通常,只有你可以推送回购。如果repo public然后每个人都可以下载,如果repo私有,那么只有你可以下载。

© www.soinside.com 2019 - 2024. All rights reserved.