Skip to content

Rework mount command string generation - #537

Merged
Samruddhi Khandale (samruddhikhandale) merged 15 commits into
devcontainers:mainfrom
alexander-smolyakov:users/alexander-smolyakov/add_proper_handling_for_src_arg_in_mount_command
May 30, 2023
Merged

Samruddhi Khandale (samruddhikhandale) merged 15 commits into
devcontainers:mainfrom
alexander-smolyakov:users/alexander-smolyakov/add_proper_handling_for_src_arg_in_mount_command

Conversation

@alexander-smolyakov

Copy link
Copy Markdown
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;

Copy link
Copy Markdown
Member

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
Copy Markdown
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

@joshspicer joshspicer left a comment

Copy link
Copy Markdown
Contributor

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 (@alexander-smolyakov) !

@joshspicer

Copy link
Copy Markdown
Contributor

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?

@chrmarti Christof Marti (chrmarti) left a comment

Copy link
Copy Markdown
Contributor

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;
Comment thread src/spec-node/dockerCompose.ts Resolved
Comment thread src/spec-node/dockerfileUtils.ts Resolved

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks great, thank you!

@chrmarti Christof Marti (chrmarti) left a comment

Copy link
Copy Markdown
Contributor

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.

Comment thread src/spec-node/dockerCompose.ts Resolved
Comment thread src/spec-node/dockerCompose.ts Resolved
Comment thread src/spec-node/dockerCompose.ts Resolved
Comment thread src/spec-node/dockerCompose.ts Resolved
@samruddhikhandale
Samruddhi Khandale (samruddhikhandale) merged commit ae783be into devcontainers:main May 30, 2023
Eljo George (eljog) added a commit that referenced this pull request Jun 2, 2023
Eljo George (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

4 participants