Dockerizing your Spring Boot App

Pre – Requisite for dockerizing your Spring Boot App

Dockerizing your spring boot app requires some pre-requisite is

  • Installation of Docker Desktop on your machine.
  • Installation of GIT Client or IDE-based git client.

Process Flow

There are multiple process by which we can dockerize our java application (Spring boot app in this case). Here we will discuss the basic process of doing so and in subsequent articles we will discuss more enterprise freindly approach and its advantages.

Sample Demo Repository

if you have local setup for a java application you can use below steps to dockerize you app or Else you can use this below sample repository for trial.

Saurabh-85/docker-demo at master (github.com)

To generate jar file under /target for any project we simply need to run mvn install.

Creating a Docker File

Dockerfile is a text file which contains all commands required to create a image for given java project.
Each line in a docker file will have Instructions and arguments-

  • In the above project docker file, we are starting with a FROM base image of openjdk:19 at line 2 for packing our javat application as a new jar.
  • At line 4 EXPOSE instruction informs docker that container listens at given port at runtime.
  • At line 7 we ADD the jar from the target folder to the docker image.
  • At line 8 we are defining the ENTRYPOINT which will allow our image to run as a executable on a container.

Building an Image

Now lets build a docker image from the docker file we just defined with below steps –

  • open terminal(command prompt)
  • cd to <local root directory with dockerfile>
  • Use CLI command to build image and push it to public registry of docker hub.
    docker build -t docker-demo.app.jar
  • docker client reads all commands from dockerfile and execute them sequentially

Running an image inside a Container

Accessing you app locally

Acessing you app on container


Comments

2 responses to “Dockerizing your Spring Boot App”

  1. […] in Blog Next Post Prev […]

  2. […] in Blog Prev Post Next […]

Leave a Reply to Docker CLI commands The Devops Guy Cancel reply

Your email address will not be published. Required fields are marked *