Skip to content

Commit a0b8540

Browse files
committed
Add a 'Docker Compose Support' section to the reference docs
Update the reference documentation with details about the recently introduced Docker Compose Support. Closes gh-35026
1 parent e19aa81 commit a0b8540

File tree

3 files changed

+213
-0
lines changed

3 files changed

+213
-0
lines changed

spring-boot-project/spring-boot-docs/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ dependencies {
4949
asciidoctorExtensions(project(path: ":spring-boot-project:spring-boot-actuator-autoconfigure"))
5050
asciidoctorExtensions(project(path: ":spring-boot-project:spring-boot-autoconfigure"))
5151
asciidoctorExtensions(project(path: ":spring-boot-project:spring-boot-devtools"))
52+
asciidoctorExtensions(project(path: ":spring-boot-project:spring-boot-docker-compose"))
5253

5354
autoConfiguration(project(path: ":spring-boot-project:spring-boot-autoconfigure", configuration: "autoConfigurationMetadata"))
5455
autoConfiguration(project(path: ":spring-boot-project:spring-boot-actuator-autoconfigure", configuration: "autoConfigurationMetadata"))

spring-boot-project/spring-boot-docs/src/docs/asciidoc/features.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ include::features/task-execution-and-scheduling.adoc[]
2626

2727
include::features/testing.adoc[]
2828

29+
include::features/docker-compose.adoc[]
30+
2931
include::features/developing-auto-configuration.adoc[]
3032

3133
include::features/kotlin.adoc[]
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
[[features.docker-compose]]
2+
== Docker Compose Support
3+
Docker Compose is a popular technology that can be used to define and manage multiple containers for services that your application needs.
4+
A `compose.yml` file is typically created next to your application which defines and configures service containers.
5+
6+
A typical workflow with Docker Compose is to run `docker compose up`.
7+
Work on your application with it connecting to started services.
8+
Then run `docker compose down` when you are finished.
9+
10+
To help with this workflow, the `spring-boot-docker-compose` module can be used. When this modules is included as a dependency Spring Boot will do the following:
11+
12+
* Search for a `compose.yml` and other common compose filenames in your application directory
13+
* Call `docker compose up` with the discovered `compose.yml`
14+
* Create service connection beans for each supported container
15+
* Call `docker compose down` when the application is shutdown
16+
17+
NOTE: The `docker compose` or `docker-compose` CLI application needs to be on your path in order for Spring Boot’s support to work correctly.
18+
19+
20+
21+
[[features.docker-compose.service-connections]]
22+
=== Service Connections
23+
A service connection is a connection to any remote service.
24+
Spring Boot’s auto-configuration can consume the details of a service connection and use them to establish a connection to a remote service.
25+
When doing so, the connection details take precedence over any connection-related configuration properties.
26+
27+
When using Spring Boot’s Docker Compose support, service connections are established to the port mapped by the container.
28+
29+
NOTE: Docker compose is usually used in such a way that the ports inside the container are mapped to ephemeral ports on your computer.
30+
For example, A Postgres server my run inside the container using port 5432 but be mapped to a totally different port locally.
31+
The service connection will always discover and use the locally mapped port.
32+
33+
Service connections are established by using the image name of the container.
34+
The following service connections are currently supported:
35+
36+
37+
|===
38+
| Connection Details | Matched on
39+
40+
| `ElasticsearchConnectionDetails`
41+
| Containers named "elasticsearch"
42+
43+
| `JdbcConnectionDetails`
44+
| Containers named "mariadb", "mysql" or "postgres"
45+
46+
| `MongoConnectionDetails`
47+
| Containers named "mongo"
48+
49+
| `R2dbcConnectionDetails`
50+
| Containers named "mariadb", "mysql" or "postgres"
51+
52+
| `RabbitConnectionDetails`
53+
| Containers named "rabbitmq"
54+
55+
| `RedisConnectionDetails`
56+
| Containers named "redis"
57+
58+
| `ZipkinConnectionDetails`
59+
| Containers named "zipkin".
60+
|===
61+
62+
63+
64+
[[features.docker-compose.custom-images]]
65+
=== Custom Images
66+
Sometimes you may need to use your own version of an image to provide a service.
67+
You can use any custom image as long as it behaves in the same way as the standard image.
68+
Specifically, any environment variables that the standard image supports must also be used in your custom image.
69+
70+
If your image uses a different name, you can use a label in your `compose.yml` file so that Spring Boot can provide a service connection.
71+
Use a label named `org.springframework.boot.service-connection` to provide the service name.
72+
73+
For example:
74+
75+
[source,yaml,indent=0]
76+
----
77+
services:
78+
redis:
79+
image: 'mycompany/mycustomredis:7.0'
80+
ports:
81+
- '6379'
82+
labels:
83+
org.springframework.boot.service-connection: redis
84+
----
85+
86+
87+
88+
[[features.docker-compose.skipping]]
89+
=== Skipping Specific Containers
90+
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.
91+
Any container with labeled with `org.springframework.boot.ignore` will be ignored by Spring Boot.
92+
93+
For example:
94+
95+
[source,yaml,indent=0]
96+
----
97+
services:
98+
redis:
99+
image: 'redis:7.0'
100+
ports:
101+
- '6379'
102+
labels:
103+
org.springframework.boot.ignore: true
104+
----
105+
106+
107+
108+
[[features.docker-compose.specific-file]]
109+
=== Using a Specific Compose File
110+
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.
111+
Properties can be defined as an exact path or a path that’s relative to your application.
112+
113+
For example:
114+
115+
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
116+
----
117+
spring:
118+
docker:
119+
compose:
120+
file: "../my-compose.yml"
121+
----
122+
123+
124+
125+
[[features.docker-compose.readiness]]
126+
=== Waiting for Container Readiness
127+
Containers started by Docker Compose may take some time to become fully ready.
128+
The recommended way of checking for readiness is to add a `healthcheck` section under the service definition in your `compose.yml` file.
129+
130+
Since it's not uncommon for `healthcheck` configuration to be omitted from `compose.yml` files, Spring Boot also checks directly for service readiness.
131+
By default, a container is considered ready when a TCP/IP connection can be established to its mapped port.
132+
133+
You can disable this on a per-container basis by add a `org.springframework.boot.readiness-check.tcp.disable` label in your `compose.yml` file.
134+
135+
For example:
136+
137+
[source,yaml,indent=0]
138+
----
139+
services:
140+
redis:
141+
image: 'redis:7.0'
142+
ports:
143+
- '6379'
144+
labels:
145+
org.springframework.boot.readiness-check.tcp.disable: true
146+
----
147+
148+
You can also change timeout values in your `application.properties` or `application.yaml` file:
149+
150+
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
151+
----
152+
spring:
153+
docker:
154+
compose:
155+
readiness:
156+
tcp:
157+
connect-timeout: 10s
158+
read-timeout: 5s
159+
----
160+
161+
The overall timeout can be configured using configprop:spring.docker.compose.readiness.timeout[].
162+
163+
TIP: You can also provide your own `ServiceReadinessCheck` implementations and register them in the `spring.factories` file.
164+
165+
166+
167+
[[features.docker-compose.lifecycle]]
168+
=== Controlling the Docker Compose Lifecycle
169+
By default Spring Boot calls `docker compose up` when your application starts and `docker compose down` when it's shutdown.
170+
If you prefer to have different lifecycle management you can use the configprop:spring.docker.compose.lifecycle-management[] property.
171+
172+
The following values are supported:
173+
174+
* `none` - Do not start or stop Docker Compose
175+
* `start-only` - Start Docker Compose on application startup and leave it running
176+
* `start-and-stop` - Start Docker Compose on application startup and stop it on application shutdown
177+
178+
In addition you can use the configprop:spring.docker.compose.startup.command[] property to change if `docker up` or `docker start` is used.
179+
The configprop:spring.docker.compose.shutdown.command[] allows you to configure if `docker down` or `docker stop` is used.
180+
181+
The following example shows how lifecycle management can be configured:
182+
183+
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
184+
----
185+
spring:
186+
docker:
187+
compose:
188+
lifecycle-management: start-and-stop
189+
startup:
190+
command: start
191+
shutdown:
192+
command: stop
193+
timeout: 1m
194+
----
195+
196+
197+
198+
[[features.docker-compose.profiles]]
199+
=== Activating Docker Compose Profiles
200+
Docker Compose profiles are similar to Spring profiles in that they let you adjust your Docker Compose configuration for specific environments.
201+
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:
202+
203+
[source,yaml,indent=0,subs="verbatim",configprops,configblocks]
204+
----
205+
spring:
206+
docker:
207+
compose:
208+
profiles:
209+
active: "myprofile"
210+
----

0 commit comments

Comments
 (0)