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

  • 4.1/5
  • 3986
  • Jul 20, 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                0.0.0.0:9001->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
Password: 

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
Index
How to Implement PostgreSQL Full-Text Search with Spring Boot

15 min

Spring's transaction management with the @Transactional annotation

9 min

Spring Boot Rest APIs with PostgreSQL (Spring Boot + Rest APIs)

15 min

Caching in Spring Boot (@Cacheable, @CacheEvict & @CachePut)

21 min

Declarative REST Client in Spring Boot (Spring 6 HTTP Interface)

13 min

Profiling a Spring Boot application with Pyroscope

7 min

Service discovery in Spring Boot (Spring Cloud + Netflix Eureka)

9 min

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

4 min

Creating a Jenkins Pipeline for Spring Boot application

2 min

Circuit Breaker Pattern in Microservices (Spring BOOT + Resilience4j)

4 min

Edge Server Pattern in Microservices (Spring Cloud Gateway)

7 min

Monitoring Microservices (Spring Boot + Micrometer + Prometheus + Grafana)

7 min

Spring Cloud config server setup with Git

8 min

Distributed Tracing in Microservices (Spring Cloud Sleuth + Zipkin)

9 min

Circuit Breaker Pattern with Resilience4J in a Spring Boot Application

24 min

Deploying Spring Boot microservices on Kubernetes Cluster

12 min

Reactive programming in Java with Project Reactor

50 min

Spring Reactive with PostgreSQL (Spring Boot WebFlux + PostgreSQL)

13 min

Spring Reactive, Thymeleaf Hello World (Spring Webflux + Thymeleaf + JS/CSS)

9 min

Problem JSON (application/problem+json) in Spring WebFlux

15 min

Spring Boot Login/Logout (Spring Security + MySql + Thymeleaf)

21 min

Securing Server-to-Server Communication with "Spring Boot" & "OAuth 2"

18 min

Sending Emails in Spring Boot via SMTP

7 min

How to create a basic Spring 6 project using Maven

5 min

Spring Boot, Thymeleaf Hello World (Spring Boot + Thymeleaf + JS/CSS)

9 min