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

Add multi build support #24

Merged
merged 15 commits into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -46,6 +46,10 @@ export interface ProvisionOptions {
remoteEnv: Record<string, string>;
additionalCacheFroms: string[];
useBuildKit: 'auto' | 'never';
enableBuildx: boolean;
buildxPlatform: string | undefined;
buildxPush: boolean;
buildxLoad: boolean;
}

export async function launch(options: ProvisionOptions, disposables: (() => Promise<unknown> | undefined)[]) {
Expand Down Expand Up @@ -112,6 +116,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 Down Expand Up @@ -146,6 +154,10 @@ export async function createDockerParams(options: ProvisionOptions, disposables:
updateRemoteUserUIDDefault,
additionalCacheFroms: options.additionalCacheFroms,
useBuildKit,
enableBuildx: common.enableBuildx,
buildxPlatform: common.buildxPlatform,
buildxPush: common.buildxPush,
buildxLoad: common.buildxLoad,
};
}

Expand Down
36 changes: 31 additions & 5 deletions src/spec-node/devContainersSpecCLI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ async function provision({
remoteEnv: keyValuesToRecord(addRemoteEnvs),
additionalCacheFroms: addCacheFroms,
useBuildKit: buildkit,
enableBuildx: false,
buildxPlatform: undefined,
juzuluag marked this conversation as resolved.
Show resolved Hide resolved
buildxPush: false,
buildxLoad: false
};

const result = await doProvision(options);
Expand Down Expand Up @@ -241,6 +245,10 @@ function buildOptions(y: Argv) {
'image-name': { type: 'string', description: 'Image name.' },
'cache-from': { type: 'string', description: 'Additional image to use as potential layer cache' },
'buildkit': { choices: ['auto' as 'auto', 'never' as 'never'], default: 'auto' as 'auto', description: 'Control whether BuildKit should be used' },
'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 @@ -269,6 +277,10 @@ async function doBuild({
'image-name': argImageName,
'cache-from': addCacheFrom,
'buildkit': buildkit,
'buildx': enableBuildx,
'platform': buildxPlatform,
'push': buildxPush,
'load': buildxLoad
}: BuildArgs) {
const disposables: (() => Promise<unknown> | undefined)[] = [];
const dispose = async () => {
Expand Down Expand Up @@ -305,7 +317,11 @@ async function doBuild({
updateRemoteUserUIDDefault: 'never',
remoteEnv: {},
additionalCacheFroms: addCacheFroms,
useBuildKit: buildkit
useBuildKit: buildkit,
enableBuildx,
buildxPlatform,
buildxPush,
buildxLoad,
}, disposables);

const { common, dockerCLI, dockerComposeCLI } = params;
Expand All @@ -325,10 +341,12 @@ async function doBuild({
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);
juzuluag marked this conversation as resolved.
Show resolved Hide resolved

if (argImageName) {
await dockerPtyCLI(params, 'tag', updatedImageName, argImageName);
if (!enableBuildx) {
await dockerPtyCLI(params, 'tag', updatedImageName, argImageName);
}
juzuluag marked this conversation as resolved.
Show resolved Hide resolved
imageNameResult = argImageName;
} else {
imageNameResult = updatedImageName;
Expand Down Expand Up @@ -509,7 +527,11 @@ async function doRunUserCommands({
updateRemoteUserUIDDefault: 'never',
remoteEnv: keyValuesToRecord(addRemoteEnvs),
additionalCacheFroms: [],
useBuildKit: 'auto'
useBuildKit: 'auto',
enableBuildx: false,
buildxPlatform: undefined,
buildxPush: false,
buildxLoad: false,
}, disposables);

const { common } = params;
Expand Down Expand Up @@ -753,7 +775,11 @@ async function doExec({
updateRemoteUserUIDDefault: 'never',
remoteEnv: keyValuesToRecord(addRemoteEnvs),
additionalCacheFroms: [],
useBuildKit: 'auto'
useBuildKit: 'auto',
enableBuildx: false,
buildxPlatform: undefined,
buildxPush: false,
buildxLoad: false,
}, disposables);

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

export async function buildNamedImageAndExtend(params: DockerResolverParameters, config: DevContainerFromDockerfileConfig | DevContainerFromImageConfig) {
export async function buildNamedImageAndExtend(params: DockerResolverParameters, config: DevContainerFromDockerfileConfig | DevContainerFromImageConfig, argImageName?: string) {
const imageName = 'image' in config ? config.image : getFolderImageName(params.common);
params.common.progress(ResolverProgress.BuildingImage);
if (isDockerFileConfig(config)) {
return await buildAndExtendImage(params, config, imageName, params.buildNoCache ?? false);
return await buildAndExtendImage(params, config, imageName, params.buildNoCache ?? false, argImageName);
}
// image-based dev container - extend
return await extendImage(params, config, imageName, 'image' in config);
}
async function buildAndExtendImage(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 buildAndExtendImage(buildParams: DockerResolverParameters, config: DevContainerFromDockerfileConfig, baseImageName: string, noCache: boolean, argImageName?: string) {
juzuluag marked this conversation as resolved.
Show resolved Hide resolved
const { cliHost, output } = buildParams.common;
const dockerfileUri = getDockerfilePath(cliHost, config);
const dockerfilePath = await uriToWSLFsPath(dockerfileUri, cliHost);
Expand Down Expand Up @@ -159,15 +178,34 @@ async function buildAndExtendImage(buildParams: DockerResolverParameters, config
}

const args: string[] = [];
if (buildParams.useBuildKit) {
if (buildParams.useBuildKit && !buildParams.enableBuildx) {
args.push('buildx', 'build',
'--load', // (short for --output=docker, i.e. load into normal 'docker images' collection)
'--build-arg', 'BUILDKIT_INLINE_CACHE=1', // ensure cache manifest is included in the image
);
} else if (buildParams.enableBuildx && argImageName) {
juzuluag marked this conversation as resolved.
Show resolved Hide resolved
if (!checkBuildxArgs(buildParams, argImageName)) {
const msg = `'devcontainer build --buildx [--platform | --push | --load] --image-name`;
throw new ContainerError({ description: msg });
}
args.push('buildx', 'build');
if (buildParams.buildxPlatform) {
args.push('--platform', buildParams.buildxPlatform);
juzuluag marked this conversation as resolved.
Show resolved Hide resolved
}
if (buildParams.buildxPush) {
args.push('--push');
}
if (buildParams.buildxLoad) {
args.push('--load');
}
args.push('-f', dockerfilePath, '-t', argImageName);
} else {
args.push('build');
juzuluag marked this conversation as resolved.
Show resolved Hide resolved
}
args.push('-f', finalDockerfilePath, '-t', baseImageName);
if (!buildParams.enableBuildx) {
args.push('-f', finalDockerfilePath, '-t', baseImageName);
}

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 @@ -69,6 +69,10 @@ export interface DockerResolverParameters {
updateRemoteUserUIDDefault: UpdateRemoteUserUIDDefault;
additionalCacheFroms: string[];
useBuildKit: boolean;
enableBuildx: boolean;
buildxPlatform: string | undefined;
buildxPush: boolean;
buildxLoad: boolean;
}

export interface ResolverResult {
Expand Down