Provide a flag to pass environment variables at runtime

pull/186/head
Romain Primet 2018-01-07 15:43:03 +00:00
rodzic ed6b483e2f
commit 5f95a5ddd3
3 zmienionych plików z 57 dodań i 2 usunięć

Wyświetl plik

@ -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)

Wyświetl plik

@ -118,4 +118,14 @@ def test_volume_no_run_fail():
builddir = os.path.dirname(__file__)
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')
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')

30
tests/env.py 100644
Wyświetl plik

@ -0,0 +1,30 @@
"""
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'