-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
base: main
Are you sure you want to change the base?
Conversation
Adds support for specifying a subpath when creating a container This allows users to specify a subdirectory within the volume to use as the container's root. Changes: * Added `subpath` parameter to `create_container` method in `docker/api/container.py` * Updated `Mount` class in `docker/types/mounts.py` to include `subpath` attribute * Modified `Container` model in `docker/models/containers.py` to include `subpath` attribute BREAKING CHANGE: None TESTED: Yes, added unit tests to verify subpath functionality Signed-off-by: Khushiyant <khushiyant2002@gmail.com>
32bb4c5
to
5edcbe4
Compare
Hello, can this PR be merged ASAP? |
@rayrapetyan Even, I'm also waiting for the review |
@thaJeztah @krissetto, could you provide a review please, this seems like a very simple and straightforward change. Thanks! |
@Khushiyant, could you re-trigger the build as seems one of the integration tests has failed before. |
Signed-off-by: Khushiyant <khushiyant2002@gmail.com>
6b8bbb5
to
b9fc1ea
Compare
Yeah, I triggered the build and now, all tests have passed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the contribution!
I gave this a quick look for now, we'll try to get it in soon
@@ -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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SGTM
@@ -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 |
There was a problem hiding this comment.
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 ☝️
mount = docker.types.Mount( | ||
type="volume", source=helpers.random_name(), | ||
target=self.mount_dest, read_only=True, | ||
subpath='subdir' |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Made changes accordingly
docker/types/services.py
Outdated
@@ -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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
@@ -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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change the SubPath
to Subpath
mount_data = filtered[0] | ||
assert mount['Source'] == mount_data['Name'] | ||
assert mount_data['RW'] is False | ||
assert mount["VolumeOptions"]["Subpath"] == "subdir" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is testing that the input mount options, as defined above on line 628, are the same.
The test needs to check that the path mounted inside the container corresponds to the subdir as defined in the mount options. The volume mounted must therefore contain the subdir
, or else there should be an error.
Maybe you could change to check mount_data
instead, if it contains the necessary info? I don't have much time now but I'd have to check how these volume tests are defined in a bit more detail later
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, sure. Will do it later today
Closes #3243
This PR aims to implement subpath support for volumes in Docker-Py, allowing users to specify a subpath when creating a volume or a container. This feature is essential for scenarios where a container needs to access a specific directory within a volume.
Added a
subpath
parameter to thecreate_volume
method indocker/api/volume.py
.Updated the
Volume
model indocker/models/volumes.py
to include a subpath attribute.Modified the
create_container
method indocker/api/container.py
to include thesubpath
parameter when creating a mount.Updated the
Mount
class indocker/types/mounts.py
to include asubpath
attribute.