Provide a flag to pass environment variables at runtime
MODIFY
repo2docker/app.py
repo2docker/app.py
@@ -297,6 +297,14 @@ class Repo2Docker(Application):
help='Username of the primary user in the image',
)
+ argparser.add_argument(
+ '--env', '-e',
+ dest='environment',
+ nargs='+',
+ help='Environment variables to define at container run time',
+ default=[]
+ )
+
return argparser
def json_excepthook(self, etype, evalue, traceback):
@@ -389,6 +397,12 @@ class Repo2Docker(Application):
if args.build_memory_limit:
self.build_memory_limit = args.build_memory_limit
+ if args.environment and not self.run:
+ print("To specify environment variables, you also need to run the container")
+ sys.exit(1)
+
+ self.environment = args.environment
+
def push_image(self):
client = docker.APIClient(version='auto', **kwargs_from_env())
# Build a progress setup for each layer, and only emit per-layer
@@ -442,7 +456,8 @@ class Repo2Docker(Application):
ports=ports,
detach=True,
command=run_cmd,
- volumes=container_volumes
+ volumes=container_volumes,
+ environment=self.environment
)
while container.status == 'created':
time.sleep(0.5)

MODIFY
tests/argumentvalidation.py
tests/argumentvalidation.py
@@ -119,3 +119,13 @@ def test_volume_no_run_fail():
args_list = ['--no-run', '-v', '/data:/data']
assert not validate_arguments(builddir, args_list, 'To Mount volumes with -v, you also need to run the container')
+
+def test_env_no_run_fail():
+ """
+ Test to check if repo2docker fails when both --no-run and -e arguments are given
+ """
+ builddir = os.path.dirname(__file__)
+ args_list = ['--no-run', '-e', 'FOO=bar', '--']
+
+ assert not validate_arguments(builddir, args_list, 'To specify environment variables, you also need to run the container')
+

ADD
tests/env.py
+"""
+Test that volume mounts work when running
+"""
+import os
+import subprocess
+import tempfile
+import time
+
+def test_env():
+ """
+ Validate that you can define environment variables
+ """
+ ts = str(time.time())
+ with tempfile.TemporaryDirectory() as tmpdir:
+ username = os.getlogin()
+ subprocess.check_call([
+ 'repo2docker',
+ '-v', '{}:/home/{}'.format(tmpdir, username),
+ '-e', 'FOO={}'.format(ts), 'BAR=baz',
+ '--',
+ tmpdir,
+ '/bin/bash',
+ '-c', 'echo -n $FOO > ts && echo -n $BAR > bar'
+ ])
+
+ with open(os.path.join(tmpdir, 'ts')) as f:
+ assert f.read().strip() == ts
+ with open(os.path.join(tmpdir, 'bar')) as f:
+ assert f.read().strip() == 'baz'
+

Provide a flag to pass environment variables at runtime