-
Notifications
You must be signed in to change notification settings - Fork 228
/
dockerCompose.ts
690 lines (632 loc) · 32.3 KB
/
dockerCompose.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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as yaml from 'js-yaml';
import * as shellQuote from 'shell-quote';
import { createContainerProperties, startEventSeen, ResolverResult, getTunnelInformation, DockerResolverParameters, inspectDockerImage, getEmptyContextFolder, getFolderImageName, SubstitutedConfig, checkDockerSupportForGPU } from './utils';
import { ContainerProperties, setupInContainer, ResolverProgress } from '../spec-common/injectHeadless';
import { ContainerError } from '../spec-common/errors';
import { Workspace } from '../spec-utils/workspaces';
import { equalPaths, parseVersion, isEarlierVersion, CLIHost } from '../spec-common/commonUtils';
import { ContainerDetails, inspectContainer, listContainers, DockerCLIParameters, dockerCLI, dockerComposeCLI, dockerComposePtyCLI, PartialExecParameters, DockerComposeCLI, ImageDetails, toExecParameters, toPtyExecParameters } from '../spec-shutdown/dockerUtils';
import { DevContainerFromDockerComposeConfig, getDockerComposeFilePaths } from '../spec-configuration/configuration';
import { Log, LogLevel, makeLog, terminalEscapeSequences } from '../spec-utils/log';
import { getExtendImageBuildInfo, updateRemoteUserUID } from './containerFeatures';
import { Mount, parseMount } from '../spec-configuration/containerFeaturesConfiguration';
import path from 'path';
import { getDevcontainerMetadata, getImageBuildInfoFromDockerfile, getImageBuildInfoFromImage, getImageMetadataFromContainer, ImageBuildInfo, lifecycleCommandOriginMapFromMetadata, mergeConfiguration, MergedDevContainerConfig } from './imageMetadata';
import { ensureDockerfileHasFinalStageName } from './dockerfileUtils';
const projectLabel = 'com.docker.compose.project';
const serviceLabel = 'com.docker.compose.service';
export async function openDockerComposeDevContainer(params: DockerResolverParameters, workspace: Workspace, config: SubstitutedConfig<DevContainerFromDockerComposeConfig>, idLabels: string[], additionalFeatures: Record<string, string | boolean | Record<string, string | boolean>>): Promise<ResolverResult> {
const { common, dockerCLI, dockerComposeCLI } = params;
const { cliHost, env, output } = common;
const buildParams: DockerCLIParameters = { cliHost, dockerCLI, dockerComposeCLI, env, output };
return _openDockerComposeDevContainer(params, buildParams, workspace, config, getRemoteWorkspaceFolder(config.config), idLabels, additionalFeatures);
}
async function _openDockerComposeDevContainer(params: DockerResolverParameters, buildParams: DockerCLIParameters, workspace: Workspace, configWithRaw: SubstitutedConfig<DevContainerFromDockerComposeConfig>, remoteWorkspaceFolder: string, idLabels: string[], additionalFeatures: Record<string, string | boolean | Record<string, string | boolean>>): Promise<ResolverResult> {
const { common } = params;
const { cliHost: buildCLIHost } = buildParams;
const { config } = configWithRaw;
let container: ContainerDetails | undefined;
let containerProperties: ContainerProperties | undefined;
try {
const composeFiles = await getDockerComposeFilePaths(buildCLIHost, config, buildCLIHost.env, buildCLIHost.cwd);
const cwdEnvFile = buildCLIHost.path.join(buildCLIHost.cwd, '.env');
const envFile = Array.isArray(config.dockerComposeFile) && config.dockerComposeFile.length === 0 && await buildCLIHost.isFile(cwdEnvFile) ? cwdEnvFile : undefined;
const projectName = await getProjectName(buildParams, workspace, composeFiles);
const containerId = await findComposeContainer(params, projectName, config.service);
if (params.expectExistingContainer && !containerId) {
throw new ContainerError({ description: 'The expected container does not exist.' });
}
container = containerId ? await inspectContainer(params, containerId) : undefined;
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;
}
// let collapsedFeaturesConfig: CollapsedFeaturesConfig | undefined;
if (!container || container.State.Status !== 'running') {
const res = await startContainer(params, buildParams, configWithRaw, projectName, composeFiles, envFile, container, idLabels, additionalFeatures);
container = await inspectContainer(params, res.containerId);
// collapsedFeaturesConfig = res.collapsedFeaturesConfig;
// } else {
// const labels = container.Config.Labels || {};
// const featuresConfig = await generateFeaturesConfig(params.common, (await createFeaturesTempFolder(params.common)), config, async () => labels, getContainerFeaturesFolder);
// collapsedFeaturesConfig = collapseFeaturesConfig(featuresConfig);
}
const imageMetadata = getImageMetadataFromContainer(container, configWithRaw, undefined, idLabels, common.output).config;
const mergedConfig = mergeConfiguration(configWithRaw.config, imageMetadata);
containerProperties = await createContainerProperties(params, container.Id, remoteWorkspaceFolder, mergedConfig.remoteUser);
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,
composeProjectName: projectName,
};
} catch (originalError) {
const err = originalError instanceof ContainerError ? originalError : new ContainerError({
description: 'An error occurred setting up the container.',
originalError
});
if (container) {
err.manageContainer = true;
err.params = params.common;
err.containerId = container.Id;
err.dockerParams = params;
}
if (containerProperties) {
err.containerProperties = containerProperties;
}
err.config = config;
throw err;
}
}
export function getRemoteWorkspaceFolder(config: DevContainerFromDockerComposeConfig) {
return config.workspaceFolder || '/';
}
// exported for testing
export function getBuildInfoForService(composeService: any, cliHostPath: typeof path, localComposeFiles: string[]) {
// composeService should taken from readDockerComposeConfig
// the 'build' property can be a string or an object (https://docs.docker.com/compose/compose-file/build/#build-definition)
const image = composeService.image as string | undefined;
const composeBuild = composeService.build;
if (!composeBuild) {
return {
image
};
}
if (typeof (composeBuild) === 'string') {
return {
image,
build: {
context: composeBuild,
dockerfilePath: 'Dockerfile'
}
};
}
return {
image,
build: {
dockerfilePath: (composeBuild.dockerfile as string | undefined) ?? 'Dockerfile',
context: (composeBuild.context as string | undefined) ?? cliHostPath.dirname(localComposeFiles[0]),
target: composeBuild.target as string | undefined,
args: composeBuild.args as Record<string, string> | undefined,
}
};
}
export async function buildAndExtendDockerCompose(configWithRaw: SubstitutedConfig<DevContainerFromDockerComposeConfig>, projectName: string, params: DockerResolverParameters, localComposeFiles: string[], envFile: string | undefined, composeGlobalArgs: string[], runServices: string[], noCache: boolean, overrideFilePath: string, overrideFilePrefix: string, versionPrefix: string, additionalFeatures: Record<string, string | boolean | Record<string, string | boolean>>, canAddLabelsToContainer: boolean, additionalCacheFroms?: string[], noBuild?: boolean) {
const { common, dockerCLI, dockerComposeCLI: dockerComposeCLIFunc } = params;
const { cliHost, env, output } = common;
const { config } = configWithRaw;
const cliParams: DockerCLIParameters = { cliHost, dockerCLI, dockerComposeCLI: dockerComposeCLIFunc, env, output };
const composeConfig = await readDockerComposeConfig(cliParams, localComposeFiles, envFile);
const composeService = composeConfig.services[config.service];
// determine base imageName for generated features build stage(s)
let baseName = 'dev_container_auto_added_stage_label';
let dockerfile: string | undefined;
let imageBuildInfo: ImageBuildInfo;
const serviceInfo = getBuildInfoForService(composeService, cliHost.path, localComposeFiles);
if (serviceInfo.build) {
const { context, dockerfilePath, target } = serviceInfo.build;
const resolvedDockerfilePath = cliHost.path.isAbsolute(dockerfilePath) ? dockerfilePath : path.resolve(context, dockerfilePath);
const originalDockerfile = (await cliHost.readFile(resolvedDockerfilePath)).toString();
dockerfile = originalDockerfile;
if (target) {
// Explictly set build target for the dev container build features on that
baseName = 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(originalDockerfile, baseName);
baseName = lastStageName;
if (modifiedDockerfile) {
dockerfile = modifiedDockerfile;
}
}
imageBuildInfo = await getImageBuildInfoFromDockerfile(params, originalDockerfile, serviceInfo.build?.args || {}, serviceInfo.build?.target, configWithRaw.substitute);
} else {
imageBuildInfo = await getImageBuildInfoFromImage(params, composeService.image, configWithRaw.substitute);
}
// determine whether we need to extend with features
const noBuildKitParams = { ...params, buildKitVersion: null }; // skip BuildKit -> can't set additional build contexts with compose
const extendImageBuildInfo = await getExtendImageBuildInfo(noBuildKitParams, configWithRaw, baseName, imageBuildInfo, composeService.user, additionalFeatures, canAddLabelsToContainer);
let overrideImageName: string | undefined;
let buildOverrideContent = '';
if (extendImageBuildInfo?.featureBuildInfo) {
// Avoid retagging a previously pulled image.
if (!serviceInfo.build) {
overrideImageName = getFolderImageName(common);
buildOverrideContent += ` image: ${overrideImageName}\n`;
}
// Create overridden Dockerfile and generate docker-compose build override content
buildOverrideContent += ' build:\n';
if (!dockerfile) {
dockerfile = `FROM ${composeService.image} AS ${baseName}\n`;
}
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}`;
const finalDockerfilePath = cliHost.path.join(featureBuildInfo?.dstFolder, 'Dockerfile-with-features');
await cliHost.writeFile(finalDockerfilePath, Buffer.from(finalDockerfileContent));
buildOverrideContent += ` dockerfile: ${finalDockerfilePath}\n`;
if (serviceInfo.build?.target) {
// Replace target. (Only when set because it is only supported with Docker Compose file version 3.4 and later.)
buildOverrideContent += ` target: ${featureBuildInfo.overrideTarget}\n`;
}
if (!serviceInfo.build?.context) {
// need to supply a context as we don't have one inherited
const emptyDir = getEmptyContextFolder(common);
await cliHost.mkdirp(emptyDir);
buildOverrideContent += ` context: ${emptyDir}\n`;
}
// track additional build args to include
if (Object.keys(featureBuildInfo.buildArgs).length > 0 || params.buildKitVersion) {
buildOverrideContent += ' args:\n';
if (params.buildKitVersion) {
buildOverrideContent += ' - BUILDKIT_INLINE_CACHE=1\n';
}
for (const buildArg in featureBuildInfo.buildArgs) {
buildOverrideContent += ` - ${buildArg}=${featureBuildInfo.buildArgs[buildArg]}\n`;
}
}
}
// Generate the docker-compose override and build
const args = ['--project-name', projectName, ...composeGlobalArgs];
const additionalComposeOverrideFiles: string[] = [];
if (additionalCacheFroms && additionalCacheFroms.length > 0 || buildOverrideContent) {
const composeFolder = cliHost.path.join(overrideFilePath, 'docker-compose');
await cliHost.mkdirp(composeFolder);
const composeOverrideFile = cliHost.path.join(composeFolder, `${overrideFilePrefix}-${Date.now()}.yml`);
const cacheFromOverrideContent = (additionalCacheFroms && additionalCacheFroms.length > 0) ? ` cache_from:\n${additionalCacheFroms.map(cacheFrom => ` - ${cacheFrom}\n`).join('\n')}` : '';
const composeOverrideContent = `${versionPrefix}services:
${config.service}:
${buildOverrideContent?.trimEnd()}
${cacheFromOverrideContent}
`;
output.write(`Docker Compose override file for building image:\n${composeOverrideContent}`);
await cliHost.writeFile(composeOverrideFile, Buffer.from(composeOverrideContent));
additionalComposeOverrideFiles.push(composeOverrideFile);
args.push('-f', composeOverrideFile);
}
if (!noBuild) {
args.push('build');
if (noCache) {
args.push('--no-cache');
// `docker build --pull` pulls local image: https://github.com/devcontainers/cli/issues/60
if (!extendImageBuildInfo) {
args.push('--pull');
}
}
if (runServices.length) {
args.push(...runServices);
if (runServices.indexOf(config.service) === -1) {
args.push(config.service);
}
}
try {
if (params.isTTY) {
const infoParams = { ...toPtyExecParameters(params, await dockerComposeCLIFunc()), output: makeLog(output, LogLevel.Info) };
await dockerComposePtyCLI(infoParams, ...args);
} else {
const infoParams = { ...toExecParameters(params, await dockerComposeCLIFunc()), output: makeLog(output, LogLevel.Info), print: 'continuous' as 'continuous' };
await dockerComposeCLI(infoParams, ...args);
}
} catch (err) {
throw err instanceof ContainerError ? err : new ContainerError({ description: 'An error occurred building the Docker Compose images.', originalError: err, data: { fileWithError: localComposeFiles[0] } });
}
}
return {
imageMetadata: getDevcontainerMetadata(imageBuildInfo.metadata, configWithRaw, extendImageBuildInfo?.featuresConfig),
additionalComposeOverrideFiles,
overrideImageName,
labels: extendImageBuildInfo?.labels,
};
}
async function checkForPersistedFile(cliHost: CLIHost, output: Log, files: string[], prefix: string) {
const file = files.find((f) => f.indexOf(prefix) > -1);
if (file) {
const composeFileExists = await cliHost.isFile(file);
if (composeFileExists) {
output.write(`Restoring ${file} from persisted storage`);
return {
foundLabel: true,
fileExists: true,
file
};
} else {
output.write(`Expected ${file} to exist, but it did not`, LogLevel.Error);
return {
foundLabel: true,
fileExists: false,
file
};
}
} else {
output.write(`Expected to find a docker-compose file prefixed with ${prefix}, but did not.`, LogLevel.Error);
}
return {
foundLabel: false
};
}
async function startContainer(params: DockerResolverParameters, buildParams: DockerCLIParameters, configWithRaw: SubstitutedConfig<DevContainerFromDockerComposeConfig>, projectName: string, composeFiles: string[], envFile: string | undefined, container: ContainerDetails | undefined, idLabels: string[], additionalFeatures: Record<string, string | boolean | Record<string, string | boolean>>) {
const { common } = params;
const { persistedFolder, output } = common;
const { cliHost: buildCLIHost } = buildParams;
const { config } = configWithRaw;
const featuresBuildOverrideFilePrefix = 'docker-compose.devcontainer.build';
const featuresStartOverrideFilePrefix = 'docker-compose.devcontainer.containerFeatures';
common.progress(ResolverProgress.StartingContainer);
const localComposeFiles = composeFiles;
// If dockerComposeFile is an array, add -f <file> in order. https://docs.docker.com/compose/extends/#multiple-compose-files
const composeGlobalArgs = ([] as string[]).concat(...localComposeFiles.map(composeFile => ['-f', composeFile]));
if (envFile) {
composeGlobalArgs.push('--env-file', envFile);
}
const infoOutput = makeLog(buildParams.output, LogLevel.Info);
const composeConfig = await readDockerComposeConfig(buildParams, localComposeFiles, envFile);
const services = Object.keys(composeConfig.services || {});
if (services.indexOf(config.service) === -1) {
throw new ContainerError({ description: `Service '${config.service}' configured in devcontainer.json not found in Docker Compose configuration.`, data: { fileWithError: composeFiles[0] } });
}
let cancel: () => void;
const canceled = new Promise<void>((_, reject) => cancel = reject);
const { started } = await startEventSeen(params, { [projectLabel]: projectName, [serviceLabel]: config.service }, canceled, common.output, common.getLogLevel() === LogLevel.Trace); // await getEvents, but only assign started.
const service = composeConfig.services[config.service];
const originalImageName = service.image || getDefaultImageName(await buildParams.dockerComposeCLI(), projectName, config.service);
// Try to restore the 'third' docker-compose file and featuresConfig from persisted storage.
// This file may have been generated upon a Codespace creation.
const labels = container?.Config?.Labels;
output.write(`PersistedPath=${persistedFolder}, ContainerHasLabels=${!!labels}`);
let didRestoreFromPersistedShare = false;
if (container) {
if (labels) {
// update args for `docker-compose up` to use cached overrides
const configFiles = labels['com.docker.compose.project.config_files'];
output.write(`Container was created with these config files: ${configFiles}`);
// Parse out the full name of the 'containerFeatures' configFile
const files = configFiles?.split(',') ?? [];
const persistedBuildFile = await checkForPersistedFile(buildCLIHost, output, files, featuresBuildOverrideFilePrefix);
const persistedStartFile = await checkForPersistedFile(buildCLIHost, output, files, featuresStartOverrideFilePrefix);
if ((persistedBuildFile.fileExists || !persistedBuildFile.foundLabel) // require build file if in label
&& persistedStartFile.fileExists // always require start file
) {
didRestoreFromPersistedShare = true;
if (persistedBuildFile.fileExists) {
composeGlobalArgs.push('-f', persistedBuildFile.file);
}
if (persistedStartFile.fileExists) {
composeGlobalArgs.push('-f', persistedStartFile.file);
}
}
}
}
if (!container || !didRestoreFromPersistedShare) {
const noBuild = !!container; //if we have an existing container, just recreate override files but skip the build
const versionPrefix = await readVersionPrefix(buildCLIHost, localComposeFiles);
const infoParams = { ...params, common: { ...params.common, output: infoOutput } };
const { imageMetadata, additionalComposeOverrideFiles, overrideImageName, labels } = await buildAndExtendDockerCompose(configWithRaw, projectName, infoParams, localComposeFiles, envFile, composeGlobalArgs, config.runServices ?? [], params.buildNoCache ?? false, persistedFolder, featuresBuildOverrideFilePrefix, versionPrefix, additionalFeatures, true, params.additionalCacheFroms, noBuild);
additionalComposeOverrideFiles.forEach(overrideFilePath => composeGlobalArgs.push('-f', overrideFilePath));
const currentImageName = overrideImageName || originalImageName;
let cache: Promise<ImageDetails> | undefined;
const imageDetails = () => cache || (cache = inspectDockerImage(params, currentImageName, true));
const mergedConfig = mergeConfiguration(config, imageMetadata.config);
const updatedImageName = noBuild ? currentImageName : await updateRemoteUserUID(params, mergedConfig, currentImageName, imageDetails, service.user);
// Save override docker-compose file to disk.
// Persisted folder is a path that will be maintained between sessions
// Note: As a fallback, persistedFolder is set to the build's tmpDir() directory
const additionalLabels = labels ? idLabels.concat(Object.keys(labels).map(key => `${key}=${labels[key]}`)) : idLabels;
const overrideFilePath = await writeFeaturesComposeOverrideFile(updatedImageName, currentImageName, mergedConfig, config, versionPrefix, imageDetails, service, additionalLabels, params.additionalMounts, persistedFolder, featuresStartOverrideFilePrefix, buildCLIHost, params, output);
if (overrideFilePath) {
// Add file path to override file as parameter
composeGlobalArgs.push('-f', overrideFilePath);
}
}
const args = ['--project-name', projectName, ...composeGlobalArgs];
args.push('up', '-d');
if (container || params.expectExistingContainer) {
args.push('--no-recreate');
}
if (config.runServices && config.runServices.length) {
args.push(...config.runServices);
if (config.runServices.indexOf(config.service) === -1) {
args.push(config.service);
}
}
try {
if (params.isTTY) {
await dockerComposePtyCLI({ ...buildParams, output: infoOutput }, ...args);
} else {
await dockerComposeCLI({ ...buildParams, output: infoOutput }, ...args);
}
} catch (err) {
cancel!();
throw new ContainerError({ description: 'An error occurred starting Docker Compose up.', originalError: err, data: { fileWithError: localComposeFiles[0] } });
}
await started;
return {
containerId: (await findComposeContainer(params, projectName, config.service))!,
};
}
export async function readVersionPrefix(cliHost: CLIHost, composeFiles: string[]) {
if (!composeFiles.length) {
return '';
}
const firstComposeFile = (await cliHost.readFile(composeFiles[0])).toString();
const version = (/^\s*(version:.*)$/m.exec(firstComposeFile) || [])[1];
return version ? `${version}\n\n` : '';
}
export function getDefaultImageName(dockerComposeCLI: DockerComposeCLI, projectName: string, serviceName: string) {
const version = parseVersion(dockerComposeCLI.version);
const separator = version && isEarlierVersion(version, [2, 8, 0]) ? '_' : '-';
return `${projectName}${separator}${serviceName}`;
}
async function writeFeaturesComposeOverrideFile(
updatedImageName: string,
originalImageName: string,
mergedConfig: MergedDevContainerConfig,
config: DevContainerFromDockerComposeConfig,
versionPrefix: string,
imageDetails: () => Promise<ImageDetails>,
service: any,
additionalLabels: string[],
additionalMounts: Mount[],
overrideFilePath: string,
overrideFilePrefix: string,
buildCLIHost: CLIHost,
params: DockerResolverParameters,
output: Log,
) {
const composeOverrideContent = await generateFeaturesComposeOverrideContent(updatedImageName, originalImageName, mergedConfig, config, versionPrefix, imageDetails, service, additionalLabels, additionalMounts, params);
const overrideFileHasContents = !!composeOverrideContent && composeOverrideContent.length > 0 && composeOverrideContent.trim() !== '';
if (overrideFileHasContents) {
output.write(`Docker Compose override file for creating container:\n${composeOverrideContent}`);
const fileName = `${overrideFilePrefix}-${Date.now()}.yml`;
const composeFolder = buildCLIHost.path.join(overrideFilePath, 'docker-compose');
const composeOverrideFile = buildCLIHost.path.join(composeFolder, fileName);
output.write(`Writing ${fileName} to ${composeFolder}`);
await buildCLIHost.mkdirp(composeFolder);
await buildCLIHost.writeFile(composeOverrideFile, Buffer.from(composeOverrideContent));
return composeOverrideFile;
} else {
output.write('Override file was generated, but was empty and thus not persisted or included in the docker-compose arguments.');
return undefined;
}
}
async function generateFeaturesComposeOverrideContent(
updatedImageName: string,
originalImageName: string,
mergedConfig: MergedDevContainerConfig,
config: DevContainerFromDockerComposeConfig,
versionPrefix: string,
imageDetails: () => Promise<ImageDetails>,
service: any,
additionalLabels: string[],
additionalMounts: Mount[],
params: DockerResolverParameters,
) {
const overrideImage = updatedImageName !== originalImageName;
const user = mergedConfig.containerUser;
const env = mergedConfig.containerEnv || {};
const capAdd = mergedConfig.capAdd || [];
const securityOpts = mergedConfig.securityOpt || [];
const mounts = [
...mergedConfig.mounts || [],
...additionalMounts,
].map(m => typeof m === 'string' ? parseMount(m) : m);
const volumeMounts = mounts.filter(m => m.type === 'volume');
const customEntrypoints = mergedConfig.entrypoints || [];
const composeEntrypoint: string[] | undefined = typeof service.entrypoint === 'string' ? shellQuote.parse(service.entrypoint) : service.entrypoint;
const composeCommand: string[] | undefined = typeof service.command === 'string' ? shellQuote.parse(service.command) : service.command;
const { overrideCommand } = mergedConfig;
const userEntrypoint = overrideCommand ? [] : composeEntrypoint /* $ already escaped. */
|| ((await imageDetails()).Config.Entrypoint || []).map(c => c.replace(/\$/g, '$$$$')); // $ > $$ to escape docker-compose.yml's interpolation.
const userCommand = overrideCommand ? [] : composeCommand /* $ already escaped. */
|| (composeEntrypoint ? [/* Ignore image CMD per docker-compose.yml spec. */] : ((await imageDetails()).Config.Cmd || []).map(c => c.replace(/\$/g, '$$$$'))); // $ > $$ to escape docker-compose.yml's interpolation.
const hasGpuRequirement = config.hostRequirements?.gpu;
const addGpuCapability = hasGpuRequirement && await checkDockerSupportForGPU(params);
if (hasGpuRequirement && hasGpuRequirement !== 'optional' && !addGpuCapability) {
params.common.output.write('No GPU support found yet a GPU was required - consider marking it as "optional"', LogLevel.Warning);
}
const gpuResources = addGpuCapability ? `
deploy:
resources:
reservations:
devices:
- capabilities: [gpu]` : '';
return `${versionPrefix}services:
'${config.service}':${overrideImage ? `
image: ${updatedImageName}` : ''}
entrypoint: ["/bin/sh", "-c", "echo Container started\\n
trap \\"exit 0\\" 15\\n
${customEntrypoints.join('\\n\n')}\\n
exec \\"$$@\\"\\n
while sleep 1 & wait $$!; do :; done", "-"${userEntrypoint.map(a => `, ${JSON.stringify(a)}`).join('')}]${userCommand !== composeCommand ? `
command: ${JSON.stringify(userCommand)}` : ''}${mergedConfig.init ? `
init: true` : ''}${user ? `
user: ${user}` : ''}${Object.keys(env).length ? `
environment:${Object.keys(env).map(key => `
- ${key}=${env[key]}`).join('')}` : ''}${mergedConfig.privileged ? `
privileged: true` : ''}${capAdd.length ? `
cap_add:${capAdd.map(cap => `
- ${cap}`).join('')}` : ''}${securityOpts.length ? `
security_opt:${securityOpts.map(securityOpt => `
- ${securityOpt}`).join('')}` : ''}${additionalLabels.length ? `
labels:${additionalLabels.map(label => `
- '${label.replace(/\$/g, '$$$$').replace(/'/g, '\'\'')}'`).join('')}` : ''}${mounts.length ? `
volumes:${mounts.map(m => `
- ${m.source}:${m.target}`).join('')}` : ''}${gpuResources}${volumeMounts.length ? `
volumes:${volumeMounts.map(m => `
${m.source}:${m.external ? '\n external: true' : ''}`).join('')}` : ''}
`;
}
export async function readDockerComposeConfig(params: DockerCLIParameters, composeFiles: string[], envFile: string | undefined) {
try {
const composeGlobalArgs = ([] as string[]).concat(...composeFiles.map(composeFile => ['-f', composeFile]));
if (envFile) {
composeGlobalArgs.push('--env-file', envFile);
}
const composeCLI = await params.dockerComposeCLI();
if ((parseVersion(composeCLI.version) || [])[0] >= 2) {
composeGlobalArgs.push('--profile', '*');
}
try {
const partial = toExecParameters(params, 'dockerComposeCLI' in params ? await params.dockerComposeCLI() : undefined);
const { stdout } = await dockerComposeCLI({ ...partial, print: 'onerror' }, ...composeGlobalArgs, 'config');
const stdoutStr = stdout.toString();
params.output.write(stdoutStr);
return yaml.load(stdoutStr) || {} as any;
} catch (err) {
if (!Buffer.isBuffer(err?.stderr) || err?.stderr.toString().indexOf('UnicodeEncodeError') === -1) {
throw err;
}
// Upstream issues. https://github.com/microsoft/vscode-remote-release/issues/5308
if (params.cliHost.platform === 'win32') {
const { cmdOutput } = await dockerComposePtyCLI({
...params,
output: makeLog({
event: params.output.event,
dimensions: {
columns: 999999,
rows: 1,
},
}, LogLevel.Info),
}, ...composeGlobalArgs, 'config');
return yaml.load(cmdOutput.replace(terminalEscapeSequences, '')) || {} as any;
}
const { stdout } = await dockerComposeCLI({
...params,
env: {
...params.env,
LANG: 'en_US.UTF-8',
LC_CTYPE: 'en_US.UTF-8',
}
}, ...composeGlobalArgs, 'config');
const stdoutStr = stdout.toString();
params.output.write(stdoutStr);
return yaml.load(stdoutStr) || {} as any;
}
} catch (err) {
throw err instanceof ContainerError ? err : new ContainerError({ description: 'An error occurred retrieving the Docker Compose configuration.', originalError: err, data: { fileWithError: composeFiles[0] } });
}
}
export async function findComposeContainer(params: DockerCLIParameters | DockerResolverParameters, projectName: string, serviceName: string): Promise<string | undefined> {
const list = await listContainers(params, true, [
`${projectLabel}=${projectName}`,
`${serviceLabel}=${serviceName}`
]);
return list && list[0];
}
export async function getProjectName(params: DockerCLIParameters | DockerResolverParameters, workspace: Workspace, composeFiles: string[]) {
const { cliHost } = 'cliHost' in params ? params : params.common;
const newProjectName = await useNewProjectName(params);
const envName = toProjectName(cliHost.env.COMPOSE_PROJECT_NAME || '', newProjectName);
if (envName) {
return envName;
}
try {
const envPath = cliHost.path.join(cliHost.cwd, '.env');
const buffer = await cliHost.readFile(envPath);
const match = /^COMPOSE_PROJECT_NAME=(.+)$/m.exec(buffer.toString());
const value = match && match[1].trim();
const envFileName = toProjectName(value || '', newProjectName);
if (envFileName) {
return envFileName;
}
} catch (err) {
if (!(err && (err.code === 'ENOENT' || err.code === 'EISDIR'))) {
throw err;
}
}
const configDir = workspace.configFolderPath;
const workingDir = composeFiles[0] ? cliHost.path.dirname(composeFiles[0]) : cliHost.cwd; // From https://github.com/docker/compose/blob/79557e3d3ab67c3697641d9af91866d7e400cfeb/compose/config/config.py#L290
if (equalPaths(cliHost.platform, workingDir, cliHost.path.join(configDir, '.devcontainer'))) {
return toProjectName(`${cliHost.path.basename(configDir)}_devcontainer`, newProjectName);
}
return toProjectName(cliHost.path.basename(workingDir), newProjectName);
}
function toProjectName(basename: string, newProjectName: boolean) {
// From https://github.com/docker/compose/blob/79557e3d3ab67c3697641d9af91866d7e400cfeb/compose/cli/command.py#L152
if (!newProjectName) {
return basename.toLowerCase().replace(/[^a-z0-9]/g, '');
}
return basename.toLowerCase().replace(/[^-_a-z0-9]/g, '');
}
async function useNewProjectName(params: DockerCLIParameters | DockerResolverParameters) {
try {
const version = parseVersion((await params.dockerComposeCLI()).version);
if (!version) {
return true; // Optimistically continue.
}
return !isEarlierVersion(version, [1, 21, 0]); // 1.21.0 changed allowed characters in project names (added hyphen and underscore).
} catch (err) {
return true; // Optimistically continue.
}
}
export function dockerComposeCLIConfig(params: Omit<PartialExecParameters, 'cmd'>, dockerCLICmd: string, dockerComposeCLICmd: string) {
let result: Promise<DockerComposeCLI>;
return () => {
return result || (result = (async () => {
let v2 = false;
let stdout: Buffer;
try {
stdout = (await dockerComposeCLI({
...params,
cmd: dockerComposeCLICmd,
}, 'version', '--short')).stdout;
} catch (err) {
if (err?.code !== 'ENOENT') {
throw err;
}
stdout = (await dockerComposeCLI({
...params,
cmd: dockerCLICmd,
}, 'compose', 'version', '--short')).stdout;
v2 = true;
}
const version = stdout.toString().trim();
params.output.write(`Docker Compose version: ${version}`);
return {
version,
cmd: v2 ? dockerCLICmd : dockerComposeCLICmd,
args: v2 ? ['compose'] : [],
};
})());
};
}