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

Implement Subpath Support for Volumes in Docker-Py (#3243) #3270

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
6 changes: 5 additions & 1 deletion docker/api/volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def volumes(self, filters=None):
url = self._url('/volumes')
return self._result(self._get(url, params=params), True)

def create_volume(self, name=None, driver=None, driver_opts=None,
def create_volume(self, name=None, driver=None, driver_opts=None, subpath=None,
labels=None):
"""
Create and register a named volume
Expand All @@ -45,6 +45,9 @@ def create_volume(self, name=None, driver=None, driver_opts=None,
driver (str): Name of the driver used to create the volume
driver_opts (dict): Driver options as a key-value dictionary
labels (dict): Labels to set on the volume
subpath (str): Subpath within the volume to use as the volume's
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd change this to

subpath (str): Path inside a volume to mount instead of the volume root.

To better match documentation we have elsewhere (eg https://github.com/docker/docs/blob/23f3fbd0639a24d0e2280fd0edb7f72b3f0cb1ca/content/reference/compose-file/services.md?plain=1#L1865)

@dvdksn WDYT?

Copy link

Choose a reason for hiding this comment

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

SGTM

root.


Returns:
(dict): The created volume reference object
Expand Down Expand Up @@ -77,6 +80,7 @@ def create_volume(self, name=None, driver=None, driver_opts=None,
'Name': name,
'Driver': driver,
'DriverOpts': driver_opts,
'Subpath': subpath
}

if labels is not None:
Expand Down
10 changes: 7 additions & 3 deletions docker/types/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,16 @@ class Mount(dict):
for the ``volume`` type.
driver_config (DriverConfig): Volume driver configuration. Only valid
for the ``volume`` type.
subpath (string): The path within the volume where the container's
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd keep this the same as the suggestion above ☝️

volume should be mounted.
tmpfs_size (int or string): The size for the tmpfs mount in bytes.
tmpfs_mode (int): The permission mode for the tmpfs mount.
"""

def __init__(self, target, source, type='volume', read_only=False,
consistency=None, propagation=None, no_copy=False,
labels=None, driver_config=None, tmpfs_size=None,
tmpfs_mode=None):
tmpfs_mode=None, subpath=None):
self['Target'] = target
self['Source'] = source
if type not in ('bind', 'volume', 'tmpfs', 'npipe'):
Expand All @@ -267,7 +269,7 @@ def __init__(self, target, source, type='volume', read_only=False,
self['BindOptions'] = {
'Propagation': propagation
}
if any([labels, driver_config, no_copy, tmpfs_size, tmpfs_mode]):
if any([labels, driver_config, no_copy, tmpfs_size, tmpfs_mode, subpath]):
raise errors.InvalidArgument(
'Incompatible options have been provided for the bind '
'type mount.'
Expand All @@ -280,6 +282,8 @@ def __init__(self, target, source, type='volume', read_only=False,
volume_opts['Labels'] = labels
if driver_config:
volume_opts['DriverConfig'] = driver_config
if subpath:
volume_opts['SubPath'] = subpath
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this should be written as Subpath here, right @vvoland?

Copy link
Contributor Author

@Khushiyant Khushiyant Sep 17, 2024

Choose a reason for hiding this comment

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

Yeah, it is Subpath instead of SubPath: moby#45687. I'll change this quick

if volume_opts:
self['VolumeOptions'] = volume_opts
if any([propagation, tmpfs_size, tmpfs_mode]):
Expand All @@ -299,7 +303,7 @@ def __init__(self, target, source, type='volume', read_only=False,
tmpfs_opts['SizeBytes'] = parse_bytes(tmpfs_size)
if tmpfs_opts:
self['TmpfsOptions'] = tmpfs_opts
if any([propagation, labels, driver_config, no_copy]):
if any([propagation, labels, driver_config, no_copy, subpath]):
raise errors.InvalidArgument(
'Incompatible options have been provided for the tmpfs '
'type mount.'
Expand Down
23 changes: 23 additions & 0 deletions tests/integration/api_container_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,29 @@ def test_create_with_volume_mount(self):
assert mount['Source'] == mount_data['Name']
assert mount_data['RW'] is True

@requires_api_version('1.45')
def test_create_with_subpath_volume_mount(self):
mount = docker.types.Mount(
type="volume", source=helpers.random_name(),
target=self.mount_dest, read_only=True,
subpath='subdir'
Copy link
Contributor

Choose a reason for hiding this comment

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

From a quick glance, it doesn't seem that the subpath is being checked in this test

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Made changes accordingly

)
host_config = self.client.create_host_config(mounts=[mount])
container = self.client.create_container(
TEST_IMG, ['true'], host_config=host_config,
)
assert container
inspect_data = self.client.inspect_container(container)
assert 'Mounts' in inspect_data
filtered = list(filter(
lambda x: x['Destination'] == self.mount_dest,
inspect_data['Mounts']
))
assert len(filtered) == 1
mount_data = filtered[0]
assert mount['Source'] == mount_data['Name']
assert mount_data['RW'] is False

def check_container_data(self, inspect_data, rw, propagation='rprivate'):
assert 'Mounts' in inspect_data
filtered = list(filter(
Expand Down