This code came from this tutorial. I later found that Spring actually maintains a repo full of the code for this tutorial with all the trimmings here.
To run this code, you need to have Java and Maven installed. Then run the Maven wrapper in the root directory like so:
./mvnw clean spring-boot:run
To package this code as a JAR for use in a container, use mvnw
like so:
./mvnw package
This will create a file called target/payroll-0.0.1-SNAPSHOT.jar
. You can
then run it like so:
java -jar target/payroll-0.0.1-SNAPSHOT.jar
To build a Docker image, you can either create a Dockerfile by hand or use the build-image feature of the Spring Boot build plugin. Detail instructions for both approaches is provided here, but if you'd like the short-short version, make sure you Docker environment is configured and running, and then...
./mvnw spring-boot:build-image -Dspring-boot.build-image.imageName=springio/gs-spring-boot-docker
docker run -p 8080:8080 -t springio/gs-spring-boot-docker
You can also do it the long way using the Dockerfile and the script I created that explodes the JAR file into different layers for efficiency:
./build-fromdockerfile.sh
docker run -p 8080:8080 -t springio/gs-spring-boot-docker
For further reference, please consider the following sections:
- Official Apache Maven documentation
- Spring Boot Maven Plugin Reference Guide
- Create an OCI image
- Spring Web
- Spring Data JPA
The following guides illustrate how to use some features concretely: