|
1 | 1 | # Doma Spring Boot Sample with Docker Compose |
2 | 2 |
|
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. |
4 | 4 |
|
5 | 5 | ## Requirements |
6 | 6 |
|
7 | 7 | - Docker |
8 | 8 | - Docker Compose |
9 | 9 |
|
| 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 | + |
10 | 20 | ## Running the sample |
11 | 21 |
|
12 | 22 | From the sample directory, run: |
13 | 23 |
|
14 | 24 | ```bash |
15 | | -docker compose up |
| 25 | +./mvnw spring-boot:run |
16 | 26 | ``` |
17 | 27 |
|
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 |
22 | 32 |
|
23 | 33 | ## Using the application |
24 | 34 |
|
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 |
26 | 36 |
|
27 | 37 | ### API Endpoints |
28 | 38 |
|
29 | 39 | - `GET /` - List all messages |
30 | 40 | - `GET /?text=hello` - Add a new message with the text "hello" |
31 | 41 |
|
32 | | -## Running the application locally |
| 42 | +## Configuration |
33 | 43 |
|
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: |
35 | 45 |
|
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 |
40 | 62 |
|
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. |
45 | 64 |
|
46 | 65 | ## Notes |
47 | 66 |
|
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 |
54 | 68 | - 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 |
0 commit comments