Skip to content

Commit fc10eb3

Browse files
committed
Added labels to build with dockerfile
1 parent e627d17 commit fc10eb3

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

src/spec-node/singleContainer.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,10 @@ async function buildAndExtendImage(buildParams: DockerResolverParameters, config
244244
}
245245
}
246246
}
247+
248+
// Add labels
249+
args.push(...buildParams.additionalLabels.length > 0 ? buildParams.additionalLabels.map(label => ['--label', label]).flat() : []);
250+
247251
const buildArgs = config.build?.args;
248252
if (buildArgs) {
249253
for (const key in buildArgs) {

src/test/cli.build.test.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,26 @@ describe('Dev Containers CLI', function () {
2727

2828
describe('Command build', () => {
2929

30-
it('should build successfully with valid image metadata --label property', async () => {
30+
it('should build successfully with valid image metadata --label property (image)', async () => {
3131
const testFolder = `${__dirname}/configs/example`;
3232
const response = await shellExec(`${cli} build --workspace-folder ${testFolder} --label 'name=label-test' --label 'type=multiple-labels'`);
3333
const res = JSON.parse(response.stdout);
3434
assert.equal(res.outcome, 'success');
35-
const labels = await shellExec(`docker inspect --format '{{json .Config.Labels}}' ${res.imageName} | jq`);
36-
assert.match(labels.stdout.toString(), /\"name\": \"label-test\"/);
35+
const labelsResponse = await shellExec(`docker inspect --format '{{json .Config.Labels}}' ${res.imageName}`);
36+
const labels = JSON.parse(labelsResponse.stdout);
37+
assert.equal(labels.name, 'label-test');
38+
assert.equal(labels.type, 'multiple-labels');
39+
});
40+
41+
it('should build successfully with valid image metadata --label property (dockerfile)', async () => {
42+
const testFolder = `${__dirname}/configs/example-dockerfile`;
43+
const response = await shellExec(`${cli} build --workspace-folder ${testFolder} --label 'name=label-test' --label 'type=multiple-labels'`);
44+
const res = JSON.parse(response.stdout);
45+
assert.equal(res.outcome, 'success');
46+
const labelsResponse = await shellExec(`docker inspect --format '{{json .Config.Labels}}' ${res.imageName}`);
47+
const labels = JSON.parse(labelsResponse.stdout);
48+
assert.equal(labels.name, 'label-test');
49+
assert.equal(labels.type, 'multiple-labels');
3750
});
3851

3952
it('should fail to build with correct error message for local feature', async () => {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Example devcontainer.json configuration with a Dockerfile
2+
{
3+
"build": {
4+
"dockerfile": "Dockerfile"
5+
},
6+
"features": {
7+
"ghcr.io/devcontainers/features/go:1": {
8+
"version": "latest"
9+
}
10+
}
11+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
FROM mcr.microsoft.com/devcontainers/base:bookworm

0 commit comments

Comments
 (0)