Skip to content

Commit 6c0c9b9

Browse files
committed
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
1 parent 17ca042 commit 6c0c9b9

File tree

13 files changed

+246
-114
lines changed

13 files changed

+246
-114
lines changed

spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/core/DefaultDockerCompose.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -48,22 +48,42 @@ class DefaultDockerCompose implements DockerCompose {
4848

4949
@Override
5050
public void up(LogLevel logLevel) {
51-
this.cli.run(new DockerCliCommand.ComposeUp(logLevel));
51+
up(logLevel, Collections.emptyList());
52+
}
53+
54+
@Override
55+
public void up(LogLevel logLevel, List<String> arguments) {
56+
this.cli.run(new DockerCliCommand.ComposeUp(logLevel, arguments));
5257
}
5358

5459
@Override
5560
public void down(Duration timeout) {
56-
this.cli.run(new DockerCliCommand.ComposeDown(timeout));
61+
down(timeout, Collections.emptyList());
62+
}
63+
64+
@Override
65+
public void down(Duration timeout, List<String> arguments) {
66+
this.cli.run(new DockerCliCommand.ComposeDown(timeout, arguments));
5767
}
5868

5969
@Override
6070
public void start(LogLevel logLevel) {
61-
this.cli.run(new DockerCliCommand.ComposeStart(logLevel));
71+
start(logLevel, Collections.emptyList());
72+
}
73+
74+
@Override
75+
public void start(LogLevel logLevel, List<String> arguments) {
76+
this.cli.run(new DockerCliCommand.ComposeStart(logLevel, arguments));
6277
}
6378

6479
@Override
6580
public void stop(Duration timeout) {
66-
this.cli.run(new DockerCliCommand.ComposeStop(timeout));
81+
stop(timeout, Collections.emptyList());
82+
}
83+
84+
@Override
85+
public void stop(Duration timeout, List<String> arguments) {
86+
this.cli.run(new DockerCliCommand.ComposeStop(timeout, arguments));
6787
}
6888

6989
@Override

spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/core/DockerCliCommand.java

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -161,8 +161,18 @@ static final class ComposePs extends DockerCliCommand<List<DockerCliComposePsRes
161161
*/
162162
static final class ComposeUp extends DockerCliCommand<Void> {
163163

164-
ComposeUp(LogLevel logLevel) {
165-
super(Type.DOCKER_COMPOSE, logLevel, Void.class, false, "up", "--no-color", "--detach", "--wait");
164+
ComposeUp(LogLevel logLevel, List<String> arguments) {
165+
super(Type.DOCKER_COMPOSE, logLevel, Void.class, false, getCommand(arguments));
166+
}
167+
168+
private static String[] getCommand(List<String> arguments) {
169+
List<String> result = new ArrayList<>();
170+
result.add("up");
171+
result.add("--no-color");
172+
result.add("--detach");
173+
result.add("--wait");
174+
result.addAll(arguments);
175+
return result.toArray(String[]::new);
166176
}
167177

168178
}
@@ -172,8 +182,17 @@ static final class ComposeUp extends DockerCliCommand<Void> {
172182
*/
173183
static final class ComposeDown extends DockerCliCommand<Void> {
174184

175-
ComposeDown(Duration timeout) {
176-
super(Type.DOCKER_COMPOSE, Void.class, false, "down", "--timeout", Long.toString(timeout.toSeconds()));
185+
ComposeDown(Duration timeout, List<String> arguments) {
186+
super(Type.DOCKER_COMPOSE, Void.class, false, getCommand(timeout, arguments));
187+
}
188+
189+
private static String[] getCommand(Duration timeout, List<String> arguments) {
190+
List<String> command = new ArrayList<>();
191+
command.add("down");
192+
command.add("--timeout");
193+
command.add(Long.toString(timeout.toSeconds()));
194+
command.addAll(arguments);
195+
return command.toArray(String[]::new);
177196
}
178197

179198
}
@@ -183,8 +202,15 @@ static final class ComposeDown extends DockerCliCommand<Void> {
183202
*/
184203
static final class ComposeStart extends DockerCliCommand<Void> {
185204

186-
ComposeStart(LogLevel logLevel) {
187-
super(Type.DOCKER_COMPOSE, logLevel, Void.class, false, "start");
205+
ComposeStart(LogLevel logLevel, List<String> arguments) {
206+
super(Type.DOCKER_COMPOSE, logLevel, Void.class, false, getCommand(arguments));
207+
}
208+
209+
private static String[] getCommand(List<String> arguments) {
210+
List<String> command = new ArrayList<>();
211+
command.add("start");
212+
command.addAll(arguments);
213+
return command.toArray(String[]::new);
188214
}
189215

190216
}
@@ -194,8 +220,17 @@ static final class ComposeStart extends DockerCliCommand<Void> {
194220
*/
195221
static final class ComposeStop extends DockerCliCommand<Void> {
196222

197-
ComposeStop(Duration timeout) {
198-
super(Type.DOCKER_COMPOSE, Void.class, false, "stop", "--timeout", Long.toString(timeout.toSeconds()));
223+
ComposeStop(Duration timeout, List<String> arguments) {
224+
super(Type.DOCKER_COMPOSE, Void.class, false, getCommand(timeout, arguments));
225+
}
226+
227+
private static String[] getCommand(Duration timeout, List<String> arguments) {
228+
List<String> command = new ArrayList<>();
229+
command.add("stop");
230+
command.add("--timeout");
231+
command.add(Long.toString(timeout.toSeconds()));
232+
command.addAll(arguments);
233+
return command.toArray(String[]::new);
199234
}
200235

201236
}

spring-boot-project/spring-boot-docker-compose/src/main/java/org/springframework/boot/docker/compose/core/DockerCompose.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -44,27 +44,63 @@ public interface DockerCompose {
4444
*/
4545
void up(LogLevel logLevel);
4646

47+
/**
48+
* Run {@code docker compose up} to create and start services. Waits until all
49+
* contains are started and healthy.
50+
* @param logLevel the log level used to report progress
51+
* @param arguments the arguments to pass to the up command
52+
* @since 3.4.0
53+
*/
54+
void up(LogLevel logLevel, List<String> arguments);
55+
4756
/**
4857
* Run {@code docker compose down} to stop and remove any running services.
4958
* @param timeout the amount of time to wait or {@link #FORCE_STOP} to stop without
5059
* waiting.
5160
*/
5261
void down(Duration timeout);
5362

63+
/**
64+
* Run {@code docker compose down} to stop and remove any running services.
65+
* @param timeout the amount of time to wait or {@link #FORCE_STOP} to stop without
66+
* waiting.
67+
* @param arguments the arguments to pass to the down command
68+
* @since 3.4.0
69+
*/
70+
void down(Duration timeout, List<String> arguments);
71+
5472
/**
5573
* Run {@code docker compose start} to start services. Waits until all containers are
5674
* started and healthy.
5775
* @param logLevel the log level used to report progress
5876
*/
5977
void start(LogLevel logLevel);
6078

79+
/**
80+
* Run {@code docker compose start} to start services. Waits until all containers are
81+
* started and healthy.
82+
* @param logLevel the log level used to report progress
83+
* @param arguments the arguments to pass to the start command
84+
* @since 3.4.0
85+
*/
86+
void start(LogLevel logLevel, List<String> arguments);
87+
6188
/**
6289
* Run {@code docker compose stop} to stop any running services.
6390
* @param timeout the amount of time to wait or {@link #FORCE_STOP} to stop without
6491
* waiting.
6592
*/
6693
void stop(Duration timeout);
6794

95+
/**
96+
* Run {@code docker compose stop} to stop any running services.
97+
* @param timeout the amount of time to wait or {@link #FORCE_STOP} to stop without
98+
* waiting.
99+
* @param arguments the arguments to pass to the stop command
100+
* @since 3.4.0
101+
*/
102+
void stop(Duration timeout, List<String> arguments);
103+
68104
/**
69105
* Return if services have been defined in the {@link DockerComposeFile} for the
70106
* active profiles.

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,14 @@ void start() {
125125
logger.info(skip.getLogMessage());
126126
}
127127
else {
128-
start.getCommand().applyTo(dockerCompose, start.getLogLevel());
128+
start.getCommand().applyTo(dockerCompose, start.getLogLevel(), start.getArguments());
129129
runningServices = dockerCompose.getRunningServices();
130130
if (wait == Wait.ONLY_IF_STARTED) {
131131
wait = Wait.ALWAYS;
132132
}
133133
if (lifecycleManagement.shouldStop()) {
134-
this.shutdownHandlers.add(() -> stop.getCommand().applyTo(dockerCompose, stop.getTimeout()));
134+
this.shutdownHandlers
135+
.add(() -> stop.getCommand().applyTo(dockerCompose, stop.getTimeout(), stop.getArguments()));
135136
}
136137
}
137138
}

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

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

1919
import java.io.File;
2020
import java.time.Duration;
21+
import java.util.ArrayList;
2122
import java.util.LinkedHashSet;
2223
import java.util.List;
2324
import java.util.Set;
@@ -155,6 +156,11 @@ public static class Start {
155156
*/
156157
private Skip skip = Skip.IF_RUNNING;
157158

159+
/**
160+
* Arguments to pass to the start command.
161+
*/
162+
private final List<String> arguments = new ArrayList<>();
163+
158164
public StartCommand getCommand() {
159165
return this.command;
160166
}
@@ -179,6 +185,10 @@ public void setSkip(Skip skip) {
179185
this.skip = skip;
180186
}
181187

188+
public List<String> getArguments() {
189+
return this.arguments;
190+
}
191+
182192
/**
183193
* Start command skip mode.
184194
*/
@@ -233,6 +243,11 @@ public static class Stop {
233243
*/
234244
private Duration timeout = Duration.ofSeconds(10);
235245

246+
/**
247+
* Arguments to pass to the stop command.
248+
*/
249+
private final List<String> arguments = new ArrayList<>();
250+
236251
public StopCommand getCommand() {
237252
return this.command;
238253
}
@@ -249,6 +264,10 @@ public void setTimeout(Duration timeout) {
249264
this.timeout = timeout;
250265
}
251266

267+
public List<String> getArguments() {
268+
return this.arguments;
269+
}
270+
252271
}
253272

254273
/**

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

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,7 @@
1616

1717
package org.springframework.boot.docker.compose.lifecycle;
1818

19-
import java.util.function.BiConsumer;
19+
import java.util.List;
2020

2121
import org.springframework.boot.docker.compose.core.DockerCompose;
2222
import org.springframework.boot.logging.LogLevel;
@@ -34,21 +34,23 @@ public enum StartCommand {
3434
/**
3535
* Start using {@code docker compose up}.
3636
*/
37-
UP(DockerCompose::up),
37+
UP {
38+
@Override
39+
void applyTo(DockerCompose dockerCompose, LogLevel logLevel, List<String> arguments) {
40+
dockerCompose.up(logLevel, arguments);
41+
}
42+
},
3843

3944
/**
4045
* Start using {@code docker compose start}.
4146
*/
42-
START(DockerCompose::start);
43-
44-
private final BiConsumer<DockerCompose, LogLevel> action;
45-
46-
StartCommand(BiConsumer<DockerCompose, LogLevel> action) {
47-
this.action = action;
48-
}
49-
50-
void applyTo(DockerCompose dockerCompose, LogLevel logLevel) {
51-
this.action.accept(dockerCompose, logLevel);
52-
}
47+
START {
48+
@Override
49+
void applyTo(DockerCompose dockerCompose, LogLevel logLevel, List<String> arguments) {
50+
dockerCompose.start(logLevel, arguments);
51+
}
52+
};
53+
54+
abstract void applyTo(DockerCompose dockerCompose, LogLevel logLevel, List<String> arguments);
5355

5456
}

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

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,7 +17,7 @@
1717
package org.springframework.boot.docker.compose.lifecycle;
1818

1919
import java.time.Duration;
20-
import java.util.function.BiConsumer;
20+
import java.util.List;
2121

2222
import org.springframework.boot.docker.compose.core.DockerCompose;
2323

@@ -34,21 +34,23 @@ public enum StopCommand {
3434
/**
3535
* Stop using {@code docker compose down}.
3636
*/
37-
DOWN(DockerCompose::down),
37+
DOWN {
38+
@Override
39+
void applyTo(DockerCompose dockerCompose, Duration timeout, List<String> arguments) {
40+
dockerCompose.down(timeout, arguments);
41+
}
42+
},
3843

3944
/**
4045
* Stop using {@code docker compose stop}.
4146
*/
42-
STOP(DockerCompose::stop);
43-
44-
private final BiConsumer<DockerCompose, Duration> action;
45-
46-
StopCommand(BiConsumer<DockerCompose, Duration> action) {
47-
this.action = action;
48-
}
49-
50-
void applyTo(DockerCompose dockerCompose, Duration timeout) {
51-
this.action.accept(dockerCompose, timeout);
52-
}
47+
STOP {
48+
@Override
49+
void applyTo(DockerCompose dockerCompose, Duration timeout, List<String> arguments) {
50+
dockerCompose.stop(timeout, arguments);
51+
}
52+
};
53+
54+
abstract void applyTo(DockerCompose dockerCompose, Duration timeout, List<String> arguments);
5355

5456
}

0 commit comments

Comments
 (0)