Skip to content

Commit 00a10fd

Browse files
committed
Move Docker Compose and Testcontainers to separate section
See gh-38361
1 parent dfee56a commit 00a10fd

File tree

15 files changed

+163
-151
lines changed

15 files changed

+163
-151
lines changed

spring-boot-project/spring-boot-docs/src/docs/antora/modules/ROOT/pages/redirect.adoc

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,15 +1038,15 @@
10381038
* xref:reference:features/developing-auto-configuration.adoc#features.developing-auto-configuration.testing.overriding-classpath[#features.developing-auto-configuration.testing.overriding-classpath]
10391039
* xref:reference:features/developing-auto-configuration.adoc#features.developing-auto-configuration.testing.simulating-a-web-context[#features.developing-auto-configuration.testing.simulating-a-web-context]
10401040
* xref:reference:features/developing-auto-configuration.adoc#features.developing-auto-configuration.understanding-auto-configured-beans[#features.developing-auto-configuration.understanding-auto-configured-beans]
1041-
* xref:reference:features/docker-compose.adoc#features.docker-compose[#features.docker-compose]
1042-
* xref:reference:features/docker-compose.adoc#features.docker-compose.custom-images[#features.docker-compose.custom-images]
1043-
* xref:reference:features/docker-compose.adoc#features.docker-compose.lifecycle[#features.docker-compose.lifecycle]
1044-
* xref:reference:features/docker-compose.adoc#features.docker-compose.prerequisites[#features.docker-compose.prerequisites]
1045-
* xref:reference:features/docker-compose.adoc#features.docker-compose.profiles[#features.docker-compose.profiles]
1046-
* xref:reference:features/docker-compose.adoc#features.docker-compose.readiness[#features.docker-compose.readiness]
1047-
* xref:reference:features/docker-compose.adoc#features.docker-compose.service-connections[#features.docker-compose.service-connections]
1048-
* xref:reference:features/docker-compose.adoc#features.docker-compose.skipping[#features.docker-compose.skipping]
1049-
* xref:reference:features/docker-compose.adoc#features.docker-compose.specific-file[#features.docker-compose.specific-file]
1041+
* xref:reference:features/dev-services.adoc#features.dev-services.docker-compose[#features.docker-compose]
1042+
* xref:reference:features/dev-services.adoc#features.dev-services.docker-compose.custom-images[#features.docker-compose.custom-images]
1043+
* xref:reference:features/dev-services.adoc#features.dev-services.docker-compose.lifecycle[#features.docker-compose.lifecycle]
1044+
* xref:reference:features/dev-services.adoc#features.dev-services.docker-compose.prerequisites[#features.docker-compose.prerequisites]
1045+
* xref:reference:features/dev-services.adoc#features.dev-services.docker-compose.profiles[#features.docker-compose.profiles]
1046+
* xref:reference:features/dev-services.adoc#features.dev-services.docker-compose.readiness[#features.docker-compose.readiness]
1047+
* xref:reference:features/dev-services.adoc#features.dev-services.docker-compose.service-connections[#features.docker-compose.service-connections]
1048+
* xref:reference:features/dev-services.adoc#features.dev-services.docker-compose.skipping[#features.docker-compose.skipping]
1049+
* xref:reference:features/dev-services.adoc#features.dev-services.docker-compose.specific-file[#features.docker-compose.specific-file]
10501050
* xref:reference:features/external-config.adoc#features.external-config[#features.external-config]
10511051
* xref:reference:features/external-config.adoc#features.external-config.application-json[#features.external-config.application-json]
10521052
* xref:reference:features/external-config.adoc#features.external-config.command-line-args[#features.external-config.command-line-args]
@@ -1154,11 +1154,11 @@
11541154
* xref:reference:features/ssl.adoc#features.ssl.pem[#features.ssl.pem]
11551155
* xref:reference:features/ssl.adoc#features.ssl.reloading[#features.ssl.reloading]
11561156
* xref:reference:features/task-execution-and-scheduling.adoc#features.task-execution-and-scheduling[#features.task-execution-and-scheduling]
1157-
* xref:reference:features/testcontainers.adoc#features.testcontainers[#features.testcontainers]
1158-
* xref:reference:features/testcontainers.adoc#features.testcontainers.at-development-time[#features.testcontainers.at-development-time]
1159-
* xref:reference:features/testcontainers.adoc#features.testcontainers.at-development-time.devtools[#features.testcontainers.at-development-time.devtools]
1160-
* xref:reference:features/testcontainers.adoc#features.testcontainers.at-development-time.dynamic-properties[#features.testcontainers.at-development-time.dynamic-properties]
1161-
* xref:reference:features/testcontainers.adoc#features.testcontainers.at-development-time.importing-container-declarations[#features.testcontainers.at-development-time.importing-container-declarations]
1157+
* xref:reference:features/dev-services.adoc#features.dev-services.testcontainers[#features.testcontainers]
1158+
* xref:reference:features/dev-services.adoc#features.dev-services.testcontainers.at-development-time[#features.testcontainers.at-development-time]
1159+
* xref:reference:features/dev-services.adoc#features.dev-services.testcontainers.at-development-time.devtools[#features.testcontainers.at-development-time.devtools]
1160+
* xref:reference:features/dev-services.adoc#features.dev-services.testcontainers.at-development-time.dynamic-properties[#features.testcontainers.at-development-time.dynamic-properties]
1161+
* xref:reference:features/dev-services.adoc#features.dev-services.testcontainers.at-development-time.importing-container-declarations[#features.testcontainers.at-development-time.importing-container-declarations]
11621162
* xref:reference:features/testing.adoc#features.testing[#features.testing]
11631163
* xref:reference:features/testing.adoc#features.testing.spring-applications[#features.testing.spring-applications]
11641164
* xref:reference:features/testing.adoc#features.testing.spring-boot-applications[#features.testing.spring-boot-applications]
Lines changed: 128 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1-
[[features.docker-compose]]
2-
= Docker Compose Support
1+
[[features.dev-services]]
2+
= Development-time services
3+
4+
Development-time services provide external dependencies needed to run the application while developing it.
5+
They are only supposed to be used while developing and are disabled when the application is deployed.
6+
7+
Spring Boot offers support for two development time services, Docker Compose and Testcontainers.
8+
The next sections will provide more details about them.
9+
10+
[[features.dev-services.docker-compose]]
11+
== Docker Compose Support
312

413
Docker Compose is a popular technology that can be used to define and manage multiple containers for services that your application needs.
514
A `compose.yml` file is typically created next to your application which defines and configures service containers.
@@ -44,16 +53,16 @@ To enable it, set configprop:spring.docker.compose.skip.in-tests[] to `false`.
4453

4554

4655

47-
[[features.docker-compose.prerequisites]]
48-
== Prerequisites
56+
[[features.dev-services.docker-compose.prerequisites]]
57+
=== Prerequisites
4958

5059
You need to have the `docker` and `docker compose` (or `docker-compose`) CLI applications on your path.
5160
The minimum supported Docker Compose version is 2.2.0.
5261

5362

5463

55-
[[features.docker-compose.service-connections]]
56-
== Service Connections
64+
[[features.dev-services.docker-compose.service-connections]]
65+
=== Service Connections
5766

5867
A service connection is a connection to any remote service.
5968
Spring Boot’s auto-configuration can consume the details of a service connection and use them to establish a connection to a remote service.
@@ -120,8 +129,8 @@ The following service connections are currently supported:
120129

121130

122131

123-
[[features.docker-compose.custom-images]]
124-
== Custom Images
132+
[[features.dev-services.docker-compose.custom-images]]
133+
=== Custom Images
125134

126135
Sometimes you may need to use your own version of an image to provide a service.
127136
You can use any custom image as long as it behaves in the same way as the standard image.
@@ -145,8 +154,8 @@ services:
145154

146155

147156

148-
[[features.docker-compose.skipping]]
149-
== Skipping Specific Containers
157+
[[features.dev-services.docker-compose.skipping]]
158+
=== Skipping Specific Containers
150159

151160
If you have a container image defined in your `compose.yml` that you don’t want connected to your application you can use a label to ignore it.
152161
Any container with labeled with `org.springframework.boot.ignore` will be ignored by Spring Boot.
@@ -166,8 +175,8 @@ services:
166175

167176

168177

169-
[[features.docker-compose.specific-file]]
170-
== Using a Specific Compose File
178+
[[features.dev-services.docker-compose.specific-file]]
179+
=== Using a Specific Compose File
171180

172181
If your compose file is not in the same directory as your application, or if it’s named differently, you can use configprop:spring.docker.compose.file[] in your `application.properties` or `application.yaml` to point to a different file.
173182
Properties can be defined as an exact path or a path that’s relative to your application.
@@ -184,8 +193,8 @@ spring:
184193

185194

186195

187-
[[features.docker-compose.readiness]]
188-
== Waiting for Container Readiness
196+
[[features.dev-services.docker-compose.readiness]]
197+
=== Waiting for Container Readiness
189198

190199
Containers started by Docker Compose may take some time to become fully ready.
191200
The recommended way of checking for readiness is to add a `healthcheck` section under the service definition in your `compose.yml` file.
@@ -225,8 +234,8 @@ The overall timeout can be configured using configprop:spring.docker.compose.rea
225234

226235

227236

228-
[[features.docker-compose.lifecycle]]
229-
== Controlling the Docker Compose Lifecycle
237+
[[features.dev-services.docker-compose.lifecycle]]
238+
=== Controlling the Docker Compose Lifecycle
230239

231240
By default Spring Boot calls `docker compose up` when your application starts and `docker compose stop` when it's shut down.
232241
If you prefer to have different lifecycle management you can use the configprop:spring.docker.compose.lifecycle-management[] property.
@@ -257,8 +266,8 @@ spring:
257266

258267

259268

260-
[[features.docker-compose.profiles]]
261-
== Activating Docker Compose Profiles
269+
[[features.dev-services.docker-compose.profiles]]
270+
=== Activating Docker Compose Profiles
262271

263272
Docker Compose profiles are similar to Spring profiles in that they let you adjust your Docker Compose configuration for specific environments.
264273
If you want to activate a specific Docker Compose profile you can use the configprop:spring.docker.compose.profiles.active[] property in your `application.properties` or `application.yaml` file:
@@ -271,3 +280,104 @@ spring:
271280
profiles:
272281
active: "myprofile"
273282
----
283+
284+
285+
286+
[[features.dev-services.testcontainers]]
287+
== Testcontainers Support
288+
289+
As well as xref:features/testing.adoc#features.testing.testcontainers[using Testcontainers for integration testing], it's also possible to use them at development time.
290+
The next sections will provide more details about that.
291+
292+
293+
294+
[[features.dev-services.testcontainers.at-development-time]]
295+
=== Using Testcontainers at Development Time
296+
297+
This approach allows developers to quickly start containers for the services that the application depends on, removing the need to manually provision things like database servers.
298+
Using Testcontainers in this way provides functionality similar to Docker Compose, except that your container configuration is in Java rather than YAML.
299+
300+
To use Testcontainers at development time you need to launch your application using your "`test`" classpath rather than "`main`".
301+
This will allow you to access all declared test dependencies and give you a natural place to write your test configuration.
302+
303+
To create a test launchable version of your application you should create an "`Application`" class in the `src/test` directory.
304+
For example, if your main application is in `src/main/java/com/example/MyApplication.java`, you should create `src/test/java/com/example/TestMyApplication.java`
305+
306+
The `TestMyApplication` class can use the `SpringApplication.from(...)` method to launch the real application:
307+
308+
include-code::launch/TestMyApplication[]
309+
310+
You'll also need to define the `Container` instances that you want to start along with your application.
311+
To do this, you need to make sure that the `spring-boot-testcontainers` module has been added as a `test` dependency.
312+
Once that has been done, you can create a `@TestConfiguration` class that declares `@Bean` methods for the containers you want to start.
313+
314+
You can also annotate your `@Bean` methods with `@ServiceConnection` in order to create `ConnectionDetails` beans.
315+
See xref:features/testing.adoc#features.testing.testcontainers.service-connections[the service connections] section for details of the supported technologies.
316+
317+
A typical Testcontainers configuration would look like this:
318+
319+
include-code::test/MyContainersConfiguration[]
320+
321+
NOTE: The lifecycle of `Container` beans is automatically managed by Spring Boot.
322+
Containers will be started and stopped automatically.
323+
324+
TIP: You can use the configprop:spring.testcontainers.beans.startup[] property to change how containers are started.
325+
By default `sequential` startup is used, but you may also choose `parallel` if you wish to start multiple containers in parallel.
326+
327+
Once you have defined your test configuration, you can use the `with(...)` method to attach it to your test launcher:
328+
329+
include-code::test/TestMyApplication[]
330+
331+
You can now launch `TestMyApplication` as you would any regular Java `main` method application to start your application and the containers that it needs to run.
332+
333+
TIP: You can use the Maven goal `spring-boot:test-run` or the Gradle task `bootTestRun` to do this from the command line.
334+
335+
336+
337+
[[features.dev-services.testcontainers.at-development-time.dynamic-properties]]
338+
==== Contributing Dynamic Properties at Development Time
339+
340+
If you want to contribute dynamic properties at development time from your `Container` `@Bean` methods, you can do so by injecting a `DynamicPropertyRegistry`.
341+
This works in a similar way to the xref:features/testing.adoc#features.testing.testcontainers.dynamic-properties[`@DynamicPropertySource` annotation] that you can use in your tests.
342+
It allows you to add properties that will become available once your container has started.
343+
344+
A typical configuration would look like this:
345+
346+
include-code::MyContainersConfiguration[]
347+
348+
NOTE: Using a `@ServiceConnection` is recommended whenever possible, however, dynamic properties can be a useful fallback for technologies that don't yet have `@ServiceConnection` support.
349+
350+
351+
352+
[[features.dev-services.testcontainers.at-development-time.importing-container-declarations]]
353+
==== Importing Testcontainer Declaration Classes
354+
355+
A common pattern when using Testcontainers is to declare `Container` instances as static fields.
356+
Often these fields are defined directly on the test class.
357+
They can also be declared on a parent class or on an interface that the test implements.
358+
359+
For example, the following `MyContainers` interface declares `mongo` and `neo4j` containers:
360+
361+
include-code::MyContainers[]
362+
363+
If you already have containers defined in this way, or you just prefer this style, you can import these declaration classes rather than defining you containers as `@Bean` methods.
364+
To do so, add the `@ImportTestcontainers` annotation to your test configuration class:
365+
366+
include-code::MyContainersConfiguration[]
367+
368+
TIP: If you don't intend to use the xref:features/testing.adoc#features.testing.testcontainers.service-connections[service connections feature] but want to use xref:features/testing.adoc#features.testing.testcontainers.dynamic-properties[`@DynamicPropertySource`] instead, remove the `@ServiceConnection` annotation from the `Container` fields.
369+
You can also add `@DynamicPropertySource` annotated methods to your declaration class.
370+
371+
372+
373+
[[features.dev-services.testcontainers.at-development-time.devtools]]
374+
==== Using DevTools with Testcontainers at Development Time
375+
376+
When using devtools, you can annotate beans and bean methods with `@RestartScope`.
377+
Such beans won't be recreated when the devtools restart the application.
378+
This is especially useful for Testcontainer `Container` beans, as they keep their state despite the application restart.
379+
380+
include-code::MyContainersConfiguration[]
381+
382+
WARNING: If you're using Gradle and want to use this feature, you need to change the configuration of the `spring-boot-devtools` dependency from `developmentOnly` to `testAndDevelopmentOnly`.
383+
With the default scope of `developmentOnly`, the `bootTestRun` task will not pick up changes in your code, as the devtools are not active.

0 commit comments

Comments
 (0)