Add support for relative paths in the volume destination too
MODIFY
repo2docker/app.py
repo2docker/app.py
@@ -109,7 +109,10 @@ class Repo2Docker(Application):
Only used when running, not during build!
Should be a key value pair, with the key being the volume source &
- value being the destination.
+ value being the destination. Both can be relative - sources are
+ resolved relative to the current working directory on the host,
+ destination is resolved relative to the working directory of the image -
+ ($HOME by default)
""",
config=True
)
@@ -331,6 +334,8 @@ class Repo2Docker(Application):
def run_image(self):
client = docker.from_env(version='auto')
+ api_client = docker.APIClient(version='auto',
+ **docker.utils.kwargs_from_env())
port = self._get_free_port()
if not self.run_cmd:
port = str(self._get_free_port())
@@ -340,16 +345,23 @@ class Repo2Docker(Application):
else:
run_cmd = self.run_cmd
ports = {}
- volumes = {
- os.path.abspath(k): { 'bind': v, 'mode': 'rw'}
- for k, v in self.volumes.items()
+ container_volumes = {}
+ if self.volumes:
+ image = api_client.inspect_image(self.output_image_spec)
+ image_workdir = image['ContainerConfig']['WorkingDir']
+
+ for k, v in self.volumes.items():
+ container_volumes[os.path.abspath(k)] = {
+ 'bind': v if v.startswith('/') else os.path.join(image_workdir, v),
+ 'mode': 'rw'
}
+
container = client.containers.run(
self.output_image_spec,
ports=ports,
detach=True,
command=run_cmd,
- volumes=volumes
+ volumes=container_volumes
)
while container.status == 'created':
time.sleep(0.5)

MODIFY
tests/volumes.py
tests/volumes.py
@@ -6,9 +6,9 @@ import subprocess
import tempfile
import time
-def test_volume_home():
+def test_volume_abspath():
"""
- Validate that you can bind mount a volume onto homedirectory & write to it
+ Validate that you can bind mount a volume onto an absolute dir & write to it
"""
ts = str(time.time())
with tempfile.TemporaryDirectory() as tmpdir:
@@ -23,3 +23,27 @@ def test_volume_home():
with open(os.path.join(tmpdir, 'ts')) as f:
assert f.read() == ts
+
+
+def test_volume_relpath():
+ """
+ Validate that you can bind mount a volume onto an relative path & write to it
+ """
+ curdir = os.getcwd()
+ try:
+ ts = str(time.time())
+ with tempfile.TemporaryDirectory() as tmpdir:
+ os.chdir(tmpdir)
+ subprocess.check_call([
+ 'repo2docker',
+ '-v', '.:.',
+ tmpdir,
+ '--',
+ '/bin/bash',
+ '-c', 'echo -n {} > ts'.format(ts)
+ ])
+
+ with open(os.path.join(tmpdir, 'ts')) as f:
+ assert f.read() == ts
+ finally:
+ os.chdir(curdir)

Add support for relative paths in the volume destination too