Skip to content

Commit 2f8df43

Browse files
authored
feat(core): allow user to specify --platform (#26368)
Allowing user to provide a `platform` property when bundling docker assets ([ref](https://docs.docker.com/build/building/multi-platform/)). Currently, this is not possible when using an existing Docker image via the [BundlingOptions](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.BundlingOptions.html). You can specify this when building a [DockerImageAsset](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ecr_assets.DockerImageAsset.html#platform), but not when using an image as a builder: Closes #25759. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 97af59e commit 2f8df43

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

packages/aws-cdk-lib/core/lib/bundling.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,15 @@ export interface BundlingOptions {
133133
* @default - BundlingFileAccess.BIND_MOUNT
134134
*/
135135
readonly bundlingFileAccess?: BundlingFileAccess;
136+
137+
/**
138+
* Platform to build for. _Requires Docker Buildx_.
139+
*
140+
* Specify this property to build images on a specific platform.
141+
*
142+
* @default - no platform specified (the current machine architecture will be used)
143+
*/
144+
readonly platform?: string;
136145
}
137146

138147
/**
@@ -256,6 +265,9 @@ export class BundlingDockerImage {
256265
...options.network
257266
? ['--network', options.network]
258267
: [],
268+
...options.platform
269+
? ['--platform', options.platform]
270+
: [],
259271
...options.user
260272
? ['-u', options.user]
261273
: [],
@@ -535,6 +547,15 @@ export interface DockerRunOptions {
535547
* @default - no networking options
536548
*/
537549
readonly network?: string;
550+
551+
/**
552+
* Set platform if server is multi-platform capable. _Requires Docker Engine API v1.38+_.
553+
*
554+
* Example value: `linux/amd64`
555+
*
556+
* @default - no platform specified
557+
*/
558+
readonly platform?: string;
538559
}
539560

540561
/**

packages/aws-cdk-lib/core/test/bundling.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,39 @@ describe('bundling', () => {
439439
], { encoding: 'utf-8', stdio: ['ignore', process.stderr, 'inherit'] })).toEqual(true);
440440
});
441441

442+
test('adding user provided platform', () => {
443+
// GIVEN
444+
sinon.stub(process, 'platform').value('darwin');
445+
const spawnSyncStub = sinon.stub(child_process, 'spawnSync').returns({
446+
status: 0,
447+
stderr: Buffer.from('stderr'),
448+
stdout: Buffer.from('stdout'),
449+
pid: 123,
450+
output: ['stdout', 'stderr'],
451+
signal: null,
452+
});
453+
const image = DockerImage.fromRegistry('alpine');
454+
455+
// GIVEN
456+
image.run({
457+
command: ['cool', 'command'],
458+
platform: 'linux/amd64',
459+
volumes: [{ hostPath: '/host-path', containerPath: '/container-path' }],
460+
workingDirectory: '/working-directory',
461+
user: 'user:group',
462+
});
463+
464+
expect(spawnSyncStub.calledWith(dockerCmd, [
465+
'run', '--rm',
466+
'--platform', 'linux/amd64',
467+
'-u', 'user:group',
468+
'-v', '/host-path:/container-path:delegated',
469+
'-w', '/working-directory',
470+
'alpine',
471+
'cool', 'command',
472+
], { encoding: 'utf-8', stdio: ['ignore', process.stderr, 'inherit'] })).toEqual(true);
473+
});
474+
442475
test('adding user provided docker volume options', () => {
443476
// GIVEN
444477
sinon.stub(process, 'platform').value('darwin');

0 commit comments

Comments
 (0)