我想将Docker镜像从本地磁盘上传到我使用AWS CDK创建的存储库中。
当我使用aws_ecr_assets.DockerImageAsset
将Docker镜像添加到存储库中时(除了我无法通过CDK对其存储库设置权限外,它工作正常),我收到以下弃用警告:
DockerImageAsset.repositoryName is deprecated. Override "core.Stack.addDockerImageAsset" to control asset locations
当研究core.Stack.addDockerImageAsset
时,我得到一个提示,我应该重写stack.synthesizer.addDockerImageAsset()
。我的带有自定义合成器的简化堆栈如下所示:
class CustomSynthesizer(core.LegacyStackSynthesizer):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def add_docker_image_asset(
self,
*,
directory_name,
source_hash,
docker_build_args=None,
docker_build_target=None,
docker_file=None,
repository_name=None,
):
# What should I put in this function to upload the docker image into my repository?
class ContainerStack(core.Stack):
def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, synthesizer=CustomSynthesizer(), **kwargs)
repo = ecr.Repository(self, "TestRepo", repository_name="test_repo")
repo.grant_pull(iam.AccountPrincipal("123456789123"))
image_location = self.synthesizer.add_docker_image_asset(
directory_name="path/to/dir/with/Dockerfile",
source_hash="latest",
repository_name=repo.repository_name,
)
我尝试过的另一件事是使用标准堆栈合成器,并在其上调用add_docker_image_asset
。不幸的是,我收到以下错误消息,并且堆栈无法部署:test-stack: deploying...
[0%] start: Publishing latest:current
[25%] fail: Unable to execute 'docker' in order to build a container asset. Please install 'docker' and try again.
...
❌ test-stack failed: Error: Failed to publish one or more assets. See the error messages above for more information.
at Object.publishAssets (/home/user/.npm-global/lib/node_modules/aws-cdk/lib/util/asset-publishing.ts:25:11)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at Object.deployStack (/home/user/.npm-global/lib/node_modules/aws-cdk/lib/api/deploy-stack.ts:216:3)
at CdkToolkit.deploy (/home/user/.npm-global/lib/node_modules/aws-cdk/lib/cdk-toolkit.ts:181:24)
at main (/home/user/.npm-global/lib/node_modules/aws-cdk/bin/cdk.ts:268:16)
at initCommandLine (/home/user/.npm-global/lib/node_modules/aws-cdk/bin/cdk.ts:188:9)
Failed to publish one or more assets. See the error messages above for more information.
对于如何解决此问题,我持坚定态度,非常感谢您的帮助! 最佳答案
DockerImageAsset
是一个托管结构,它自己处理存储库和版本控制。默认情况下,它将为您的堆栈创建一个存储库,并将您的docker镜像上传到该存储库(通过其哈希标记它们)。
您不需要自己创建此存储库。但是,如果您像我一样,并且想使用一个清晰的存储库名称,则可以使用cdk.json
配置文件及其context
部分命名存储库:
// cdk.json
{
"app": "python3 your_app",
"context": {
"@aws-cdk/core:enableStackNameDuplicates": "true",
"aws-cdk:enableDiffNoFail": "true",
"assets-ecr-repository-name": "<REPO NAME GOES HERE>"
}
}
如果您想进一步更改存储库(我希望对其进行完全管理,但是,嘿),可以使用aws_ecr.Repository
构造上的静态方法之一将存储库加载到CDK堆栈中。https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-ecr.Repository.html#static-from-wbr-repository-wbr-namescope-id-repositoryname
希望这有所帮助 :-)
关于python - 如何在适用于Python的AWS CDK中覆盖aws_cdk.core.LegacyStackSynthesizer.add_docker_image_asset,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/63230559/