Add properties to specify arguments to Docker Compose commands

These new properties take a List<String>:

- spring.docker.compose.start.arguments
- spring.docker.compose.stop.arguments

Closes gh-38763
This commit is contained in:
Moritz Halbritter
2024-06-18 12:27:18 +02:00
parent 17ca0421e7
commit 6c0c9b9a80
13 changed files with 244 additions and 112 deletions

View File

@@ -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.
@@ -48,22 +48,42 @@ class DefaultDockerCompose implements DockerCompose {
@Override
public void up(LogLevel logLevel) {
this.cli.run(new DockerCliCommand.ComposeUp(logLevel));
up(logLevel, Collections.emptyList());
}
@Override
public void up(LogLevel logLevel, List<String> arguments) {
this.cli.run(new DockerCliCommand.ComposeUp(logLevel, arguments));
}
@Override
public void down(Duration timeout) {
this.cli.run(new DockerCliCommand.ComposeDown(timeout));
down(timeout, Collections.emptyList());
}
@Override
public void down(Duration timeout, List<String> arguments) {
this.cli.run(new DockerCliCommand.ComposeDown(timeout, arguments));
}
@Override
public void start(LogLevel logLevel) {
this.cli.run(new DockerCliCommand.ComposeStart(logLevel));
start(logLevel, Collections.emptyList());
}
@Override
public void start(LogLevel logLevel, List<String> arguments) {
this.cli.run(new DockerCliCommand.ComposeStart(logLevel, arguments));
}
@Override
public void stop(Duration timeout) {
this.cli.run(new DockerCliCommand.ComposeStop(timeout));
stop(timeout, Collections.emptyList());
}
@Override
public void stop(Duration timeout, List<String> arguments) {
this.cli.run(new DockerCliCommand.ComposeStop(timeout, arguments));
}
@Override

View File

@@ -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.
@@ -161,8 +161,18 @@ abstract sealed class DockerCliCommand<R> {
*/
static final class ComposeUp extends DockerCliCommand<Void> {
ComposeUp(LogLevel logLevel) {
super(Type.DOCKER_COMPOSE, logLevel, Void.class, false, "up", "--no-color", "--detach", "--wait");
ComposeUp(LogLevel logLevel, List<String> arguments) {
super(Type.DOCKER_COMPOSE, logLevel, Void.class, false, getCommand(arguments));
}
private static String[] getCommand(List<String> arguments) {
List<String> result = new ArrayList<>();
result.add("up");
result.add("--no-color");
result.add("--detach");
result.add("--wait");
result.addAll(arguments);
return result.toArray(String[]::new);
}
}
@@ -172,8 +182,17 @@ abstract sealed class DockerCliCommand<R> {
*/
static final class ComposeDown extends DockerCliCommand<Void> {
ComposeDown(Duration timeout) {
super(Type.DOCKER_COMPOSE, Void.class, false, "down", "--timeout", Long.toString(timeout.toSeconds()));
ComposeDown(Duration timeout, List<String> arguments) {
super(Type.DOCKER_COMPOSE, Void.class, false, getCommand(timeout, arguments));
}
private static String[] getCommand(Duration timeout, List<String> arguments) {
List<String> command = new ArrayList<>();
command.add("down");
command.add("--timeout");
command.add(Long.toString(timeout.toSeconds()));
command.addAll(arguments);
return command.toArray(String[]::new);
}
}
@@ -183,8 +202,15 @@ abstract sealed class DockerCliCommand<R> {
*/
static final class ComposeStart extends DockerCliCommand<Void> {
ComposeStart(LogLevel logLevel) {
super(Type.DOCKER_COMPOSE, logLevel, Void.class, false, "start");
ComposeStart(LogLevel logLevel, List<String> arguments) {
super(Type.DOCKER_COMPOSE, logLevel, Void.class, false, getCommand(arguments));
}
private static String[] getCommand(List<String> arguments) {
List<String> command = new ArrayList<>();
command.add("start");
command.addAll(arguments);
return command.toArray(String[]::new);
}
}
@@ -194,8 +220,17 @@ abstract sealed class DockerCliCommand<R> {
*/
static final class ComposeStop extends DockerCliCommand<Void> {
ComposeStop(Duration timeout) {
super(Type.DOCKER_COMPOSE, Void.class, false, "stop", "--timeout", Long.toString(timeout.toSeconds()));
ComposeStop(Duration timeout, List<String> arguments) {
super(Type.DOCKER_COMPOSE, Void.class, false, getCommand(timeout, arguments));
}
private static String[] getCommand(Duration timeout, List<String> arguments) {
List<String> command = new ArrayList<>();
command.add("stop");
command.add("--timeout");
command.add(Long.toString(timeout.toSeconds()));
command.addAll(arguments);
return command.toArray(String[]::new);
}
}

View File

@@ -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.
@@ -44,6 +44,15 @@ public interface DockerCompose {
*/
void up(LogLevel logLevel);
/**
* Run {@code docker compose up} to create and start services. Waits until all
* contains are started and healthy.
* @param logLevel the log level used to report progress
* @param arguments the arguments to pass to the up command
* @since 3.4.0
*/
void up(LogLevel logLevel, List<String> arguments);
/**
* Run {@code docker compose down} to stop and remove any running services.
* @param timeout the amount of time to wait or {@link #FORCE_STOP} to stop without
@@ -51,6 +60,15 @@ public interface DockerCompose {
*/
void down(Duration timeout);
/**
* Run {@code docker compose down} to stop and remove any running services.
* @param timeout the amount of time to wait or {@link #FORCE_STOP} to stop without
* waiting.
* @param arguments the arguments to pass to the down command
* @since 3.4.0
*/
void down(Duration timeout, List<String> arguments);
/**
* Run {@code docker compose start} to start services. Waits until all containers are
* started and healthy.
@@ -58,6 +76,15 @@ public interface DockerCompose {
*/
void start(LogLevel logLevel);
/**
* Run {@code docker compose start} to start services. Waits until all containers are
* started and healthy.
* @param logLevel the log level used to report progress
* @param arguments the arguments to pass to the start command
* @since 3.4.0
*/
void start(LogLevel logLevel, List<String> arguments);
/**
* Run {@code docker compose stop} to stop any running services.
* @param timeout the amount of time to wait or {@link #FORCE_STOP} to stop without
@@ -65,6 +92,15 @@ public interface DockerCompose {
*/
void stop(Duration timeout);
/**
* Run {@code docker compose stop} to stop any running services.
* @param timeout the amount of time to wait or {@link #FORCE_STOP} to stop without
* waiting.
* @param arguments the arguments to pass to the stop command
* @since 3.4.0
*/
void stop(Duration timeout, List<String> arguments);
/**
* Return if services have been defined in the {@link DockerComposeFile} for the
* active profiles.

View File

@@ -125,13 +125,14 @@ class DockerComposeLifecycleManager {
logger.info(skip.getLogMessage());
}
else {
start.getCommand().applyTo(dockerCompose, start.getLogLevel());
start.getCommand().applyTo(dockerCompose, start.getLogLevel(), start.getArguments());
runningServices = dockerCompose.getRunningServices();
if (wait == Wait.ONLY_IF_STARTED) {
wait = Wait.ALWAYS;
}
if (lifecycleManagement.shouldStop()) {
this.shutdownHandlers.add(() -> stop.getCommand().applyTo(dockerCompose, stop.getTimeout()));
this.shutdownHandlers
.add(() -> stop.getCommand().applyTo(dockerCompose, stop.getTimeout(), stop.getArguments()));
}
}
}

View File

@@ -18,6 +18,7 @@ package org.springframework.boot.docker.compose.lifecycle;
import java.io.File;
import java.time.Duration;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
@@ -155,6 +156,11 @@ public class DockerComposeProperties {
*/
private Skip skip = Skip.IF_RUNNING;
/**
* Arguments to pass to the start command.
*/
private final List<String> arguments = new ArrayList<>();
public StartCommand getCommand() {
return this.command;
}
@@ -179,6 +185,10 @@ public class DockerComposeProperties {
this.skip = skip;
}
public List<String> getArguments() {
return this.arguments;
}
/**
* Start command skip mode.
*/
@@ -233,6 +243,11 @@ public class DockerComposeProperties {
*/
private Duration timeout = Duration.ofSeconds(10);
/**
* Arguments to pass to the stop command.
*/
private final List<String> arguments = new ArrayList<>();
public StopCommand getCommand() {
return this.command;
}
@@ -249,6 +264,10 @@ public class DockerComposeProperties {
this.timeout = timeout;
}
public List<String> getArguments() {
return this.arguments;
}
}
/**

View File

@@ -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.
@@ -16,7 +16,7 @@
package org.springframework.boot.docker.compose.lifecycle;
import java.util.function.BiConsumer;
import java.util.List;
import org.springframework.boot.docker.compose.core.DockerCompose;
import org.springframework.boot.logging.LogLevel;
@@ -34,21 +34,23 @@ public enum StartCommand {
/**
* Start using {@code docker compose up}.
*/
UP(DockerCompose::up),
UP {
@Override
void applyTo(DockerCompose dockerCompose, LogLevel logLevel, List<String> arguments) {
dockerCompose.up(logLevel, arguments);
}
},
/**
* Start using {@code docker compose start}.
*/
START(DockerCompose::start);
START {
@Override
void applyTo(DockerCompose dockerCompose, LogLevel logLevel, List<String> arguments) {
dockerCompose.start(logLevel, arguments);
}
};
private final BiConsumer<DockerCompose, LogLevel> action;
StartCommand(BiConsumer<DockerCompose, LogLevel> action) {
this.action = action;
}
void applyTo(DockerCompose dockerCompose, LogLevel logLevel) {
this.action.accept(dockerCompose, logLevel);
}
abstract void applyTo(DockerCompose dockerCompose, LogLevel logLevel, List<String> arguments);
}

View File

@@ -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.
@@ -17,7 +17,7 @@
package org.springframework.boot.docker.compose.lifecycle;
import java.time.Duration;
import java.util.function.BiConsumer;
import java.util.List;
import org.springframework.boot.docker.compose.core.DockerCompose;
@@ -34,21 +34,23 @@ public enum StopCommand {
/**
* Stop using {@code docker compose down}.
*/
DOWN(DockerCompose::down),
DOWN {
@Override
void applyTo(DockerCompose dockerCompose, Duration timeout, List<String> arguments) {
dockerCompose.down(timeout, arguments);
}
},
/**
* Stop using {@code docker compose stop}.
*/
STOP(DockerCompose::stop);
STOP {
@Override
void applyTo(DockerCompose dockerCompose, Duration timeout, List<String> arguments) {
dockerCompose.stop(timeout, arguments);
}
};
private final BiConsumer<DockerCompose, Duration> action;
StopCommand(BiConsumer<DockerCompose, Duration> action) {
this.action = action;
}
void applyTo(DockerCompose dockerCompose, Duration timeout) {
this.action.accept(dockerCompose, timeout);
}
abstract void applyTo(DockerCompose dockerCompose, Duration timeout, List<String> arguments);
}