-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathdocker.ts
More file actions
406 lines (364 loc) · 10.9 KB
/
docker.ts
File metadata and controls
406 lines (364 loc) · 10.9 KB
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
import path from 'path';
import * as fs from 'fs';
import * as os from 'os';
import * as config from './config';
import {ExecFunction} from './exec';
import {getAbsolutePath} from './file';
import {substituteValues} from './envvars';
import {parseGroup, parsePasswd} from './users';
export async function isDockerBuildXInstalled(
exec: ExecFunction,
): Promise<boolean> {
const {exitCode} = await exec('docker', ['buildx', '--help'], {silent: true});
return exitCode === 0;
}
export async function buildImage(
exec: ExecFunction,
imageName: string,
imageTag: string | undefined,
checkoutPath: string,
subFolder: string,
skipContainerUserIdUpdate: boolean,
cacheFrom: string[],
cacheTo: string[],
): Promise<string> {
const folder = path.join(checkoutPath, subFolder);
const devcontainerJsonPath = path.join(
folder,
'.devcontainer/devcontainer.json',
);
const devcontainerConfig = await config.loadFromFile(devcontainerJsonPath);
// build the image from the .devcontainer spec
await buildImageBase(
exec,
imageName,
imageTag,
folder,
devcontainerConfig,
cacheFrom,
cacheTo,
);
if (!devcontainerConfig.remoteUser || skipContainerUserIdUpdate == true) {
return imageName;
}
return await ensureHostAndContainerUsersAlign(
exec,
imageName,
imageTag,
devcontainerConfig,
);
}
function coerceToArray(value: string | string[]): string[] {
return typeof value === 'string' ? [value] : value;
}
async function buildImageBase(
exec: ExecFunction,
imageName: string,
imageTag: string | undefined,
folder: string,
devcontainerConfig: config.DevContainerConfig,
cacheFrom: string[],
cacheTo: string[],
): Promise<void> {
const configDockerfile = config.getDockerfile(devcontainerConfig);
if (!configDockerfile) {
throw new Error(
'dockerfile not set in devcontainer.json - devcontainer-build-run currently only supports Dockerfile-based dev containers',
);
}
const dockerfilePath = path.join(folder, '.devcontainer', configDockerfile);
const configContext = config.getContext(devcontainerConfig) ?? '';
const contextPath = path.join(folder, '.devcontainer', configContext);
const args = ['buildx', 'build'];
args.push('--tag');
args.push(`${imageName}:${imageTag ?? 'latest'}`);
args.push('--cache-from');
args.push(`type=registry,ref=${imageTag ?? 'latest'}`);
const configCacheFrom = devcontainerConfig.build?.cacheFrom;
if (configCacheFrom) {
coerceToArray(configCacheFrom).forEach(cacheValue =>
args.push('--cache-from', cacheValue),
);
}
cacheFrom.forEach(cacheValue => args.push('--cache-from', cacheValue));
if (cacheTo) {
coerceToArray(cacheTo).forEach(cacheValue =>
args.push('--cache-to', cacheValue),
);
} else {
args.push('--cache-to');
args.push('type=inline');
}
args.push('--output=type=docker');
const buildArgs = devcontainerConfig.build?.args;
for (const argName in buildArgs) {
const argValue = substituteValues(buildArgs[argName]);
args.push('--build-arg', `${argName}=${argValue}`);
}
args.push('-f', dockerfilePath);
args.push(contextPath);
const {exitCode} = await exec('docker', args, {});
if (exitCode !== 0) {
throw new Error(`build failed with ${exitCode}`);
}
}
// returns the name of the image to run in the next step
async function ensureHostAndContainerUsersAlign(
exec: ExecFunction,
imageName: string,
imageTag: string | undefined,
devcontainerConfig: config.DevContainerConfig,
): Promise<string> {
if (!devcontainerConfig.remoteUser) {
return imageName;
}
const resultHostUser = await exec('/bin/sh', ['-c', 'id -u -n'], {
silent: true,
});
if (resultHostUser.exitCode !== 0) {
throw new Error(
`Failed to get host user (exitcode: ${resultHostUser.exitCode}):${resultHostUser.stdout}\n${resultHostUser.stderr}`,
);
}
const resultHostPasswd = await exec('/bin/sh', ['-c', 'cat /etc/passwd'], {
silent: true,
});
if (resultHostPasswd.exitCode !== 0) {
throw new Error(
`Failed to get host user info (exitcode: ${resultHostPasswd.exitCode}):${resultHostPasswd.stdout}\n${resultHostPasswd.stderr}`,
);
}
const resultContainerPasswd = await exec(
'docker',
[
'run',
'--rm',
`${imageName}:${imageTag ?? 'latest'}`,
'sh',
'-c',
'cat /etc/passwd',
],
{silent: true},
);
if (resultContainerPasswd.exitCode !== 0) {
throw new Error(
`Failed to get container user info (exitcode: ${resultContainerPasswd.exitCode}):${resultContainerPasswd.stdout}\n${resultContainerPasswd.stderr}`,
);
}
const resultContainerGroup = await exec(
'docker',
[
'run',
'--rm',
`${imageName}:${imageTag ?? 'latest'}`,
'sh',
'-c',
'cat /etc/group',
],
{silent: true},
);
if (resultContainerGroup.exitCode !== 0) {
throw new Error(
`Failed to get container group info (exitcode: ${resultContainerGroup.exitCode}):${resultContainerGroup.stdout}\n${resultContainerGroup.stderr}`,
);
}
const hostUserName = resultHostUser.stdout.trim();
const hostUsers = parsePasswd(resultHostPasswd.stdout);
const hostUser = hostUsers.find(u => u.name === hostUserName);
if (!hostUser) {
console.log(`Host /etc/passwd:\n${resultHostPasswd.stdout}`);
throw new Error(
`Failed to find host user in host info. (hostUserName='${hostUserName}')`,
);
}
const containerUserName = devcontainerConfig.remoteUser;
const containerUsers = parsePasswd(resultContainerPasswd.stdout);
const containerGroups = parseGroup(resultContainerGroup.stdout);
const containerUser = containerUsers.find(u => u.name === containerUserName);
if (!containerUser) {
console.log(`Container /etc/passwd:\n${resultContainerPasswd.stdout}`);
throw new Error(
`Failed to find container user in container info. (containerUserName='${containerUserName}')`,
);
}
const existingContainerUserGroup = containerGroups.find(
g => g.gid == hostUser.gid,
);
if (existingContainerUserGroup)
throw new Error(
`Host user GID (${hostUser.gid}) already exists as a group in the container`,
);
const containerUserAligned =
hostUser.uid === containerUser.uid && hostUser.gid == containerUser.gid;
if (containerUserAligned) {
// all good - nothing to do
return imageName;
}
// Generate a Dockerfile to run to build a derived image with the UID/GID updated
const dockerfileContent = `FROM ${imageName}:${imageTag ?? 'latest'}
RUN sudo chown -R ${hostUser.uid}:${hostUser.gid} /home/${containerUserName} \
&& sudo sed -i /etc/passwd -e s/${containerUser.name}:x:${
containerUser.uid
}:${containerUser.gid}/${containerUser.name}:x:${hostUser.uid}:${
hostUser.gid
}/
`;
const tempDir = fs.mkdtempSync(
path.join(os.tmpdir(), 'tmp-devcontainer-build-run'),
);
const derivedDockerfilePath = path.join(tempDir, 'Dockerfile');
fs.writeFileSync(derivedDockerfilePath, dockerfileContent);
const derivedImageName = `${imageName}-userfix`;
// TODO - `buildx build` was giving issues when building an image for the first time and it is unable to
// pull the image from the registry
// const derivedDockerBuild = await exec('docker', ['buildx', 'build', '--tag', derivedImageName, '-f', derivedDockerfilePath, tempDir, '--output=type=docker'], {})
const derivedDockerBuild = await exec(
'docker',
[
'build',
'--tag',
`${derivedImageName}:${imageTag ?? 'latest'}`,
'-f',
derivedDockerfilePath,
tempDir,
'--output=type=docker',
],
{},
);
if (derivedDockerBuild.exitCode !== 0) {
throw new Error('Failed to build derived Docker image with users updated');
}
return derivedImageName;
}
export async function runContainer(
exec: ExecFunction,
imageName: string,
imageTag: string | undefined,
checkoutPath: string,
subFolder: string,
command: string,
envs?: string[],
mounts?: string[],
): Promise<void> {
const checkoutPathAbsolute = getAbsolutePath(checkoutPath, process.cwd());
const folder = path.join(checkoutPathAbsolute, subFolder);
const devcontainerJsonPath = path.join(
folder,
'.devcontainer/devcontainer.json',
);
const devcontainerConfig = await config.loadFromFile(devcontainerJsonPath);
const workspaceFolder = config.getWorkspaceFolder(
devcontainerConfig,
checkoutPathAbsolute,
);
const workdir = path.join(workspaceFolder, subFolder);
const remoteUser = config.getRemoteUser(devcontainerConfig);
const args = ['run', '--rm'];
args.push('--label', `github.com/devcontainers/ci/`);
args.push(
'--mount',
`type=bind,src=${checkoutPathAbsolute},dst=${workspaceFolder}`,
);
if (devcontainerConfig.mounts) {
devcontainerConfig.mounts
.map(m => substituteValues(m))
.forEach(m => {
const mount = parseMount(m);
if (mount.type === 'bind') {
// check path exists
if (!fs.existsSync(mount.source)) {
console.log(`Skipping mount as source does not exist: '${m}'`);
return;
}
}
args.push('--mount', m);
});
}
args.push('--workdir', workdir);
args.push('--user', remoteUser);
if (devcontainerConfig.runArgs) {
const substitutedRunArgs = devcontainerConfig.runArgs.map(a =>
substituteValues(a),
);
args.push(...substitutedRunArgs);
}
if (envs) {
for (const env of envs) {
args.push('--env', env);
}
}
args.push(`${imageName}:${imageTag ?? 'latest'}`);
args.push('bash', '-c', command);
const {exitCode} = await exec('docker', args, {});
if (exitCode !== 0) {
throw new Error(`run failed with ${exitCode}`);
}
}
export async function pushImage(
exec: ExecFunction,
imageName: string,
imageTag: string | undefined,
): Promise<void> {
const args = ['push'];
args.push(`${imageName}:${imageTag ?? 'latest'}`);
const {exitCode} = await exec('docker', args, {});
if (exitCode !== 0) {
throw new Error(`push failed with ${exitCode}`);
}
}
export async function createManifest(
exec: ExecFunction,
imageName: string,
tag: string,
platformTags: string[],
): Promise<void> {
const args = ['buildx', 'imagetools', 'create'];
args.push('-t', `${imageName}:${tag}`);
for (const platformTag of platformTags) {
args.push(`${imageName}:${tag}-${platformTag}`);
}
const {exitCode} = await exec('docker', args, {});
if (exitCode !== 0) {
throw new Error(`manifest creation failed with ${exitCode}`);
}
}
export interface DockerMount {
type: string;
source: string;
target: string;
// ignoring readonly as not relevant
}
export function parseMount(mountString: string): DockerMount {
// https://docs.docker.com/engine/reference/commandline/service_create/#add-bind-mounts-volumes-or-memory-filesystems
// examples:
// type=bind,source=/var/run/docker.sock,target=/var/run/docker.sock
// src=home-cache,target=/home/vscode/.cache
let type = '';
let source = '';
let target = '';
const options = mountString.split(',');
for (const option of options) {
const parts = option.split('=');
switch (parts[0]) {
case 'type':
type = parts[1];
break;
case 'src':
case 'source':
source = parts[1];
break;
case 'dst':
case 'destination':
case 'target':
target = parts[1];
break;
case 'readonly':
case 'ro':
// ignore
break;
default:
throw new Error(`Unhandled mount option '${parts[0]}'`);
}
}
return {type, source, target};
}