Build and push docker Image using a Spring Boot Application


This article aims to walk through the Process of Creating a Docker Image and then push it to Docker Hub. We are going to create an image of a Spring boot application. You can download a sample spring boot application from Github (link provided below).

To know more about docker you can read our introduction on Docker here.


Setup Docker Hub Profile – Create a profile on DockerHub. Once Logged in create your repository which will hold the docker image.

Install Docker desktop – Docker Desktop is an open platform that provides all the docker tools to your computer. Download and install the docker application for Mac, Windows, or Linux from the Docker Desktop site.

Containerize Application Using DockerFile

Now we will containerize our application, before creating a docker image. First, create a spring boot application or download a sample maven based application from GitHub. Build and run this application to ensure its working fine. Now we need to create a Dockerfile. Dockerfile is a simple document which contains sequences of command which are required for building an image. We are going to create this at the same hierarchy as pom.xml.

Sample Docker File

FROM openjdk:8-jdk-alpine
WORKDIR target/dependency
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
RUN jar -xf ./app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

FROM defines the base image to define the build process. We are  using ‘openjdk’ with the tag as ‘8-jdk-alpine’

WORKDIR  sets the working directory for any RUN, CMD, ENTRYPOINT, COPY, and ADD instructions that follow in the Dockerfile.

COPY will copy the project JAR into the container as app.jar.

RUN executes the shell command line within the target system.

ENTRYPOINT sets as a default application to be used every time a container is created with the image.

Build and Push an Image

  • Build an Image – Create a docker image to push to the same repository in the docker hub with a tag name in small letters. Here, we have created in a repository named as ‘firstphasetech/dockertest’ with tagname as ‘dockersampletest’

docker build -t firstphasetech/dockertest:dockersampletest .

  • Push Image – Pushing and pulling Image to Docker Hub Repository. The Docker push might fail if the configuration doesn’t match with the dockerID and repository path. In case you are getting an Access Denied error, log in to the Docker hub passing login id and password, and then try pushing the image again.

docker push firstphasetech/dockertest:dockersampletest.                      

  • Run Image – Docker Image can also be run without publishing images to the docker hub using the one that was built locally. We can run the docker image present locally.

docker run -p 8090:8080  firstphasetech/dockertest:dockersampletest

     Here, we have to map the port of the host operation system 8090 and the port inside the container 8080 using -p tag. The application is now available on http://localhost:8080

Useful Docker Commands

  1. Docker images: It will show up all docker images, their repositories, tags, and sizes.
  2. docker rmi: It will delete the docker image.


We dockerize Java, Node Js, .Net applications, create images, and get them deployed. Once the image is created, there are various ways to run it. In Production, we generally opt for Kubernetes which scale our application based on network traffic. We will cover about Kubernetes in our next article.

Sample Spring Boot Application at Github-


Happy Learning!