Back

/ 2 min read

Spring Boot—Quick Start

Last Updated:

What is Spring (Boot)?

The Spring framework is an open-source framework for building Java web-based applications. It has a lot of features that make it a popular choice for developers. On the other hand, Spring Boot is a project that is built on top of the Spring framework. It provides a simpler and faster way to set up, configure, and run both web-based and standalone applications.

In this article, we will discuss how to get started with Spring Boot with Docker and devcontainer.

Getting Started

We will use docker devcontainer to develop the Spring Boot application. Please make sure you have Docker and Visual Studio Code installed on your machine.

Step 1: Setup devcontainer

Create a new directory for your project and create a new file named dockerfile.devel with the following content:

dockerfile.devel
FROM FROM mcr.microsoft.com/devcontainers/java:1-21-bookworm
# args
ARG GRADLE_VERSION=8.7
ARG MAVEN_VERSION=3.8.4
# check sdkman installation
RUN bash -c "source /usr/local/sdkman/bin/sdkman-init.sh && sdk version"
# install gradle
RUN bash -c "source /usr/local/sdkman/bin/sdkman-init.sh && sdk install gradle ${GRADLE_VERSION}"
# install maven
RUN bash -c "source /usr/local/sdkman/bin/sdkman-init.sh && sdk install maven ${MAVEN_VERSION}"
CMD [ "bash" ]

Suppose we are using PostgreSQL as the database. We need to create a new file named docker-compose.devel.yaml with the following content:

docker-compose.devel.yaml
services:
java-devel:
container_name: java-devel
build:
context: .
dockerfile: dockerfile.devel
args:
- GRADLE_VERSION=8.7
- MAVEN_VERSION=3.8.4
volumes:
- .:/home/vscode/workspace
command: sleep infinity
networks:
- java-devel-network
depends_on:
- java-postgres
java-postgres:
container_name: java-postgres
image: postgres:16-alpine
restart: always
env_file: .env
volumes:
- ./data/postgres:/var/lib/postgresql/data
command: -p ${POSTGRES_PORT:-5432}
networks:
- java-devel-network
networks:
java-devel-network:
name: java-devel-network
driver: bridge

We are using docker compose to manage the development environment. We have two services: java-devel and java-postgres. The java-devel service is used to develop the Spring Boot application, while the java-postgres service is used to run the PostgreSQL database.

Next, we need to create a new directory .devcontainer and create a new file named devcontainer.json with the following content:

.devcontainer/devcontainer.json
{
"name": "java-devel",
"service": "java-devel",
"dockerComposeFile": "../docker-compose.devel.yaml",
"features": {
"ghcr.io/devcontainers/features/docker-outside-of-docker": {}
},
"workspaceFolder": "/home/vscode/workspace",
"workspaceMount": "source=${localWorkspaceFolder},target=/home/vscode/workspace,type=bind",
"containerEnv": {
"TZ": "Asia/Jakarta"
},
"customizations": {
"vscode": {
"extensions": [
"redhat.java",
"vscjava.vscode-java-debug",
"vscjava.vscode-java-test",
"vscjava.vscode-maven",
"vscjava.vscode-gradle",
"vscjava.vscode-java-dependency",
"vmware.vscode-boot-dev-pack",
"eamodio.gitlens",
"redhat.vscode-yaml",
"redhat.vscode-xml",
"yzhang.markdown-all-in-one"
]
}
}
}

Our directory structure should look like this:

.
├── .devcontainer
│ └── devcontainer.json
├── docker-compose.devel.yaml
└── dockerfile.devel

In order to start the devcontainer environment, we can open the project directory in VSCode and press CTRL+SHIFT+P and type Dev Containers: Reopen in Container. This will start the devcontainer environment.