Skip to content

Commit 3f29c7f

Browse files
committed
Add log message if Docker Compose services are already running
Closes gh-38398
1 parent 24886ea commit 3f29c7f

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/lifecycle/DockerComposeLifecycleManager.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,19 @@ void start() {
118118
Stop stop = this.properties.getStop();
119119
Wait wait = this.properties.getReadiness().getWait();
120120
List<RunningService> runningServices = dockerCompose.getRunningServices();
121-
if (lifecycleManagement.shouldStart() && runningServices.isEmpty()) {
122-
start.getCommand().applyTo(dockerCompose, start.getLogLevel());
123-
runningServices = dockerCompose.getRunningServices();
124-
if (wait == Wait.ONLY_IF_STARTED) {
125-
wait = Wait.ALWAYS;
121+
if (lifecycleManagement.shouldStart()) {
122+
if (runningServices.isEmpty()) {
123+
start.getCommand().applyTo(dockerCompose, start.getLogLevel());
124+
runningServices = dockerCompose.getRunningServices();
125+
if (wait == Wait.ONLY_IF_STARTED) {
126+
wait = Wait.ALWAYS;
127+
}
128+
if (lifecycleManagement.shouldStop()) {
129+
this.shutdownHandlers.add(() -> stop.getCommand().applyTo(dockerCompose, stop.getTimeout()));
130+
}
126131
}
127-
if (lifecycleManagement.shouldStop()) {
128-
this.shutdownHandlers.add(() -> stop.getCommand().applyTo(dockerCompose, stop.getTimeout()));
132+
else {
133+
logger.info("There are already Docker Compose services running, skipping startup");
129134
}
130135
}
131136
List<RunningService> relevantServices = new ArrayList<>(runningServices);

spring-boot-project/spring-boot-docker-compose/src/test/java/org/springframework/boot/docker/compose/lifecycle/DockerComposeLifecycleManagerTests.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import org.junit.jupiter.api.BeforeEach;
3131
import org.junit.jupiter.api.Test;
32+
import org.junit.jupiter.api.extension.ExtendWith;
3233
import org.junit.jupiter.api.io.TempDir;
3334

3435
import org.springframework.aot.AotDetector;
@@ -38,6 +39,8 @@
3839
import org.springframework.boot.docker.compose.core.DockerComposeFile;
3940
import org.springframework.boot.docker.compose.core.RunningService;
4041
import org.springframework.boot.docker.compose.lifecycle.DockerComposeProperties.Readiness.Wait;
42+
import org.springframework.boot.test.system.CapturedOutput;
43+
import org.springframework.boot.test.system.OutputCaptureExtension;
4144
import org.springframework.context.ApplicationContext;
4245
import org.springframework.context.ApplicationListener;
4346
import org.springframework.context.support.GenericApplicationContext;
@@ -59,6 +62,7 @@
5962
* @author Phillip Webb
6063
* @author Scott Frederick
6164
*/
65+
@ExtendWith(OutputCaptureExtension.class)
6266
class DockerComposeLifecycleManagerTests {
6367

6468
@TempDir
@@ -365,6 +369,21 @@ void startPublishesEvent() {
365369
assertThat(event.getRunningServices()).isEqualTo(this.runningServices);
366370
}
367371

372+
@Test
373+
void shouldLogIfServicesAreAlreadyRunning(CapturedOutput output) {
374+
setUpRunningServices();
375+
this.lifecycleManager.start();
376+
assertThat(output).contains("There are already Docker Compose services running, skipping startup");
377+
}
378+
379+
@Test
380+
void shouldNotLogIfThereAreNoServicesRunning(CapturedOutput output) {
381+
given(this.dockerCompose.hasDefinedServices()).willReturn(true);
382+
given(this.dockerCompose.getRunningServices()).willReturn(Collections.emptyList());
383+
this.lifecycleManager.start();
384+
assertThat(output).doesNotContain("There are already Docker Compose services running, skipping startup");
385+
}
386+
368387
private void setUpRunningServices() {
369388
setUpRunningServices(true);
370389
}

0 commit comments

Comments
 (0)