Skip to main content

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

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

Comments

Popular posts from this blog

Deploying Spring Boot microservices on Kubernetes Cluster

This article guides you through the deployment of two Spring Boot microservices, namely "order-service" and "inventory-service," on Kubernetes using "MiniKube" . We will establish communication between them, with "order-service" making calls to an endpoint in "inventory-service." Additionally, we will configure "order-service" to be accessible from the local machine's browser . 1) Create Spring Boot microservices The Spring Boot microservices, "order-service" and "inventory-service," have been developed and can be found in this GitHub repository. If you are interested in learning more about creating Spring Boot REST microservices, please refer to this or this (Reactive) link. 2) Build Docker Images The Docker images for both "order-service" and "inventory-service" have already been generated and deployed on DockerHub, as shown below. codeburps/order-service cod...

Circuit Breaker Pattern with Resilience4J in a Spring Boot Application

Read Also: Spring Cloud Circuit Breaker + Resilience4j Resilience4j is a lightweight fault tolerance library that draws inspiration from Netflix Hystrix but is specifically crafted for functional programming. The library offers higher-order functions, known as decorators , designed to augment any functional interface, lambda expression, or method reference with features such as Circuit Breaker, Rate Limiter, Retry, or Bulkhead . These functionalities can be seamlessly integrated within a project, class, or even applied to a single method. It's possible to layer multiple decorators on any functional interface, lambda expression, or method reference, allowing for versatile and customizable fault tolerance. While numerous annotation-based implementations exist online, this article focuses solely on the reactive approach using router predicates and router functions . How Circuit Breaker Pattern works? In general, a circuit breaker functions as an automatic electrical s...

Reactive programming in Java with Project Reactor

Reactive programming is a declarative programming paradigm that focuses on building applications that are responsive, resilient, and scalable in the face of modern challenges like concurrency, distributed systems, and asynchronous data streams . Reactive programming provides a set of tools, patterns, and abstractions to handle asynchronous and event-driven programming more effectively. Imperative programming focuses on describing the step-by-step instructions or commands that the computer needs to follow to achieve a specific task. In this paradigm, you explicitly state how to perform each operation and control flow in your code. The emphasis is on "how" the computation should be done. int sum = 0; for (int i = 1; i Declarative programming emphasizes specifying what you want to achieve rather than detailing how to achieve it. You describe the desired outcome or the properties of the result, and the programming language or framework handles the execution detai...