-
Notifications
You must be signed in to change notification settings - Fork 220
/
singleContainer.ts
447 lines (399 loc) · 19.9 KB
/
singleContainer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { createContainerProperties, startEventSeen, ResolverResult, getTunnelInformation, getDockerfilePath, getDockerContextPath, DockerResolverParameters, isDockerFileConfig, uriToWSLFsPath, WorkspaceConfiguration, getFolderImageName, inspectDockerImage, logUMask, SubstitutedConfig, checkDockerSupportForGPU, isBuildKitImagePolicyError } from './utils';
import { ContainerProperties, setupInContainer, ResolverProgress, ResolverParameters } from '../spec-common/injectHeadless';
import { ContainerError, toErrorText } from '../spec-common/errors';
import { ContainerDetails, listContainers, DockerCLIParameters, inspectContainers, dockerCLI, dockerPtyCLI, toPtyExecParameters, ImageDetails, toExecParameters } from '../spec-shutdown/dockerUtils';
import { DevContainerConfig, DevContainerFromDockerfileConfig, DevContainerFromImageConfig } from '../spec-configuration/configuration';
import { LogLevel, Log, makeLog } from '../spec-utils/log';
import { extendImage, getExtendImageBuildInfo, updateRemoteUserUID } from './containerFeatures';
import { getDevcontainerMetadata, getImageBuildInfoFromDockerfile, getImageMetadataFromContainer, ImageMetadataEntry, lifecycleCommandOriginMapFromMetadata, mergeConfiguration, MergedDevContainerConfig } from './imageMetadata';
import { ensureDockerfileHasFinalStageName, generateMountCommand } from './dockerfileUtils';
export const hostFolderLabel = 'devcontainer.local_folder'; // used to label containers created from a workspace/folder
export const configFileLabel = 'devcontainer.config_file';
export async function openDockerfileDevContainer(params: DockerResolverParameters, configWithRaw: SubstitutedConfig<DevContainerFromDockerfileConfig | DevContainerFromImageConfig>, workspaceConfig: WorkspaceConfiguration, idLabels: string[], additionalFeatures: Record<string, string | boolean | Record<string, string | boolean>>): Promise<ResolverResult> {
const { common } = params;
const { config } = configWithRaw;
// let collapsedFeaturesConfig: () => Promise<CollapsedFeaturesConfig | undefined>;
let container: ContainerDetails | undefined;
let containerProperties: ContainerProperties | undefined;
try {
container = await findExistingContainer(params, idLabels);
let imageMetadata: ImageMetadataEntry[];
let mergedConfig: MergedDevContainerConfig;
if (container) {
// let _collapsedFeatureConfig: Promise<CollapsedFeaturesConfig | undefined>;
// collapsedFeaturesConfig = async () => {
// return _collapsedFeatureConfig || (_collapsedFeatureConfig = (async () => {
// const allLabels = container?.Config.Labels || {};
// const featuresConfig = await generateFeaturesConfig(params.common, (await createFeaturesTempFolder(params.common)), config, async () => allLabels, getContainerFeaturesFolder);
// return collapseFeaturesConfig(featuresConfig);
// })());
// };
await startExistingContainer(params, idLabels, container);
imageMetadata = getImageMetadataFromContainer(container, configWithRaw, undefined, idLabels, common.output).config;
mergedConfig = mergeConfiguration(config, imageMetadata);
} else {
const res = await buildNamedImageAndExtend(params, configWithRaw, additionalFeatures, true);
imageMetadata = res.imageMetadata.config;
mergedConfig = mergeConfiguration(config, imageMetadata);
const { containerUser } = mergedConfig;
const updatedImageName = await updateRemoteUserUID(params, mergedConfig, res.updatedImageName[0], res.imageDetails, findUserArg(config.runArgs) || containerUser);
// collapsedFeaturesConfig = async () => res.collapsedFeaturesConfig;
try {
await spawnDevContainer(params, config, mergedConfig, updatedImageName, idLabels, workspaceConfig.workspaceMount, res.imageDetails, containerUser, res.labels || {});
} finally {
// In 'finally' because 'docker run' can fail after creating the container.
// Trying to get it here, so we can offer 'Rebuild Container' as an action later.
container = await findDevContainer(params, idLabels);
}
if (!container) {
return bailOut(common.output, 'Dev container not found.');
}
}
containerProperties = await createContainerProperties(params, container.Id, workspaceConfig.workspaceFolder, mergedConfig.remoteUser);
return await setupContainer(container, params, containerProperties, config, mergedConfig, imageMetadata);
} catch (e) {
throw createSetupError(e, container, params, containerProperties, config);
}
}
function createSetupError(originalError: any, container: ContainerDetails | undefined, params: DockerResolverParameters, containerProperties: ContainerProperties | undefined, config: DevContainerConfig | undefined): ContainerError {
let description = 'An error occurred setting up the container.';
if (originalError?.cmdOutput?.includes('docker: Error response from daemon: authorization denied by plugin')) {
description = originalError.cmdOutput;
}
const err = originalError instanceof ContainerError ? originalError : new ContainerError({
description,
originalError
});
if (container) {
err.manageContainer = true;
err.params = params.common;
err.containerId = container.Id;
err.dockerParams = params;
}
if (containerProperties) {
err.containerProperties = containerProperties;
}
if (config) {
err.config = config;
}
return err;
}
async function setupContainer(container: ContainerDetails, params: DockerResolverParameters, containerProperties: ContainerProperties, config: DevContainerFromDockerfileConfig | DevContainerFromImageConfig, mergedConfig: MergedDevContainerConfig, imageMetadata: ImageMetadataEntry[]): Promise<ResolverResult> {
const { common } = params;
const {
remoteEnv: extensionHostEnv,
} = await setupInContainer(common, containerProperties, mergedConfig, lifecycleCommandOriginMapFromMetadata(imageMetadata));
return {
params: common,
properties: containerProperties,
config,
resolvedAuthority: {
extensionHostEnv,
},
tunnelInformation: common.isLocalContainer ? getTunnelInformation(container) : {},
dockerParams: params,
dockerContainerId: container.Id,
};
}
function getDefaultName(config: DevContainerFromDockerfileConfig | DevContainerFromImageConfig, params: DockerResolverParameters) {
return 'image' in config && config.image ? config.image : getFolderImageName(params.common);
}
export async function buildNamedImageAndExtend(params: DockerResolverParameters, configWithRaw: SubstitutedConfig<DevContainerFromDockerfileConfig | DevContainerFromImageConfig>, additionalFeatures: Record<string, string | boolean | Record<string, string | boolean>>, canAddLabelsToContainer: boolean, argImageNames?: string[]): Promise<{ updatedImageName: string[]; imageMetadata: SubstitutedConfig<ImageMetadataEntry[]>; imageDetails: () => Promise<ImageDetails>; labels?: Record<string, string> }> {
const { config } = configWithRaw;
const imageNames = argImageNames ?? [getDefaultName(config, params)];
params.common.progress(ResolverProgress.BuildingImage);
if (isDockerFileConfig(config)) {
return await buildAndExtendImage(params, configWithRaw as SubstitutedConfig<DevContainerFromDockerfileConfig>, imageNames, params.buildNoCache ?? false, additionalFeatures);
}
// image-based dev container - extend
return await extendImage(params, configWithRaw, imageNames[0], additionalFeatures, canAddLabelsToContainer);
}
async function buildAndExtendImage(buildParams: DockerResolverParameters, configWithRaw: SubstitutedConfig<DevContainerFromDockerfileConfig>, baseImageNames: string[], noCache: boolean, additionalFeatures: Record<string, string | boolean | Record<string, string | boolean>>) {
const { cliHost, output } = buildParams.common;
const { config } = configWithRaw;
const dockerfileUri = getDockerfilePath(cliHost, config);
const dockerfilePath = await uriToWSLFsPath(dockerfileUri, cliHost);
if (!cliHost.isFile(dockerfilePath)) {
throw new ContainerError({ description: `Dockerfile (${dockerfilePath}) not found.` });
}
let dockerfile = (await cliHost.readFile(dockerfilePath)).toString();
const originalDockerfile = dockerfile;
let baseName = 'dev_container_auto_added_stage_label';
if (config.build?.target) {
// Explictly set build target for the dev container build features on that
baseName = config.build.target;
} else {
// Use the last stage in the Dockerfile
// Find the last line that starts with "FROM" (possibly preceeded by white-space)
const { lastStageName, modifiedDockerfile } = ensureDockerfileHasFinalStageName(dockerfile, baseName);
baseName = lastStageName;
if (modifiedDockerfile) {
dockerfile = modifiedDockerfile;
}
}
const imageBuildInfo = await getImageBuildInfoFromDockerfile(buildParams, originalDockerfile, config.build?.args || {}, config.build?.target, configWithRaw.substitute);
const extendImageBuildInfo = await getExtendImageBuildInfo(buildParams, configWithRaw, baseName, imageBuildInfo, undefined, additionalFeatures, false);
let finalDockerfilePath = dockerfilePath;
const additionalBuildArgs: string[] = [];
if (extendImageBuildInfo?.featureBuildInfo) {
const { featureBuildInfo } = extendImageBuildInfo;
// We add a '# syntax' line at the start, so strip out any existing line
const syntaxMatch = dockerfile.match(/^\s*#\s*syntax\s*=.*[\r\n]/g);
if (syntaxMatch) {
dockerfile = dockerfile.slice(syntaxMatch[0].length);
}
let finalDockerfileContent = `${featureBuildInfo.dockerfilePrefixContent}${dockerfile}\n${featureBuildInfo.dockerfileContent}`;
finalDockerfilePath = cliHost.path.join(featureBuildInfo?.dstFolder, 'Dockerfile-with-features');
await cliHost.writeFile(finalDockerfilePath, Buffer.from(finalDockerfileContent));
// track additional build args to include below
for (const buildContext in featureBuildInfo.buildKitContexts) {
additionalBuildArgs.push('--build-context', `${buildContext}=${featureBuildInfo.buildKitContexts[buildContext]}`);
}
for (const buildArg in featureBuildInfo.buildArgs) {
additionalBuildArgs.push('--build-arg', `${buildArg}=${featureBuildInfo.buildArgs[buildArg]}`);
}
}
const args: string[] = [];
if (!buildParams.buildKitVersion &&
(buildParams.buildxPlatform || buildParams.buildxPush)) {
throw new ContainerError({ description: '--platform or --push require BuildKit enabled.', data: { fileWithError: dockerfilePath } });
}
if (buildParams.buildKitVersion) {
args.push('buildx', 'build');
if (buildParams.buildxPlatform) {
output.write('Setting BuildKit platform(s): ' + buildParams.buildxPlatform, LogLevel.Trace);
args.push('--platform', buildParams.buildxPlatform);
}
if (buildParams.buildxPush) {
args.push('--push');
} else {
if (buildParams.buildxOutput) {
args.push('--output', buildParams.buildxOutput);
} else {
args.push('--load'); // (short for --output=docker, i.e. load into normal 'docker images' collection)
}
}
if (buildParams.buildxCacheTo) {
args.push('--cache-to', buildParams.buildxCacheTo);
}
args.push('--build-arg', 'BUILDKIT_INLINE_CACHE=1');
} else {
args.push('build');
}
args.push('-f', finalDockerfilePath);
baseImageNames.map(imageName => args.push('-t', imageName));
const target = extendImageBuildInfo?.featureBuildInfo ? extendImageBuildInfo.featureBuildInfo.overrideTarget : config.build?.target;
if (target) {
args.push('--target', target);
}
if (noCache) {
args.push('--no-cache');
// `docker build --pull` pulls local image: https://github.com/devcontainers/cli/issues/60
if (buildParams.buildKitVersion || !extendImageBuildInfo) {
args.push('--pull');
}
} else {
const configCacheFrom = config.build?.cacheFrom;
if (buildParams.additionalCacheFroms.length || (configCacheFrom && (configCacheFrom === 'string' || configCacheFrom.length))) {
await logUMask(buildParams);
}
buildParams.additionalCacheFroms.forEach(cacheFrom => args.push('--cache-from', cacheFrom));
if (config.build && config.build.cacheFrom) {
if (typeof config.build.cacheFrom === 'string') {
args.push('--cache-from', config.build.cacheFrom);
} else {
for (let index = 0; index < config.build.cacheFrom.length; index++) {
const cacheFrom = config.build.cacheFrom[index];
args.push('--cache-from', cacheFrom);
}
}
}
}
const buildArgs = config.build?.args;
if (buildArgs) {
for (const key in buildArgs) {
args.push('--build-arg', `${key}=${buildArgs[key]}`);
}
}
args.push(...additionalBuildArgs);
args.push(await uriToWSLFsPath(getDockerContextPath(cliHost, config), cliHost));
try {
if (buildParams.isTTY) {
const infoParams = { ...toPtyExecParameters(buildParams), output: makeLog(output, LogLevel.Info) };
await dockerPtyCLI(infoParams, ...args);
} else {
const infoParams = { ...toExecParameters(buildParams), output: makeLog(output, LogLevel.Info), print: 'continuous' as 'continuous' };
await dockerCLI(infoParams, ...args);
}
} catch (err) {
if (isBuildKitImagePolicyError(err)) {
throw new ContainerError({ description: 'Could not resolve image due to policy.', originalError: err, data: { fileWithError: dockerfilePath } });
}
throw new ContainerError({ description: 'An error occurred building the image.', originalError: err, data: { fileWithError: dockerfilePath } });
}
const imageDetails = () => inspectDockerImage(buildParams, baseImageNames[0], false);
return {
updatedImageName: baseImageNames,
imageMetadata: getDevcontainerMetadata(imageBuildInfo.metadata, configWithRaw, extendImageBuildInfo?.featuresConfig),
imageDetails
};
}
export function findUserArg(runArgs: string[] = []) {
for (let i = runArgs.length - 1; i >= 0; i--) {
const runArg = runArgs[i];
if ((runArg === '-u' || runArg === '--user') && i + 1 < runArgs.length) {
return runArgs[i + 1];
}
if (runArg.startsWith('-u=') || runArg.startsWith('--user=')) {
return runArg.substr(runArg.indexOf('=') + 1);
}
}
return undefined;
}
export async function findExistingContainer(params: DockerResolverParameters, labels: string[]) {
const { common } = params;
let container = await findDevContainer(params, labels);
if (params.expectExistingContainer && !container) {
throw new ContainerError({ description: 'The expected container does not exist.' });
}
if (container && (params.removeOnStartup === true || params.removeOnStartup === container.Id)) {
const text = 'Removing Existing Container';
const start = common.output.start(text);
await dockerCLI(params, 'rm', '-f', container.Id);
common.output.stop(text, start);
container = undefined;
}
return container;
}
async function startExistingContainer(params: DockerResolverParameters, labels: string[], container: ContainerDetails) {
const { common } = params;
const start = container.State.Status !== 'running';
if (start) {
const starting = 'Starting container';
const start = common.output.start(starting);
const infoParams = { ...toExecParameters(params), output: makeLog(common.output, LogLevel.Info), print: 'continuous' as 'continuous' };
await dockerCLI(infoParams, 'start', container.Id);
common.output.stop(starting, start);
let startedContainer = await findDevContainer(params, labels);
if (!startedContainer) {
bailOut(common.output, 'Dev container not found.');
}
}
return start;
}
export async function findDevContainer(params: DockerCLIParameters | DockerResolverParameters, labels: string[]): Promise<ContainerDetails | undefined> {
const ids = await listContainers(params, true, labels);
const details = await inspectContainers(params, ids);
return details.filter(container => container.State.Status !== 'removing')[0];
}
export async function extraRunArgs(common: ResolverParameters, params: DockerCLIParameters | DockerResolverParameters, config: DevContainerFromDockerfileConfig | DevContainerFromImageConfig) {
const extraArguments: string[] = [];
if (config.hostRequirements?.gpu) {
if (await checkDockerSupportForGPU(params)) {
common.output.write(`GPU support found, add GPU flags to docker call.`);
extraArguments.push('--gpus', 'all');
} else {
if (config.hostRequirements?.gpu !== 'optional') {
common.output.write('No GPU support found yet a GPU was required - consider marking it as "optional"', LogLevel.Warning);
}
}
}
return extraArguments;
}
export async function spawnDevContainer(params: DockerResolverParameters, config: DevContainerFromDockerfileConfig | DevContainerFromImageConfig, mergedConfig: MergedDevContainerConfig, imageName: string, labels: string[], workspaceMount: string | undefined, imageDetails: (() => Promise<ImageDetails>) | undefined, containerUser: string | undefined, extraLabels: Record<string, string>) {
const { common } = params;
common.progress(ResolverProgress.StartingContainer);
const appPort = config.appPort;
const exposedPorts = typeof appPort === 'number' || typeof appPort === 'string' ? [appPort] : appPort || [];
const exposed = (<string[]>[]).concat(...exposedPorts.map(port => ['-p', typeof port === 'number' ? `127.0.0.1:${port}:${port}` : port]));
const cwdMount = workspaceMount ? ['--mount', workspaceMount] : [];
const envObj = mergedConfig.containerEnv || {};
const containerEnv = Object.keys(envObj)
.reduce((args, key) => {
args.push('-e', `${key}=${envObj[key]}`);
return args;
}, [] as string[]);
const containerUserArgs = containerUser ? ['-u', containerUser] : [];
const featureArgs: string[] = [];
if (mergedConfig.init) {
featureArgs.push('--init');
}
if (mergedConfig.privileged) {
featureArgs.push('--privileged');
}
for (const cap of mergedConfig.capAdd || []) {
featureArgs.push('--cap-add', cap);
}
for (const securityOpt of mergedConfig.securityOpt || []) {
featureArgs.push('--security-opt', securityOpt);
}
const featureMounts = ([] as string[]).concat(
...[
...mergedConfig.mounts || [],
...params.additionalMounts,
].map(m => generateMountCommand(m))
);
const customEntrypoints = mergedConfig.entrypoints || [];
const entrypoint = ['--entrypoint', '/bin/sh'];
const cmd = ['-c', `echo Container started
trap "exit 0" 15
${customEntrypoints.join('\n')}
exec "$@"
while sleep 1 & wait $!; do :; done`, '-']; // `wait $!` allows for the `trap` to run (synchronous `sleep` would not).
const overrideCommand = mergedConfig.overrideCommand;
if (overrideCommand === false && imageDetails) {
const details = await imageDetails();
cmd.push(...details.Config.Entrypoint || []);
cmd.push(...details.Config.Cmd || []);
}
const args = [
'run',
'--sig-proxy=false',
'-a', 'STDOUT',
'-a', 'STDERR',
...exposed,
...cwdMount,
...featureMounts,
...getLabels(labels),
...containerEnv,
...containerUserArgs,
...(config.runArgs || []),
...(await extraRunArgs(common, params, config) || []),
...featureArgs,
...entrypoint,
...Object.keys(extraLabels).map(key => ['-l', `${key}=${extraLabels[key]}`]).flat(),
imageName,
...cmd
];
let cancel: () => void;
const canceled = new Promise<void>((_, reject) => cancel = reject);
const { started } = await startEventSeen(params, getLabelsAsRecord(labels), canceled, common.output, common.getLogLevel() === LogLevel.Trace);
const text = 'Starting container';
const start = common.output.start(text);
const infoParams = { ...toPtyExecParameters(params), output: makeLog(params.common.output, LogLevel.Info) };
const result = dockerPtyCLI(infoParams, ...args);
result.then(cancel!, cancel!);
await started;
common.output.stop(text, start);
}
function getLabels(labels: string[]): string[] {
let result: string[] = [];
labels.forEach(each => result.push('-l', each));
return result;
}
function getLabelsAsRecord(labels: string[]): Record<string, string> {
let result: Record<string, string> = {};
labels.forEach(each => {
let pair = each.split('=');
result[pair[0]] = pair[1];
});
return result;
}
export function bailOut(output: Log, message: string): never {
output.write(toErrorText(message));
throw new Error(message);
}