Răsfoiți Sursa

Attach logger to containers after crashing.

Fixes #6060

Signed-off-by: Nicholas Higgins <[email protected]>
Nicholas Higgins 7 ani în urmă
părinte
comite
28085ebee2

+ 10 - 0
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),

+ 16 - 0
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'])

+ 7 - 0
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"