Merge pull request #3670 from aanand/3343-run-with-env-values-from-system

[Carry #3373] Unset env vars behavior in 'run' mirroring engine
This commit is contained in:
Aanand Prasad 2016-07-01 12:12:29 -07:00 committed by GitHub
commit f1974f6c5e
3 changed files with 28 additions and 1 deletions

View File

@ -19,6 +19,7 @@ from ..bundle import MissingDigests
from ..bundle import serialize_bundle
from ..config import ConfigurationError
from ..config import parse_environment
from ..config.environment import Environment
from ..config.serialize import serialize_config
from ..const import DEFAULT_TIMEOUT
from ..const import IS_WINDOWS_PLATFORM
@ -891,7 +892,9 @@ def build_container_options(options, detach, command):
}
if options['-e']:
container_options['environment'] = parse_environment(options['-e'])
container_options['environment'] = Environment.from_command_line(
parse_environment(options['-e'])
)
if options['--entrypoint']:
container_options['entrypoint'] = options.get('--entrypoint')

View File

@ -60,6 +60,18 @@ class Environment(dict):
instance.update(os.environ)
return instance
@classmethod
def from_command_line(cls, parsed_env_opts):
result = cls()
for k, v in parsed_env_opts.items():
# Values from the command line take priority, unless they're unset
# in which case they take the value from the system's environment
if v is None and k in os.environ:
result[k] = os.environ[k]
else:
result[k] = v
return result
def __getitem__(self, key):
try:
return super(Environment, self).__getitem__(key)

View File

@ -1216,6 +1216,18 @@ class CLITestCase(DockerClientTestCase):
'simplecomposefile_simple_run_1',
'exited'))
@mock.patch.dict(os.environ)
def test_run_env_values_from_system(self):
os.environ['FOO'] = 'bar'
os.environ['BAR'] = 'baz'
self.dispatch(['run', '-e', 'FOO', 'simple', 'true'], None)
container = self.project.containers(one_off=OneOffFilter.only, stopped=True)[0]
environment = container.get('Config.Env')
assert 'FOO=bar' in environment
assert 'BAR=baz' not in environment
def test_rm(self):
service = self.project.get_service('simple')
service.create_container()