Spring Boot 3.0 API deployment using Jenkins pipeline and Docker

Eric Anicet
5 min readApr 4


In this story, we’ll explain step by step how to deploy a Spring boot application using Jenkins pipeline and Docker.

· Prerequisites
· Spring boot application setup
Project Structure
Containerize application
· Github
· Configure Jenkins
· Build Jenkins pipeline job
· Conclusion
· References


This is the list of all the prerequisites for following this story:

  • Spring Boot 3
  • Maven 3.6.+
  • Java 17
  • A GitHub repository
  • Git
  • Jenkins instance installed
  • Docker installed
  • Docker compose installed
  • PostgreSQL
  • Postman / insomnia or any other API testing tool (optional)

Spring boot application setup

Suppose we already have a simple Spring Boot Rest API that exposes book endpoints. The application is connected to the PostgreSQL database.

Project Structure

Here is our project structure:

book rest api

It works when trying to retrieve the list of books.

GET http://localhost:8080/api/book

Containerize application

  1. Dockerfile

To build the container image with docker, we’ll need to use a file Dockerfile. A Dockerfile is simply a text-based file with no file extension that contains a script of instructions. Docker uses this script to build a container image. The file must be created at the root of the project.

# creates a layer from the openjdk:17-alpine Docker image.
FROM openjdk:17-alpine

MAINTAINER boottechnologies.ci@gmail.com

# cd /app

# Refer to Maven build -> finalName
ARG JAR_FILE=target/spring-boot-docker-*.jar

# cp target/spring-boot-docker-0.0.1-SNAPSHOT.jar /app/spring-boot-docker.jar
COPY ${JAR_FILE} spring-boot-docker.jar

# java -jar /app/spring-boot-docker.jar
CMD ["java", "-jar", "-Xmx1024M", "/app/spring-boot-docker.jar"]

# Make port 8090 available to the world outside this container

2. Docker compose

Docker Compose is a tool that was developed to help define and share multi-container applications. With Compose, we can create a YAML file to define the services and with a single command, can spin everything up or tear it all down.

In this case, we need to create 2 containers (database and API application). At the root of the project folder, create a file named docker-compose.yml

version: '3.8'

build: .
- '8090:8090'
container_name: bookapi
- SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/bookdb
- 'db:database'
image: postgres:15.2
restart: always
POSTGRES_USER: book-user
- db-data:/var/lib/postgresql/data
- 6432:5432
driver: local

3. Using a Jenkinsfile

A Jenkinsfile is a text file that contains the definition of a Jenkins Pipeline and is checked into source control. Let’s create a new Jenkinsfile in the root directory of the project which implements a basic three-stage continuous delivery pipeline.

pipeline {
agent any
environment {
MAVEN_ARGS=" -e clean install"
registry = ""
dockerContainerName = 'bookapi'
dockerImageName = 'bookapi-api'
stages {
stage('Build') {
steps {
withMaven(maven: 'MAVEN_ENV') {
sh "mvn ${MAVEN_ARGS}"

stage('clean container') {
steps {
sh 'docker ps -f name=${dockerContainerName} -q | xargs --no-run-if-empty docker container stop'
sh 'docker container ls -a -fname=${dockerContainerName} -q | xargs -r docker container rm'
sh 'docker images -q --filter=reference=${dockerImageName} | xargs --no-run-if-empty docker rmi -f'
stage('docker-compose start') {
steps {
sh 'docker compose up -d'


Once we have completed all the previous configurations, we need to push all the source code to our GitHub repository.

If you have a private repository, you must provide a personal access token in GitHub to use in Jenkins jobs.

The new repository link for Jenkins will look like the following syntax:


Configure Jenkins

  1. Install Maven Pipeline integration plugins

Jenkins Dashboard > Manage Jenkins -> Manage Plugins

Search “maven” in the Available section and “Install without restart”

2. Add Global Tool Configuration

Jenkins Dashboard > Manage Jenkins -> Global Tool Configuration

3. Stack deployment

> Create a New Item

> Pipeline configuration

  1. From the Definition field, choose the Pipeline script from SCM option: With the Pipeline script from SCM option selected, you do not enter any Groovy code in the Jenkins UI; you just indicate by specifying a path where in source code you want to retrieve the pipeline from.
  2. From the SCM field, choose the type of source control system of the repository containing your Jenkinsfile. Select the Git option for this case.
  3. The URL of the Git repository in the Repository URL field where we stored your pipeline script. (For this story, the repository is public. we don’t need personal access)
  4. The branch name of the Git repository in the Branch Specifier field.

Apply and Save

It’s done your pipeline is created.

Build Jenkins pipeline job

Click on the Build Now option to run the Jenkins pipeline. We can see all the stages that have been described in the pipeline file.

After the pipeline build is successful, we can check the containers with the docker command docker ps

The stack is well deployed. Congratulations.


In this story, We have seen how to deploy step by step a Spring boot application using a Jenkins pipeline and Docker.

The complete source code is available on GitHub.

If you enjoyed this story, please give it a few claps 👏.

Thanks for reading!




Eric Anicet

Recommended from Medium


See more recommendations