Home Post Spring Framework

Dockerize Spring Boot app and Push image to DockerHub (Spring Boot + DockerHub)

Mar 31, 2024

In this article we will see how to "Dockerize Spring Boot app and Push/Pull image to/from DockerHub".

1) Download and Install Docker Desktop

Download the latest Docker Desktop release and install it:

2) Create a DockerHub account

Go to DockerHub and create an account, we need these credentials in Push/Pull image to/from DockerHub

3) Create Spring Boot App

3.1) Dependencies

Let's create a project with Spring Initializr, make sure to add required dependencies as shown in the picture below:

3.2) Controller

Let's create a demo rest endpoint for our service; a simple get call returning string message.

3.3) Dockerfile

In order to dockerize Spring Boot app, one file named 'Dockerfile' needs to be added in root directory.

3.4) Generate .jar file

Generate "spring-boot-docker.jar" inside "target" directory:

mvn clean install

4) Dockerize app

Open command line and navigate to project directory to create an image of Spring Boot app:

% cd ~/code/spring-boot-docker
% docker build -t spring-boot-docker:1 .

In order to check if docker image is created successfully, run following command:

% docker images
REPOSITORY           TAG       IMAGE ID       CREATED         SIZE
spring-boot-docker   1         da08505a6a6b   6 minutes ago   489MB

Lets try to run this image on Docker locally to make sure everything happened as expected:

% docker run -p 9001:8080 spring-boot-docker:1

Here "8080" is the docker's internal port, the application is running on this port inside the container. We have mapped this port to our machine's "9001" port so that we can access this through browser outside docker container.

Let's check if the container is built successfully.

% docker ps -a

CONTAINER ID   IMAGE                  COMMAND                  CREATED          STATUS                       PORTS                    NAMES
53faaf02d5e3   spring-boot-docker:1   "java -jar /spring-b…"   23 seconds ago   Up 22 seconds      >8080/tcp   eloquent_heyrovsky

The application is accessable on "localhost:9001" as shown below:

% curl http://localhost:9001/message
From Spring Boot Docker APP !%

Source code on Github: spring-boot-docker

5) Push image to DockerHub

Here we are done with generating a docker image for our Spring Boot application, lets push it to DockerHub:

% docker login

Username: codeburps

Login Succeeded

Now lets tag the "image" with "codeburps/spring-boot-docker:1" :

% docker tag spring-boot-docker:1 codeburps/spring-boot-docker:1

Here "spring-boot-docker" is the image name, "codeburps" is the DockerHub username and "1" is tag name for this image, we can give any name as tag name.

Lets push this newly tagged image to "DockerHub":

% docker push codeburps/spring-boot-docker:1

If everything goes fine, you should see this newly pushed image in your DockerHub dashboard, as shown below:

We can pull this image to our local machine or to any other server with following command:

% docker pull codeburps/spring-boot-docker:1

NK Chauhan

NK Chauhan is a Principal Software Engineer with one of the biggest E Commerce company in the World.

Chauhan has around 12 Yrs of experience with a focus on JVM based technologies and Big Data.

His hobbies include playing Cricket, Video Games and hanging with friends.

Spring Framework
Core Java
Java Concurrency