Developing REST APIs is a common practice in modern backend projects. Spring Boot greatly simplifies this task, offering a clear structure, integration with popular libraries, and support for environment profiles. In this article, you'll learn how to create a simple REST API with Spring Boot and deploy it using Docker. This approach ensures portability, repeatability, and ease of deployment in any environment.
1. Create the project with Spring Boot
You can use Spring Initializr To generate the basic structure, select:
- Project: Maven
- Language: Java
- Dependencies: Spring Web, Spring Data JPA, PostgreSQL Driver
Once the project is generated, add an entity and a basic REST controller:
@Entity public class Task { @Id @GeneratedValue private Long id; private String title; private boolean completed; // getters and setters } @RestController @RequestMapping("/tasks") public class TaskController { @Autowired private TaskRepository repository; @GetMapping public List getAll() { return repository.findAll(); } }2. Configure Docker
To containerize the application, create a Dockerfile:
FROM openjdk:17-jdk-slim ARG JAR_FILE=target/*.jar COPY ${JAR_FILE} app.jar ENTRYPOINT ["java","-jar","/app.jar"]And a docker-compose.yml to start the app and PostgreSQL:
version: '3.8' services: db: image: postgres:15 environment: POSTGRES_DB: tasks POSTGRES_USER: user POSTGRES_PASSWORD: pass ports: - "5432:5432" app: build: . ports: - "8080:8080" depends_on: - db environment: SPRING_DATASOURCE_URL: jdbc:postgresql://db:5432/tasks SPRING_DATASOURCE_USERNAME: user SPRING_DATASOURCE_PASSWORD: pass3. Test the local environment
Execute docker-compose up --build. Access http://localhost:8080/tasks to test the endpoint. You can use Postman either curl to make requests GET, POST, etc.
4. Production Considerations
- Use profiles
application-dev.ymlandapplication-prod.yml - Configure
Flywayfor scheme management - Adds structured logging and global error handling
- Use lighter base images (Alpine)
With this configuration you have a basic development and deployment environment ready to grow.





