From 6559af7660fd157a21d0abf4d0e1708201a5c5de Mon Sep 17 00:00:00 2001 From: Sebastian Pipping Date: Mon, 19 Nov 2018 15:01:32 +0100 Subject: [PATCH 1/2] Fix one-off commands for "restart: unless-stopped" (fixes #6302) Signed-off-by: Sebastian Pipping --- compose/cli/main.py | 4 ++-- tests/unit/cli_test.py | 5 ++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/compose/cli/main.py b/compose/cli/main.py index e9c7dbb43..0dc39d660 100644 --- a/compose/cli/main.py +++ b/compose/cli/main.py @@ -1288,8 +1288,8 @@ def build_container_options(options, detach, command): [""] if options['--entrypoint'] == '' else options['--entrypoint'] ) - if options['--rm']: - container_options['restart'] = None + # Ensure that run command remains one-off (issue #6302) + container_options['restart'] = None if options['--user']: container_options['user'] = options.get('--user') diff --git a/tests/unit/cli_test.py b/tests/unit/cli_test.py index 7c8a1423c..a7522f939 100644 --- a/tests/unit/cli_test.py +++ b/tests/unit/cli_test.py @@ -171,7 +171,10 @@ class CLITestCase(unittest.TestCase): '--workdir': None, }) - assert mock_client.create_host_config.call_args[1]['restart_policy']['Name'] == 'always' + # NOTE: The "run" command is supposed to be a one-off tool; therefore restart policy "no" + # (the default) is enforced despite explicit wish for "always" in the project + # configuration file + assert not mock_client.create_host_config.call_args[1].get('restart_policy') command = TopLevelCommand(project) command.run({ From e7f82d298919639980f72d8a85acd20da042940d Mon Sep 17 00:00:00 2001 From: Sebastian Pipping Date: Mon, 26 Nov 2018 23:21:26 +0100 Subject: [PATCH 2/2] Rename build_container_options to build_one_off_container_options .. to better reflect that its scope is limited to one-off execution (i.e. the "run" command) Signed-off-by: Sebastian Pipping --- compose/cli/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compose/cli/main.py b/compose/cli/main.py index 0dc39d660..950e5055d 100644 --- a/compose/cli/main.py +++ b/compose/cli/main.py @@ -872,7 +872,7 @@ class TopLevelCommand(object): else: command = service.options.get('command') - container_options = build_container_options(options, detach, command) + container_options = build_one_off_container_options(options, detach, command) run_one_off_container( container_options, self.project, service, options, self.toplevel_options, self.project_dir @@ -1267,7 +1267,7 @@ def build_action_from_opts(options): return BuildAction.none -def build_container_options(options, detach, command): +def build_one_off_container_options(options, detach, command): container_options = { 'command': command, 'tty': not (detach or options['-T'] or not sys.stdin.isatty()),