For development purposes, you can easily run Spring Boot apps with Docker Compose.
Dockerize Spring Boot on development environment
We’re going to start a Spring Boot application written in Java 11 (OpenJDK) in a container. Additionally, you can see the example spring-boot-log4j-2-scaffolding project for more details.
Build a Docker image for your app
Firstly, we have to create a JAR file for our project. Run the following command in the project’s directory:
1 |
$ mvn package |
As a result, Maven builds the ready to use JAR file, as stated in docs:
The package goal will compile your Java code, run any tests, and finish by packaging the code up in a JAR file within the target directory.
https://spring.io/guides/gs/maven/
Secondly, create the Dockerfile
file and copy the following code:
1 2 3 4 5 6 |
# Dockerfile FROM openjdk:11-jdk-slim VOLUME /tmp ARG JAR_FILE=target/*.jar COPY ${JAR_FILE} app.jar ENTRYPOINT ["java","-jar","/app.jar"] |
Make sure, that the ARG will contain the proper target
path. If your Dockerfile
isn’t in the same directory as the target
folder, the build process for a service called ‘app’ will exit with the following error:
1 2 |
Step 4/5 : COPY ${JAR_FILE} app.jar ERROR: Service 'app' failed to build: COPY failed: no source files were specified |
You can fix it in two different ways:
- provide an absolute path to the
target
; - move
Dockerfile
so it’s in the same directory astarget
.
Run Spring Boot with Docker Compose
Create the docker-compose.yml
file and put the following code in it:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# docker-compose.yml version: "3.3" services: app: build: context: ./ ports: - "8080:8080" networks: - internal networks: internal: |
- Our build context (
Dockerfile
) is in the same folder as the compose file. Therefore we specify the location just with the./
path. - We keep using the default 8080 port. As a result we still can access our app by visiting http://localhost:8080/.
- It’s likely that we want to enable communication between our Spring Boot app and other services that might be specified in the
docker-compose.yml
file. Thus, we define a network that they can share.
Finally, we can run the application. Remember to use the name of your service instead of my name – app
:
1 |
$ docker-compose up -d app |
In case of any changes, rebuild the image:
1 |
$ docker-compose up -d --build app |
In the end, you can verify whether the service was started by running in the console:
1 |
$ docker ps |
The output should be similar to the one presented in the screenshot below:
In addition, you can find the code responsible for building and running a Docker container with my Spring Boot project in the commit e2e4862fe782c5f3ac089ebcdf6521881f0794df.
Learn more on building Spring Boot apps with Docker
- Spring Boot in a Container
- Furthermore, you can use IntelliJ features to Run and debug a Spring Boot application using Docker Compose
- Spring Boot & Docker using VOLUME /tmp
- More on Docker
Photo by Retha Ferguson from Pexels