Skip to content

Commit 9d2508c

Browse files
author
Ian Campbell
committed
Pass environment when calling through to docker cli.
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]>
1 parent 4218b46 commit 9d2508c

2 files changed

Lines changed: 19 additions & 11 deletions

File tree

compose/cli/main.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ def exec_command(self, options):
516516
if IS_WINDOWS_PLATFORM or use_cli and not detach:
517517
sys.exit(call_docker(
518518
build_exec_command(options, container.id, command),
519-
self.toplevel_options)
519+
self.toplevel_options, environment)
520520
)
521521

522522
create_exec_options = {
@@ -1361,7 +1361,6 @@ def remove_container(force=False):
13611361
environment_file = options.get('--env-file')
13621362
environment = Environment.from_env_file(project_dir, environment_file)
13631363
use_cli = not environment.get_boolean('COMPOSE_INTERACTIVE_NO_CLI')
1364-
13651364
signals.set_signal_handler_to_shutdown()
13661365
signals.set_signal_handler_to_hang_up()
13671366
try:
@@ -1370,7 +1369,7 @@ def remove_container(force=False):
13701369
service.connect_container_to_networks(container, use_network_aliases)
13711370
exit_code = call_docker(
13721371
["start", "--attach", "--interactive", container.id],
1373-
toplevel_options
1372+
toplevel_options, environment
13741373
)
13751374
else:
13761375
operation = RunOperation(
@@ -1450,7 +1449,7 @@ def exit_if(condition, message, exit_code):
14501449
raise SystemExit(exit_code)
14511450

14521451

1453-
def call_docker(args, dockeropts):
1452+
def call_docker(args, dockeropts, environment):
14541453
executable_path = find_executable('docker')
14551454
if not executable_path:
14561455
raise UserError(errors.docker_not_found_msg("Couldn't find `docker` binary."))
@@ -1480,7 +1479,7 @@ def call_docker(args, dockeropts):
14801479
args = [executable_path] + tls_options + args
14811480
log.debug(" ".join(map(pipes.quote, args)))
14821481

1483-
return subprocess.call(args)
1482+
return subprocess.call(args, env=environment)
14841483

14851484

14861485
def parse_scale_args(options):

tests/unit/cli/main_test.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,13 @@ def mock_find_executable(exe):
123123
class TestCallDocker(object):
124124
def test_simple_no_options(self):
125125
with mock.patch('subprocess.call') as fake_call:
126-
call_docker(['ps'], {})
126+
call_docker(['ps'], {}, {})
127127

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

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

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

@@ -140,7 +140,7 @@ def test_advanced_tls_options(self):
140140
'--tlscacert': './ca.pem',
141141
'--tlscert': './cert.pem',
142142
'--tlskey': './key.pem',
143-
})
143+
}, {})
144144

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

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

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

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

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

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

170170
assert fake_call.call_args[0][0] == [
171171
'docker', '--host', 'tcp://mydocker.net:2333', 'ps'
172172
]
173+
174+
def test_with_env(self):
175+
with mock.patch('subprocess.call') as fake_call:
176+
call_docker(['ps'], {}, {'DOCKER_HOST': 'tcp://mydocker.net:2333'})
177+
178+
assert fake_call.call_args[0][0] == [
179+
'docker', 'ps'
180+
]
181+
assert fake_call.call_args[1]['env'] == {'DOCKER_HOST': 'tcp://mydocker.net:2333'}

0 commit comments

Comments
 (0)