Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions compose/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,8 @@ def run(self, options):
else:
command = service.options.get('command')

options['stdin_open'] = service.options.get('stdin_open', True)

container_options = build_one_off_container_options(options, detach, command)
run_one_off_container(
container_options, self.project, service, options,
Expand Down Expand Up @@ -1286,7 +1288,7 @@ def build_one_off_container_options(options, detach, command):
container_options = {
'command': command,
'tty': not (detach or options['-T'] or not sys.stdin.isatty()),
'stdin_open': not detach,
'stdin_open': options.get('stdin_open'),
'detach': detach,
}

Expand Down Expand Up @@ -1368,7 +1370,7 @@ def remove_container(force=False):
if IS_WINDOWS_PLATFORM or use_cli:
service.connect_container_to_networks(container, use_network_aliases)
exit_code = call_docker(
["start", "--attach", "--interactive", container.id],
get_docker_start_call(container_options, container.id),
toplevel_options, environment
)
else:
Expand All @@ -1395,6 +1397,16 @@ def remove_container(force=False):
sys.exit(exit_code)


def get_docker_start_call(container_options, container_id):
docker_call = ["start"]
if not container_options.get('detach'):
docker_call.append("--attach")
if container_options.get('stdin_open'):
docker_call.append("--interactive")
docker_call.append(container_id)
return docker_call


def log_printer_from_project(
project,
containers,
Expand Down
60 changes: 60 additions & 0 deletions tests/unit/cli/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
from compose import container
from compose.cli.errors import UserError
from compose.cli.formatter import ConsoleWarningFormatter
from compose.cli.main import build_one_off_container_options
from compose.cli.main import call_docker
from compose.cli.main import convergence_strategy_from_opts
from compose.cli.main import filter_containers_to_service_names
from compose.cli.main import get_docker_start_call
from compose.cli.main import setup_console_handler
from compose.cli.main import warn_for_swarm_mode
from compose.service import ConvergenceStrategy
Expand Down Expand Up @@ -65,6 +67,64 @@ def test_warning_in_swarm_mode(self):
warn_for_swarm_mode(mock_client)
assert fake_log.warning.call_count == 1

def test_build_one_off_container_options(self):
command = 'build myservice'
detach = False
options = {
'-e': ['MYVAR=MYVALUE'],
'-T': True,
'--label': ['MYLABEL'],
'--entrypoint': 'bash',
'--user': 'MYUSER',
'--service-ports': [],
'--publish': '',
'--name': 'MYNAME',
'--workdir': '.',
'--volume': [],
'stdin_open': False,
}

expected_container_options = {
'command': command,
'tty': False,
'stdin_open': False,
'detach': detach,
'entrypoint': 'bash',
'environment': {'MYVAR': 'MYVALUE'},
'labels': {'MYLABEL': ''},
'name': 'MYNAME',
'ports': [],
'restart': None,
'user': 'MYUSER',
'working_dir': '.',
}

container_options = build_one_off_container_options(options, detach, command)
assert container_options == expected_container_options

def test_get_docker_start_call(self):
container_id = 'my_container_id'

mock_container_options = {'detach': False, 'stdin_open': True}
expected_docker_start_call = ['start', '--attach', '--interactive', container_id]
docker_start_call = get_docker_start_call(mock_container_options, container_id)
assert expected_docker_start_call == docker_start_call

mock_container_options = {'detach': False, 'stdin_open': False}
expected_docker_start_call = ['start', '--attach', container_id]
docker_start_call = get_docker_start_call(mock_container_options, container_id)
assert expected_docker_start_call == docker_start_call

mock_container_options = {'detach': True, 'stdin_open': True}
expected_docker_start_call = ['start', '--interactive', container_id]
docker_start_call = get_docker_start_call(mock_container_options, container_id)
assert expected_docker_start_call == docker_start_call

mock_container_options = {'detach': True, 'stdin_open': False}
expected_docker_start_call = ['start', container_id]
docker_start_call = get_docker_start_call(mock_container_options, container_id)
assert expected_docker_start_call == docker_start_call


class TestSetupConsoleHandlerTestCase(object):

Expand Down