Skip to content

Commit 9a54fdc

Browse files
Update Docker Compose sample to use Spring Boot's built-in Docker Compose support (#291)
Co-Authored-By: [email protected] <[email protected]>
1 parent 492079e commit 9a54fdc

File tree

14 files changed

+531
-107
lines changed

14 files changed

+531
-107
lines changed

doma-spring-boot-samples/doma-spring-boot-sample-docker-compose/Dockerfile

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Getting Started
2+
3+
### Reference Documentation
4+
For further reference, please consider the following sections:
5+
6+
* [Official Apache Maven documentation](https://maven.apache.org/guides/index.html)
7+
* [Spring Boot Maven Plugin Reference Guide](https://docs.spring.io/spring-boot/3.4.5/maven-plugin)
8+
* [Create an OCI image](https://docs.spring.io/spring-boot/3.4.5/maven-plugin/build-image.html)
9+
* [Docker Compose Support](https://docs.spring.io/spring-boot/3.4.5/reference/features/dev-services.html#features.dev-services.docker-compose)
10+
* [Spring Web](https://docs.spring.io/spring-boot/3.4.5/reference/web/servlet.html)
11+
12+
### Guides
13+
The following guides illustrate how to use some features concretely:
14+
15+
* [Building a RESTful Web Service](https://spring.io/guides/gs/rest-service/)
16+
* [Serving Web Content with Spring MVC](https://spring.io/guides/gs/serving-web-content/)
17+
* [Building REST services with Spring](https://spring.io/guides/tutorials/rest/)
18+
19+
### Docker Compose support
20+
This project contains a Docker Compose file named `compose.yaml`.
21+
In this file, the following services have been defined:
22+
23+
* postgres: [`postgres:latest`](https://hub.docker.com/_/postgres)
24+
25+
Please review the tags of the used images and set them to the same as you're running in production.
26+
27+
### Maven Parent overrides
28+
29+
Due to Maven's design, elements are inherited from the parent POM to the project POM.
30+
While most of the inheritance is fine, it also inherits unwanted elements like `<license>` and `<developers>` from the parent.
31+
To prevent this, the project POM contains empty overrides for these elements.
32+
If you manually switch to a different parent and actually want the inheritance, you need to remove those overrides.
33+
Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,69 @@
11
# Doma Spring Boot Sample with Docker Compose
22

3-
This sample demonstrates how to use Doma with Spring Boot using Docker Compose.
3+
This sample demonstrates how to use Doma with Spring Boot's Docker Compose support.
44

55
## Requirements
66

77
- Docker
88
- Docker Compose
99

10+
## How it works
11+
12+
This sample uses Spring Boot's built-in Docker Compose support, which automatically:
13+
14+
1. Detects the `compose.yaml` file in the project root
15+
2. Starts the PostgreSQL container defined in the compose file
16+
3. Configures the application to connect to the database
17+
18+
The `spring-boot-docker-compose` dependency enables this functionality, allowing the application to seamlessly integrate with Docker Compose services.
19+
1020
## Running the sample
1121

1222
From the sample directory, run:
1323

1424
```bash
15-
docker compose up
25+
./mvnw spring-boot:run
1626
```
1727

18-
This will:
19-
1. Start a PostgreSQL database container
20-
2. Build the Spring Boot application
21-
3. Start the application container linked to the database
28+
Spring Boot will automatically:
29+
- Start the PostgreSQL container defined in compose.yaml
30+
- Configure the application to connect to the database
31+
- Run the application
2232

2333
## Using the application
2434

25-
Once both containers are running, you can access the application at http://localhost:8080
35+
Once the application is running, you can access it at http://localhost:8080
2636

2737
### API Endpoints
2838

2939
- `GET /` - List all messages
3040
- `GET /?text=hello` - Add a new message with the text "hello"
3141

32-
## Running the application locally
42+
## Configuration
3343

34-
If you want to run the application locally while using the Docker PostgreSQL database:
44+
The PostgreSQL configuration is defined in the `compose.yaml` file:
3545

36-
1. Start only the database container:
37-
```bash
38-
docker compose up postgres
39-
```
46+
```yaml
47+
services:
48+
postgres:
49+
image: 'postgres:latest'
50+
environment:
51+
- 'POSTGRES_DB=${POSTGRES_DB}'
52+
- 'POSTGRES_PASSWORD=${POSTGRES_PASSWORD}'
53+
- 'POSTGRES_USER=${POSTGRES_USER}'
54+
ports:
55+
- '5432'
56+
```
57+
58+
Before running the application, you should set the following environment variables:
59+
- `POSTGRES_DB`: The name of the PostgreSQL database (e.g., "domadb")
60+
- `POSTGRES_USER`: The PostgreSQL username (e.g., "doma")
61+
- `POSTGRES_PASSWORD`: The PostgreSQL password
4062

41-
2. Run the application:
42-
```bash
43-
./mvnw spring-boot:run
44-
```
63+
Spring Boot automatically configures the application to connect to this database.
4564

4665
## Notes
4766

48-
- The PostgreSQL data is persisted in a Docker volume
49-
- The application connects to PostgreSQL using environment variables with default values
50-
- You can customize the database configuration by setting the following environment variables:
51-
- `POSTGRES_DB`: Database name (default: domadb)
52-
- `POSTGRES_USER`: Database username (default: doma)
53-
- `POSTGRES_PASSWORD`: Database password (default: changeme)
67+
- No manual configuration of database connection properties is needed
5468
- The schema.sql file is automatically executed when the application starts
55-
56-
## Security Note
57-
58-
For production use, always set secure passwords through environment variables rather than using the defaults.
69+
- Spring Boot manages the lifecycle of the Docker containers
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
services:
2+
postgres:
3+
image: 'postgres:latest'
4+
environment:
5+
- 'POSTGRES_DB=${POSTGRES_DB}'
6+
- 'POSTGRES_PASSWORD=${POSTGRES_PASSWORD}'
7+
- 'POSTGRES_USER=${POSTGRES_USER}'
8+
ports:
9+
- '5432'

doma-spring-boot-samples/doma-spring-boot-sample-docker-compose/docker-compose.yml

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)