Skip to content

Commit 6a828e8

Browse files
committed
fix(aws-ecr-assets): TarballImageAsset respects CDK_DOCKER environment variable
1 parent a01aa38 commit 6a828e8

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

packages/aws-cdk-lib/aws-ecr-assets/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ This will instruct the toolkit to add the tarball as a file asset. During deploy
163163
from `local-image.tar`, push it to an Amazon ECR repository and wire the name of the repository as CloudFormation parameters
164164
to your stack.
165165

166+
Similar to `DockerImageAsset`, you can set the `CDK_DOCKER` environment variable to provide a custom Docker executable
167+
command or path. This may be needed when building in environments where the standard docker cannot be executed or when
168+
using alternative container runtimes like Finch.
169+
166170
## Publishing images to ECR repositories
167171

168172
`DockerImageAsset` is designed for seamless build & consumption of image assets by CDK code deployed to multiple environments

packages/aws-cdk-lib/aws-ecr-assets/lib/tarball-asset.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export class TarballImageAsset extends Construct implements IAsset {
107107
executable: [
108108
'sh',
109109
'-c',
110-
`docker load -i ${relativePathInOutDir} | tail -n 1 | sed "${DOCKER_LOAD_OUTPUT_REGEX}"`,
110+
`${process.env.CDK_DOCKER ?? 'docker'} load -i ${relativePathInOutDir} | tail -n 1 | sed "${DOCKER_LOAD_OUTPUT_REGEX}"`,
111111
],
112112
displayName: props.displayName ?? Names.stackRelativeConstructPath(this),
113113
});

packages/aws-cdk-lib/aws-ecr-assets/test/tarball-asset.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,40 @@ describe('image asset', () => {
190190
expect(oldSedWithNewFormat.stdout.toString().trim()).not.toBe('sha256:4a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a');
191191
expect(oldSedWithNewFormat.stdout.toString().trim()).toBe('Loaded image ID: sha256:4a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a');
192192
});
193+
194+
test('respects CDK_DOCKER environment variable', () => {
195+
// GIVEN
196+
const originalCdkDocker = process.env.CDK_DOCKER;
197+
process.env.CDK_DOCKER = 'custom-docker';
198+
199+
try {
200+
const app = new App();
201+
const stack = new Stack(app);
202+
const asset = new TarballImageAsset(stack, 'Image', {
203+
tarballFile,
204+
});
205+
206+
// WHEN
207+
const asm = app.synth();
208+
209+
// THEN
210+
const manifestArtifact = getAssetManifest(asm);
211+
const manifest = readAssetManifest(manifestArtifact);
212+
213+
expect(manifest.dockerImages?.[asset.assetHash]?.source?.executable).toEqual([
214+
'sh',
215+
'-c',
216+
`custom-docker load -i asset.${asset.assetHash}.tar | tail -n 1 | sed "${DOCKER_LOAD_OUTPUT_REGEX}"`,
217+
]);
218+
} finally {
219+
// Cleanup
220+
if (originalCdkDocker !== undefined) {
221+
process.env.CDK_DOCKER = originalCdkDocker;
222+
} else {
223+
delete process.env.CDK_DOCKER;
224+
}
225+
}
226+
});
193227
});
194228

195229
function isAssetManifest(x: cxapi.CloudArtifact): x is cxapi.AssetManifestArtifact {

0 commit comments

Comments
 (0)