Allow mounting arbitrary volumes into the repo2docker container

Tries to mimick the docker run '-v' syntax, but only supports
rw volumes for now. Source can be a relative path, but destination
needs to be an absolute path.
pull/172/head
yuvipanda 2017-12-19 11:02:27 -08:00
rodzic 1c7bd6e589
commit e6a13eb8ba
1 zmienionych plików z 36 dodań i 2 usunięć

Wyświetl plik

@ -19,7 +19,7 @@ import escapism
from traitlets.config import Application
from traitlets import Unicode, List, default, Tuple
from traitlets import Unicode, List, default, Tuple, Dict
import docker
from docker.utils import kwargs_from_env
@ -101,6 +101,19 @@ class Repo2Docker(Application):
config=True
)
volumes = Dict(
{},
help="""
Volumes to mount when running the container.
Only used when running, not during build!
Should be a key value pair, with the key being the volume source &
value being the destination.
""",
config=True
)
def fetch(self, url, ref, checkout_path):
try:
for line in execute_cmd(['git', 'clone', url, checkout_path],
@ -200,6 +213,13 @@ class Repo2Docker(Application):
help='Push docker image to repository'
)
argparser.add_argument(
'--volume', '-v',
dest='volumes',
action='append',
help='Volumes to mount inside the container, in form src:dest'
)
return argparser
def json_excepthook(self, etype, evalue, traceback):
@ -272,6 +292,15 @@ class Repo2Docker(Application):
self.run = False
self.push = False
if args.volumes and not args.run:
# Can't mount if we aren't running
print("Can not mount volumes when not running container")
sys.exit(1)
for v in args.volumes:
src, dest = v.split(':')
self.volumes[src] = dest
self.run_cmd = args.cmd
if args.build_memory_limit:
@ -310,11 +339,16 @@ 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 = client.containers.run(
self.output_image_spec,
ports=ports,
detach=True,
command=run_cmd
command=run_cmd,
volumes=volumes
)
while container.status == 'created':
time.sleep(0.5)