Add property to control Docker Compose start command execution
If the property 'spring.docker.compose.start.skip' is set to 'never', the start command is always executed. The default value of 'if-running' only executes the start command if there are no services running already, which is the old behavior. Closes gh-39749
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2023 the original author or authors.
|
||||
* Copyright 2012-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -32,6 +32,7 @@ import org.springframework.boot.docker.compose.core.DockerComposeFile;
|
||||
import org.springframework.boot.docker.compose.core.RunningService;
|
||||
import org.springframework.boot.docker.compose.lifecycle.DockerComposeProperties.Readiness.Wait;
|
||||
import org.springframework.boot.docker.compose.lifecycle.DockerComposeProperties.Start;
|
||||
import org.springframework.boot.docker.compose.lifecycle.DockerComposeProperties.Start.Skip;
|
||||
import org.springframework.boot.docker.compose.lifecycle.DockerComposeProperties.Stop;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
@@ -119,7 +120,11 @@ class DockerComposeLifecycleManager {
|
||||
Wait wait = this.properties.getReadiness().getWait();
|
||||
List<RunningService> runningServices = dockerCompose.getRunningServices();
|
||||
if (lifecycleManagement.shouldStart()) {
|
||||
if (runningServices.isEmpty()) {
|
||||
Skip skip = this.properties.getStart().getSkip();
|
||||
if (skip.shouldSkip(runningServices)) {
|
||||
logger.info(skip.getLogMessage());
|
||||
}
|
||||
else {
|
||||
start.getCommand().applyTo(dockerCompose, start.getLogLevel());
|
||||
runningServices = dockerCompose.getRunningServices();
|
||||
if (wait == Wait.ONLY_IF_STARTED) {
|
||||
@@ -129,9 +134,6 @@ class DockerComposeLifecycleManager {
|
||||
this.shutdownHandlers.add(() -> stop.getCommand().applyTo(dockerCompose, stop.getTimeout()));
|
||||
}
|
||||
}
|
||||
else {
|
||||
logger.info("There are already Docker Compose services running, skipping startup");
|
||||
}
|
||||
}
|
||||
List<RunningService> relevantServices = new ArrayList<>(runningServices);
|
||||
relevantServices.removeIf(this::isIgnored);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2023 the original author or authors.
|
||||
* Copyright 2012-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -19,10 +19,12 @@ package org.springframework.boot.docker.compose.lifecycle;
|
||||
import java.io.File;
|
||||
import java.time.Duration;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.bind.Binder;
|
||||
import org.springframework.boot.docker.compose.core.RunningService;
|
||||
import org.springframework.boot.logging.LogLevel;
|
||||
|
||||
/**
|
||||
@@ -148,6 +150,11 @@ public class DockerComposeProperties {
|
||||
*/
|
||||
private LogLevel logLevel = LogLevel.INFO;
|
||||
|
||||
/**
|
||||
* Whether to skip executing the start command.
|
||||
*/
|
||||
private Skip skip = Skip.IF_RUNNING;
|
||||
|
||||
public StartCommand getCommand() {
|
||||
return this.command;
|
||||
}
|
||||
@@ -164,6 +171,51 @@ public class DockerComposeProperties {
|
||||
this.logLevel = logLevel;
|
||||
}
|
||||
|
||||
public Skip getSkip() {
|
||||
return this.skip;
|
||||
}
|
||||
|
||||
public void setSkip(Skip skip) {
|
||||
this.skip = skip;
|
||||
}
|
||||
|
||||
/**
|
||||
* Start command skip mode.
|
||||
*/
|
||||
public enum Skip {
|
||||
|
||||
/**
|
||||
* Never skip start.
|
||||
*/
|
||||
NEVER {
|
||||
@Override
|
||||
boolean shouldSkip(List<RunningService> runningServices) {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Skip start if there are already services running.
|
||||
*/
|
||||
IF_RUNNING {
|
||||
@Override
|
||||
boolean shouldSkip(List<RunningService> runningServices) {
|
||||
return !runningServices.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
String getLogMessage() {
|
||||
return "There are already Docker Compose services running, skipping startup";
|
||||
}
|
||||
};
|
||||
|
||||
abstract boolean shouldSkip(List<RunningService> runningServices);
|
||||
|
||||
String getLogMessage() {
|
||||
return "";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -18,6 +18,10 @@
|
||||
"name": "spring.docker.compose.start.log-level",
|
||||
"defaultValue": "info"
|
||||
},
|
||||
{
|
||||
"name": "spring.docker.compose.start.skip",
|
||||
"defaultValue": "if-running"
|
||||
},
|
||||
{
|
||||
"name": "spring.docker.compose.stop.command",
|
||||
"defaultValue": "stop"
|
||||
|
||||
Reference in New Issue
Block a user