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

Conversation

alexander-smolyakov
Copy link
Contributor

Description:

This PR addresses the issue when the Docker volume is named as undefined if the src argument is not specified in the mount command.

The issue related to the following code:

const featureMounts = ([] as string[]).concat(
...[
...mergedConfig.mounts || [],
...params.additionalMounts,
].map(m => ['--mount', typeof m === 'string' ? m : `type=${m.type},src=${m.source},dst=${m.target}`])
);

When the mount command string is generated, there is no check for the src argument. This results in the src attribute being set to undefined when cast to a string.

To fix this behavior, we need to apply null-check for the src argument and don't pass it to Docker. When the src arg is not passed, Docker generates a random name for the volume.

Repro sample:

{
	"image": "mcr.microsoft.com/devcontainers/base:latest",
	"mounts": [
        {
            "target": "/home/vscode/.vscode-server",
            "type": "volume",
        }
    ]
}

Examples:
Without fix:

node ➜ /workspaces/devcontainers-cli (main) $ docker inspect -f '{{json .Mounts }}' a85f6e52d478
[
	{
		"Type": "bind",
		"Source": "/workspaces/devcontainers-cli",
		"Destination": "/workspaces/devcontainers-cli",
		"Mode": "",
		"RW": true,
		"Propagation": "rprivate"
	},
	{
		"Type": "volume",
		"Name": "undefined",
		"Source": "/var/lib/docker/volumes/undefined/_data",
		"Destination": "/home/vscode/.vscode-server",
		"Driver": "local",
		"Mode": "z",
		"RW": true,
		"Propagation": ""
	}
]

With fix:

node ➜ /workspaces/devcontainers-cli (users/alexander-smolyakov/add_proper_handling_for_src_arg_in_mount_command) $ docker inspect -f '{{json .Mounts }}' 2e77ff5392dc
[
	{
		"Type": "bind",
		"Source": "/workspaces/devcontainers-cli",
		"Destination": "/workspaces/devcontainers-cli",
		"Mode": "",
		"RW": true,
		"Propagation": "rprivate"
	},
	{
		"Type": "volume",
		"Name": "628f29020e5d6a9514614ce0ad90411d2400161b67e07bb8744f39a83a397922",
		"Source": "/var/lib/docker/volumes/628f29020e5d6a9514614ce0ad90411d2400161b67e07bb8744f39a83a397922/_data",
		"Destination": "/home/vscode/.vscode-server",
		"Driver": "local",
		"Mode": "z",
		"RW": true,
		"Propagation": ""
	}
]

Attached related issue:

Changelog:

  • Move the mount command generation logic to a separate function;
  • Add check if the src argument is undefined;

- Move command generation logic to separate function;
- Add check if `src` argument is undefined;
@alexander-smolyakov alexander-smolyakov requested a review from a team as a code owner May 23, 2023 15:41
Copy link
Member

@samruddhikhandale samruddhikhandale left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, can you help add a unit test to validate this fix?

@alexander-smolyakov
Copy link
Contributor Author

Thanks, can you help add a unit test to validate this fix?

Added test in 7fc4270

joshspicer
joshspicer previously approved these changes May 25, 2023
Copy link
Member

@joshspicer joshspicer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me - thanks @alexander-smolyakov !

@joshspicer
Copy link
Member

Here's the spec article on anonymous volumes https://docs.docker.com/storage/volumes/#choose-the--v-or---mount-flag. Not sure why this is particularly useful, I guess so as to not clutter up a container's filesystem between starts/stops?

Copy link
Contributor

@chrmarti chrmarti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking into this!

We will also need to update the schemas to make source optional: https://github.com/devcontainers/spec/tree/main/schemas

- Make `source` property optional in `Mount` interface;
- Move logic for converting mount to volume to separate function;
- Add test to cover docker compose case;
src/spec-node/dockerCompose.ts Outdated Show resolved Hide resolved
src/spec-node/dockerfileUtils.ts Outdated Show resolved Hide resolved
Copy link
Member

@samruddhikhandale samruddhikhandale left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thank you!

Copy link
Contributor

@chrmarti chrmarti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. A few polishing suggestions. Thanks.

src/spec-node/dockerCompose.ts Outdated Show resolved Hide resolved
src/spec-node/dockerCompose.ts Outdated Show resolved Hide resolved
src/spec-node/dockerCompose.ts Outdated Show resolved Hide resolved
src/spec-node/dockerCompose.ts Outdated Show resolved Hide resolved
@samruddhikhandale samruddhikhandale merged commit ae783be into devcontainers:main May 30, 2023
eljog added a commit that referenced this pull request Jun 2, 2023
eljog added a commit that referenced this pull request Jun 2, 2023
* 0.43.0

* add #537 to changelog
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants