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

Rework mount command string generation #537

Merged
Show file tree
Hide file tree
Changes from 3 commits
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
22 changes: 22 additions & 0 deletions src/spec-node/dockerfileUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*--------------------------------------------------------------------------------------------*/

import * as semver from 'semver';
import { Mount } from '../spec-configuration/containerFeaturesConfiguration';

export { getConfigFilePath, getDockerfilePath, isDockerFileConfig, resolveConfigFilePath } from '../spec-configuration/configuration';
export { uriToFsPath, parentURI } from '../spec-configuration/configurationCommonUtils';
Expand Down Expand Up @@ -270,3 +271,24 @@ export function supportsBuildContexts(dockerfile: Dockerfile) {
}
return semver.intersects(numVersion, '>=1.4');
}

/**
* Process the mount command arguments and return the command string
* @param mount
* @returns command string
*/
export function generateMountCommand(mount: Mount | string): string[] {
const command: string = '--mount';

if (typeof mount === 'string') {
return [command, mount];
}

const type: string = `type=${mount.type},`;
const source: string = mount.source ? `src=${mount.source},` : '';
const destination: string = `dst=${mount.target}`;

const args: string = `${type}${source}${destination}`;

return [command, args];
}
4 changes: 2 additions & 2 deletions src/spec-node/singleContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { DevContainerConfig, DevContainerFromDockerfileConfig, DevContainerFromI
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 } from './dockerfileUtils';
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';
Expand Down Expand Up @@ -360,7 +360,7 @@ export async function spawnDevContainer(params: DockerResolverParameters, config
...[
...mergedConfig.mounts || [],
...params.additionalMounts,
].map(m => ['--mount', typeof m === 'string' ? m : `type=${m.type},src=${m.source},dst=${m.target}`])
].map(m => generateMountCommand(m))
);

const customEntrypoints = mergedConfig.entrypoints || [];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"image": "mcr.microsoft.com/devcontainers/base:latest",
"mounts": [
{
"target": "/home/vscode/.vscode-server",
"type": "volume",
}
]
}
15 changes: 15 additions & 0 deletions src/test/imageMetadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,21 @@ describe('Image Metadata', function () {
await shellExec(`docker rm -f ${response.containerId}`);
});
});

['image-with-mounts'].forEach(testFolderName => {
it('docker volume should not be named undefined if the src argument is omitted in mount command', async () => {
const imageTestFolder = `${__dirname}/configs/${testFolderName}`;
const cliResult = await shellExec(`${cli} up --workspace-folder ${imageTestFolder}`);
const response = JSON.parse(cliResult.stdout);
assert.strictEqual(response.outcome, 'success');
const details = JSON.parse((await shellExec(`docker inspect ${response.containerId}`)).stdout)[0] as ContainerDetails;
const targetMount = details.Mounts.find(mount => mount.Destination === '/home/vscode/.vscode-server');

assert.notEqual(targetMount?.Name?.toLowerCase(), 'undefined');

await shellExec(`docker rm -f ${response.containerId}`);
});
});
});
});

Expand Down