diff --git a/compose/cli/log_printer.py b/compose/cli/log_printer.py index 60bba8da6..bd6723ef2 100644 --- a/compose/cli/log_printer.py +++ b/compose/cli/log_printer.py @@ -210,10 +210,15 @@ def start_producer_thread(thread_args): def watch_events(thread_map, event_stream, presenters, thread_args): + crashed_containers = set() for event in event_stream: if event['action'] == 'stop': thread_map.pop(event['id'], None) + if event['action'] == 'die': + thread_map.pop(event['id'], None) + crashed_containers.add(event['id']) + if event['action'] != 'start': continue @@ -223,6 +228,11 @@ def watch_events(thread_map, event_stream, presenters, thread_args): # Container was stopped and started, we need a new thread thread_map.pop(event['id'], None) + # Container crashed so we should reattach to it + if event['id'] in crashed_containers: + event['container'].attach_log_stream() + crashed_containers.remove(event['id']) + thread_map[event['id']] = build_thread( event['container'], next(presenters), diff --git a/tests/acceptance/cli_test.py b/tests/acceptance/cli_test.py index 43e8fa822..6da01ab1b 100644 --- a/tests/acceptance/cli_test.py +++ b/tests/acceptance/cli_test.py @@ -2253,6 +2253,22 @@ class CLITestCase(DockerClientTestCase): assert 'logs-composefile_another_1 exited with code 0' in result.stdout assert 'logs-composefile_simple_1 exited with code 137' in result.stdout + def test_logs_follow_logs_from_restarted_containers(self): + self.base_dir = 'tests/fixtures/logs-restart-composefile' + proc = start_process(self.base_dir, ['up']) + + wait_on_condition(ContainerStateCondition( + self.project.client, + 'logs-restart-composefile_another_1', + 'exited')) + + self.dispatch(['kill', 'simple']) + + result = wait_on_process(proc) + + assert result.stdout.count('logs-restart-composefile_another_1 exited with code 1') == 3 + assert result.stdout.count('world') == 3 + def test_logs_default(self): self.base_dir = 'tests/fixtures/logs-composefile' self.dispatch(['up', '-d']) diff --git a/tests/fixtures/logs-restart-composefile/docker-compose.yml b/tests/fixtures/logs-restart-composefile/docker-compose.yml new file mode 100644 index 000000000..c662a1e71 --- /dev/null +++ b/tests/fixtures/logs-restart-composefile/docker-compose.yml @@ -0,0 +1,7 @@ +simple: + image: busybox:latest + command: sh -c "echo hello && tail -f /dev/null" +another: + image: busybox:latest + command: sh -c "sleep 0.5 && echo world && /bin/false" + restart: "on-failure:2"