Skip to content

Commit b5c8da0

Browse files
Add Docker Compose sample for Doma Spring Boot (#291)
Co-Authored-By: [email protected] <[email protected]>
1 parent 64e373b commit b5c8da0

File tree

13 files changed

+284
-0
lines changed

13 files changed

+284
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM eclipse-temurin:17-jdk AS build
2+
WORKDIR /app
3+
COPY . /app
4+
RUN ./mvnw clean package -DskipTests
5+
6+
FROM eclipse-temurin:17-jre
7+
WORKDIR /app
8+
COPY --from=build /app/target/*.jar app.jar
9+
ENTRYPOINT ["java", "-jar", "app.jar"]
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Doma Spring Boot Sample with Docker Compose
2+
3+
This sample demonstrates how to use Doma with Spring Boot using Docker Compose.
4+
5+
## Requirements
6+
7+
- Docker
8+
- Docker Compose
9+
10+
## Running the sample
11+
12+
From the sample directory, run:
13+
14+
```bash
15+
docker compose up
16+
```
17+
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
22+
23+
## Using the application
24+
25+
Once both containers are running, you can access the application at http://localhost:8080
26+
27+
### API Endpoints
28+
29+
- `GET /` - List all messages
30+
- `GET /?text=hello` - Add a new message with the text "hello"
31+
32+
## Running the application locally
33+
34+
If you want to run the application locally while using the Docker PostgreSQL database:
35+
36+
1. Start only the database container:
37+
```bash
38+
docker compose up postgres
39+
```
40+
41+
2. Run the application:
42+
```bash
43+
./mvnw spring-boot:run
44+
```
45+
46+
## Notes
47+
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)
54+
- 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.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
version: '3.8'
2+
3+
services:
4+
postgres:
5+
image: postgres:16
6+
container_name: doma-postgres
7+
environment:
8+
POSTGRES_DB: ${POSTGRES_DB:-domadb}
9+
POSTGRES_USER: ${POSTGRES_USER:-doma}
10+
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-changeme}
11+
ports:
12+
- "5432:5432"
13+
volumes:
14+
- postgres-data:/var/lib/postgresql/data
15+
16+
app:
17+
build:
18+
context: .
19+
dockerfile: Dockerfile
20+
container_name: doma-app
21+
depends_on:
22+
- postgres
23+
environment:
24+
SPRING_DATASOURCE_URL: jdbc:postgresql://postgres:5432/${POSTGRES_DB:-domadb}
25+
SPRING_DATASOURCE_USERNAME: ${POSTGRES_USER:-doma}
26+
SPRING_DATASOURCE_PASSWORD: ${POSTGRES_PASSWORD:-changeme}
27+
ports:
28+
- "8080:8080"
29+
30+
volumes:
31+
postgres-data:
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<artifactId>doma-spring-boot-sample-docker-compose</artifactId>
7+
<packaging>jar</packaging>
8+
9+
<name>doma-spring-boot-sample-docker-compose</name>
10+
<description>Demo project with Docker Compose for Spring Boot and Doma</description>
11+
12+
<parent>
13+
<groupId>org.seasar.doma.boot</groupId>
14+
<artifactId>doma-spring-boot-samples</artifactId>
15+
<version>2.5.0-SNAPSHOT</version>
16+
<relativePath>../pom.xml</relativePath>
17+
</parent>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.seasar.doma</groupId>
22+
<artifactId>doma-core</artifactId>
23+
<version>${doma.version}</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.seasar.doma</groupId>
27+
<artifactId>doma-processor</artifactId>
28+
<version>${doma.version}</version>
29+
<optional>true</optional>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.seasar.doma.boot</groupId>
33+
<artifactId>doma-spring-boot-starter</artifactId>
34+
<version>${project.version}</version>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-web</artifactId>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.postgresql</groupId>
42+
<artifactId>postgresql</artifactId>
43+
<scope>runtime</scope>
44+
</dependency>
45+
<dependency>
46+
<groupId>com.h2database</groupId>
47+
<artifactId>h2</artifactId>
48+
<scope>test</scope>
49+
</dependency>
50+
<dependency>
51+
<groupId>org.springframework.boot</groupId>
52+
<artifactId>spring-boot-starter-test</artifactId>
53+
<scope>test</scope>
54+
</dependency>
55+
<dependency>
56+
<groupId>org.junit.jupiter</groupId>
57+
<artifactId>junit-jupiter</artifactId>
58+
<scope>test</scope>
59+
</dependency>
60+
</dependencies>
61+
62+
<build>
63+
<plugins>
64+
<plugin>
65+
<groupId>org.springframework.boot</groupId>
66+
<artifactId>spring-boot-maven-plugin</artifactId>
67+
<version>${spring-boot.version}</version>
68+
</plugin>
69+
</plugins>
70+
</build>
71+
72+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.seasar.doma.boot.sample;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Application {
8+
9+
@SuppressWarnings("resource")
10+
public static void main(String[] args) {
11+
SpringApplication.run(Application.class, args);
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.seasar.doma.boot.sample;
2+
3+
import org.seasar.doma.*;
4+
5+
@Entity
6+
@Table(name = "messages")
7+
public class Message {
8+
@Id
9+
@GeneratedValue(strategy = GenerationType.IDENTITY)
10+
public Integer id;
11+
12+
public String text;
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.seasar.doma.boot.sample;
2+
3+
import java.util.List;
4+
5+
import org.seasar.doma.boot.Pageables;
6+
import org.springframework.data.domain.Pageable;
7+
import org.springframework.data.web.PageableDefault;
8+
import org.springframework.web.bind.annotation.GetMapping;
9+
import org.springframework.web.bind.annotation.RequestMapping;
10+
import org.springframework.web.bind.annotation.RequestParam;
11+
import org.springframework.web.bind.annotation.RestController;
12+
13+
@RestController
14+
@RequestMapping("/")
15+
public class MessageController {
16+
17+
private final MessageDao messageDao;
18+
19+
public MessageController(MessageDao messageDao) {
20+
this.messageDao = messageDao;
21+
}
22+
23+
@GetMapping
24+
List<Message> list(@PageableDefault Pageable pageable) {
25+
return messageDao.selectAll(Pageables.toSelectOptions(pageable));
26+
}
27+
28+
@GetMapping(params = "text")
29+
Message add(@RequestParam String text) {
30+
Message message = new Message();
31+
message.text = text;
32+
messageDao.insert(message);
33+
return message;
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.seasar.doma.boot.sample;
2+
3+
import java.util.List;
4+
5+
import org.seasar.doma.Dao;
6+
import org.seasar.doma.Insert;
7+
import org.seasar.doma.Select;
8+
import org.seasar.doma.boot.ConfigAutowireable;
9+
import org.seasar.doma.jdbc.SelectOptions;
10+
import org.springframework.transaction.annotation.Transactional;
11+
12+
@Dao
13+
@ConfigAutowireable
14+
@Transactional
15+
public interface MessageDao {
16+
@Select
17+
List<Message> selectAll(SelectOptions options);
18+
19+
@Insert
20+
int insert(Message message);
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SELECT id, text FROM messages ORDER BY id;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
doma.dialect=POSTGRES
2+
spring.datasource.url=jdbc:postgresql://localhost:5432/${POSTGRES_DB:domadb}
3+
spring.datasource.username=${POSTGRES_USER:doma}
4+
spring.datasource.password=${POSTGRES_PASSWORD:changeme}
5+
spring.datasource.driver-class-name=org.postgresql.Driver
6+
logging.level.org.springframework.jdbc.datasource.DataSourceTransactionManager=DEBUG

0 commit comments

Comments
 (0)