Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for multi-platform builds #18

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/spec-common/injectHeadless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ export interface ResolverParameters {
backgroundTasks: (Promise<void> | (() => Promise<void>))[];
persistedFolder: string; // A path where config can be persisted and restored at a later time. Should default to tmpdir() folder if not provided.
remoteEnv: Record<string, string>;
enableBuildx: boolean;
buildxPlatform: string | undefined;
buildxPush: boolean;
buildxLoad: boolean;
}

export interface PostCreate {
Expand Down
12 changes: 12 additions & 0 deletions src/spec-node/devContainers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ export interface ProvisionOptions {
updateRemoteUserUIDDefault: UpdateRemoteUserUIDDefault;
remoteEnv: Record<string, string>;
additionalCacheFroms: string[];
enableBuildx: boolean;
buildxPlatform: string | undefined;
buildxPush: boolean;
buildxLoad: boolean;
}

export async function launch(options: ProvisionOptions, disposables: (() => Promise<unknown> | undefined)[]) {
Expand Down Expand Up @@ -110,6 +114,10 @@ export async function createDockerParams(options: ProvisionOptions, disposables:
backgroundTasks: [],
persistedFolder: persistedFolder || await cliHost.tmpdir(), // Fallback to tmpDir(), even though that isn't 'persistent'
remoteEnv,
enableBuildx: options.enableBuildx,
buildxPlatform: options.buildxPlatform,
buildxPush: options.buildxPush,
buildxLoad: options.buildxLoad,
};

const dockerPath = options.dockerPath || 'docker';
Expand All @@ -135,6 +143,10 @@ export async function createDockerParams(options: ProvisionOptions, disposables:
userRepositoryConfigurationPaths: [],
updateRemoteUserUIDDefault,
additionalCacheFroms: options.additionalCacheFroms,
enableBuildx: common.enableBuildx,
buildxPlatform: common.buildxPlatform,
buildxPush: common.buildxPush,
buildxLoad: common.buildxLoad,
};
}

Expand Down
31 changes: 28 additions & 3 deletions src/spec-node/devContainersSpecCLI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ async function provision({
updateRemoteUserUIDDefault,
remoteEnv: keyValuesToRecord(addRemoteEnvs),
additionalCacheFroms: addCacheFroms,
enableBuildx: false,
buildxPlatform: undefined,
buildxPush: false,
buildxLoad: false
};

const result = await doProvision(options);
Expand Down Expand Up @@ -237,6 +241,10 @@ function buildOptions(y: Argv) {
'no-cache': { type: 'boolean', default: false, description: 'Builds the image with `--no-cache`.' },
'image-name': { type: 'string', description: 'Image name.' },
'cache-from' : {type: 'string', description: 'Additional image to use as potential layer cache' },
'buildx' : { type: 'boolean', default: false, description: 'Enable to use buildx arguments.' },
'platform': { type: 'string', description: 'Set target platforms.' },
'push': { type: 'boolean', default: false, description: 'Push to a container registry.' },
'load': { type: 'boolean', default: false, description: 'Load builded image to images.' },
});
}

Expand Down Expand Up @@ -264,6 +272,10 @@ async function doBuild({
'no-cache': buildNoCache,
'image-name': argImageName,
'cache-from': addCacheFrom,
'buildx' : enableBuildx,
'platform': buildxPlatform,
'push': buildxPush,
'load': buildxLoad
}: BuildArgs) {
const disposables: (() => Promise<unknown> | undefined)[] = [];
const dispose = async () => {
Expand Down Expand Up @@ -300,6 +312,10 @@ async function doBuild({
updateRemoteUserUIDDefault: 'never',
remoteEnv: {},
additionalCacheFroms: addCacheFroms,
enableBuildx,
buildxPlatform,
buildxPush,
buildxLoad,
}, disposables);

const { common, dockerCLI, dockerComposeCLI } = params;
Expand All @@ -317,12 +333,13 @@ async function doBuild({
let imageNameResult = '';

if (isDockerFileConfig(config)) {

// Build the base image and extend with features etc.
const { updatedImageName } = await buildNamedImageAndExtend(params, config);
const { updatedImageName } = await buildNamedImageAndExtend(params, config, argImageName);

if (argImageName) {
await dockerPtyCLI(params, 'tag', updatedImageName, argImageName);
if (!enableBuildx) {
await dockerPtyCLI(params, 'tag', updatedImageName, argImageName);
}
imageNameResult = argImageName;
} else {
imageNameResult = updatedImageName;
Expand Down Expand Up @@ -503,6 +520,10 @@ async function doRunUserCommands({
updateRemoteUserUIDDefault: 'never',
remoteEnv: keyValuesToRecord(addRemoteEnvs),
additionalCacheFroms: [],
enableBuildx: false,
buildxPlatform: undefined,
buildxPush: false,
buildxLoad: false,
}, disposables);

const { common } = params;
Expand Down Expand Up @@ -746,6 +767,10 @@ async function doExec({
updateRemoteUserUIDDefault: 'never',
remoteEnv: keyValuesToRecord(addRemoteEnvs),
additionalCacheFroms: [],
enableBuildx: false,
buildxPlatform: undefined,
buildxPush: false,
buildxLoad: false,
}, disposables);

const { common } = params;
Expand Down
48 changes: 42 additions & 6 deletions src/spec-node/singleContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,16 @@ async function setupContainer(container: ContainerDetails, params: DockerResolve
};
}

export async function buildNamedImageAndExtend(params: DockerResolverParameters, config: DevContainerFromDockerfileConfig | DevContainerFromImageConfig) {
const baseImageName = await buildNamedImage(params, config);
export async function buildNamedImageAndExtend(params: DockerResolverParameters, config: DevContainerFromDockerfileConfig | DevContainerFromImageConfig, argImageName?: string) {
const baseImageName = await buildNamedImage(params, config, argImageName);
return await extendImage(params, config, baseImageName, 'image' in config);
}

export async function buildNamedImage(params: DockerResolverParameters, config: DevContainerFromDockerfileConfig | DevContainerFromImageConfig) {
export async function buildNamedImage(params: DockerResolverParameters, config: DevContainerFromDockerfileConfig | DevContainerFromImageConfig, argImageName?: string) {
const imageName = 'image' in config ? config.image : getFolderImageName(params.common);
if (isDockerFileConfig(config)) {
params.common.progress(ResolverProgress.BuildingImage);
await buildImage(params, config, imageName, params.buildNoCache ?? false);
await buildImage(params, config, imageName, params.buildNoCache ?? false, argImageName);
}
return imageName;
}
Expand Down Expand Up @@ -165,15 +165,51 @@ export async function findDevContainer(params: DockerCLIParameters | DockerResol
return details.filter(container => container.State.Status !== 'removing')[0];
}

async function buildImage(buildParams: DockerResolverParameters, config: DevContainerFromDockerfileConfig, baseImageName: string, noCache: boolean) {
function checkBuildxArgs(buildParams: DockerResolverParameters, imageName?: string) {
if (buildParams.enableBuildx &&
!buildParams.buildxPlatform &&
!buildParams.buildxPush &&
!buildParams.buildxLoad) {
return true;
}
if (!buildParams.enableBuildx &&
((buildParams.buildxPlatform || buildParams.buildxPlatform?.length === 0) || buildParams.buildxPush || buildParams.buildxLoad)) {
return false;
}
if (buildParams.enableBuildx &&
((buildParams.buildxPlatform && buildParams.buildxPlatform?.length > 0) || buildParams.buildxPush || buildParams.buildxLoad) && !imageName) {
return false;
}
return true;
}

async function buildImage(buildParams: DockerResolverParameters, config: DevContainerFromDockerfileConfig, baseImageName: string, noCache: boolean, argImageName?: string) {
const { cliHost, output } = buildParams.common;
const dockerfileUri = getDockerfilePath(cliHost, config);
const dockerfilePath = await uriToWSLFsPath(dockerfileUri, cliHost);
if (!cliHost.isFile(dockerfilePath)) {
throw new ContainerError({ description: `Dockerfile (${dockerfilePath}) not found.` });
}

const args = ['build', '-f', dockerfilePath, '-t', baseImageName];
let args = ['build', '-f', dockerfilePath, '-t', baseImageName];
if (buildParams.enableBuildx && argImageName) {
if (!checkBuildxArgs(buildParams, argImageName)) {
const msg = `'devcontainer build --buildx [--platform | --push | --load] --image-name`;
throw new ContainerError({ description: msg });
}
args = ['buildx', 'build'];
if (buildParams.buildxPlatform) {
args.push('--platform', buildParams.buildxPlatform);
}
if (buildParams.buildxPush) {
args.push('--push');
}
if (buildParams.buildxLoad) {
args.push('--load');
}
args.push('-f', dockerfilePath, '-t', argImageName);
}

const target = config.build?.target;
if (target) {
args.push('--target', target);
Expand Down
4 changes: 4 additions & 0 deletions src/spec-node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ export interface DockerResolverParameters {
additionalMounts: Mount[];
updateRemoteUserUIDDefault: UpdateRemoteUserUIDDefault;
additionalCacheFroms: string[];
enableBuildx: boolean;
buildxPlatform: string | undefined;
buildxPush: boolean;
buildxLoad: boolean;
}

export interface ResolverResult {
Expand Down