Skip to content

Commit

Permalink
Pass environment when calling through to docker cli.
Browse files Browse the repository at this point in the history
This ensures that settings from any `.env` file (such as `DOCKER_HOST`) are
passed on to the cli.

Unit tests are adjusted for the new parameter and a new case is added to ensure
it is propagated as expected.

Fixes: 6661

Signed-off-by: Ian Campbell <[email protected]>
  • Loading branch information
Ian Campbell committed May 23, 2019
1 parent 4218b46 commit 9d2508c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
9 changes: 4 additions & 5 deletions compose/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ def exec_command(self, options):
if IS_WINDOWS_PLATFORM or use_cli and not detach:
sys.exit(call_docker(
build_exec_command(options, container.id, command),
self.toplevel_options)
self.toplevel_options, environment)
)

create_exec_options = {
Expand Down Expand Up @@ -1361,7 +1361,6 @@ def remove_container(force=False):
environment_file = options.get('--env-file')
environment = Environment.from_env_file(project_dir, environment_file)
use_cli = not environment.get_boolean('COMPOSE_INTERACTIVE_NO_CLI')

signals.set_signal_handler_to_shutdown()
signals.set_signal_handler_to_hang_up()
try:
Expand All @@ -1370,7 +1369,7 @@ def remove_container(force=False):
service.connect_container_to_networks(container, use_network_aliases)
exit_code = call_docker(
["start", "--attach", "--interactive", container.id],
toplevel_options
toplevel_options, environment
)
else:
operation = RunOperation(
Expand Down Expand Up @@ -1450,7 +1449,7 @@ def exit_if(condition, message, exit_code):
raise SystemExit(exit_code)


def call_docker(args, dockeropts):
def call_docker(args, dockeropts, environment):
executable_path = find_executable('docker')
if not executable_path:
raise UserError(errors.docker_not_found_msg("Couldn't find `docker` binary."))
Expand Down Expand Up @@ -1480,7 +1479,7 @@ def call_docker(args, dockeropts):
args = [executable_path] + tls_options + args
log.debug(" ".join(map(pipes.quote, args)))

return subprocess.call(args)
return subprocess.call(args, env=environment)


def parse_scale_args(options):
Expand Down
21 changes: 15 additions & 6 deletions tests/unit/cli/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,13 @@ def mock_find_executable(exe):
class TestCallDocker(object):
def test_simple_no_options(self):
with mock.patch('subprocess.call') as fake_call:
call_docker(['ps'], {})
call_docker(['ps'], {}, {})

assert fake_call.call_args[0][0] == ['docker', 'ps']

def test_simple_tls_option(self):
with mock.patch('subprocess.call') as fake_call:
call_docker(['ps'], {'--tls': True})
call_docker(['ps'], {'--tls': True}, {})

assert fake_call.call_args[0][0] == ['docker', '--tls', 'ps']

Expand All @@ -140,7 +140,7 @@ def test_advanced_tls_options(self):
'--tlscacert': './ca.pem',
'--tlscert': './cert.pem',
'--tlskey': './key.pem',
})
}, {})

assert fake_call.call_args[0][0] == [
'docker', '--tls', '--tlscacert', './ca.pem', '--tlscert',
Expand All @@ -149,24 +149,33 @@ def test_advanced_tls_options(self):

def test_with_host_option(self):
with mock.patch('subprocess.call') as fake_call:
call_docker(['ps'], {'--host': 'tcp://mydocker.net:2333'})
call_docker(['ps'], {'--host': 'tcp://mydocker.net:2333'}, {})

assert fake_call.call_args[0][0] == [
'docker', '--host', 'tcp://mydocker.net:2333', 'ps'
]

def test_with_http_host(self):
with mock.patch('subprocess.call') as fake_call:
call_docker(['ps'], {'--host': 'http://mydocker.net:2333'})
call_docker(['ps'], {'--host': 'http://mydocker.net:2333'}, {})

assert fake_call.call_args[0][0] == [
'docker', '--host', 'tcp://mydocker.net:2333', 'ps',
]

def test_with_host_option_shorthand_equal(self):
with mock.patch('subprocess.call') as fake_call:
call_docker(['ps'], {'--host': '=tcp://mydocker.net:2333'})
call_docker(['ps'], {'--host': '=tcp://mydocker.net:2333'}, {})

assert fake_call.call_args[0][0] == [
'docker', '--host', 'tcp://mydocker.net:2333', 'ps'
]

def test_with_env(self):
with mock.patch('subprocess.call') as fake_call:
call_docker(['ps'], {}, {'DOCKER_HOST': 'tcp://mydocker.net:2333'})

assert fake_call.call_args[0][0] == [
'docker', 'ps'
]
assert fake_call.call_args[1]['env'] == {'DOCKER_HOST': 'tcp://mydocker.net:2333'}

0 comments on commit 9d2508c

Please sign in to comment.