Goglides Dev 🌱

Cover image for Process to dockerize Java Springboot application and publish it to public registry Docker Hub.
Jeewan Gautam
Jeewan Gautam

Posted on • Updated on

Process to dockerize Java Springboot application and publish it to public registry Docker Hub.

What is docker?
Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Docker’s methodologies for shipping, testing, and deploying code quickly, you can significantly reduce the delay between writing code and running it in production.

First, pull the Demo-springboot-app from my github using this https://github.com/JeewanGautam/springbootapp.git url.

Let's Create docker file in the root directory to dockerize java springbootapp springboot application.

Step 1 βˆ’ Create a file called Docker File and edit it using vim. Please note that the name of the file has to be "Dockerfile" with "D" as capital.

vim Dockerfile
Enter fullscreen mode Exit fullscreen mode

Step 2 βˆ’ Build your Docker File using the following instructions.

# Simple Dockefile to build java springboot application image
FROM openjdk:latest
EXPOSE 8080:8080
MAINTAINER Jeewan Gautam
ADD /target/springbootapp-0.0.1-SNAPSHOT.jar springbootapp-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java","-jar","springbootapp-0.0.1-SNAPSHOT.jar"]
Enter fullscreen mode Exit fullscreen mode

The following points need to be noted about the above file βˆ’

The first line "#Simple Dockefile to build java springboot application image" is a comment. You can add comments to the Docker File with the help of the # command

The next line has to start with the FROM keyword. It tells docker, from which base image you want to base your image from. In our example, we are creating an image from the openjdk:latest image.

The EXPOSE instruction exposes a particular port with a specified protocol inside a Docker Container. In our example we are exposing local port 8080 to container port 8080.

The next command is the person who is going to maintain this image. Here you specify the MAINTAINER keyword and just mention Details of maintainer.

The next ADD command is used to copy files/directories into a Docker image. It Copy files from the local storage to a destination in the Docker image. In our example it will copy springbootapp-0.0.1-SNAPSHOT.jar local jar file to Dockerfile.

The next ENTRYPOINT give you a way to identify which executable should be run when a container is started from your image.

Step 3βˆ’ Save the file. As of now we have created Dockerfile. Let's build image using docker file. Run below command in the terminal.

docker build -t jeewangautam02261995/springbootapp:v1 .
Enter fullscreen mode Exit fullscreen mode

Output
Image description

The following points need to be noted about the above command βˆ’

The docker build command instructs Docker to build a Docker image from a Dockerfile.

-t or -tag describes the Name and optionally a tag in the 'name:tag' format.Here, jeewangautam02261995/springbootapp is the name we are giving to the Image and v1 is the tag number we are giving to our image.

Now, run below command to view the images we have just created.

docker images 
Enter fullscreen mode Exit fullscreen mode

Output
Image description
we can see, jeewangautam02261995/springbootapp docker image with tag v1 is successfully created.

Run below command to run jeewangautam02261995/springbootapp image in docker container.

docker run jeewangautam02261995/springbootapp:v1
Enter fullscreen mode Exit fullscreen mode

Output
Image description

Let's, Publish a docker container's port to the localhost port.

docker run -it -p 8081:8081 2f0df6cd4aa4
Enter fullscreen mode Exit fullscreen mode

Output
Image description

Image description

Let's discuss how to push docker images into public repositories. Here we will use Docker Hub.

Public repositories can be used to host Docker images which can be used by everyone else. An example is the images which are available in Docker Hub. Most of the images such as Centos, Ubuntu, and Jenkins are all publicly available for all. We can also make our images available by publishing it to the public repository on Docker Hub.

Here, we have our jeewangautam02261995/springbootapp:v1 image which was created before. Let’s use this to upload to the Docker public repository Docker Hub.

The following steps explain how you can upload an image to public repository.

Step 1 - Log into Docker Hub and create your repository. This is the repository where your image will be stored. Go to https://hub.docker.com/ and log in with your credentials.

Image description

Step 2 βˆ’ Click the button "Create Repository" on the above screen and create a repository with the name springbootapp. Make sure that the visibility of the repository is public
Image description

Step 3 βˆ’ Now go back to the Docker Host. Here we need to tag our jeewangautam02261995/springbootapp:v1 to the new repository created in Docker Hub. We can do this via the Docker tag command.

docker tag method allows one to tag an image to the relevant repository.

docker tag 2f0df6cd4aa4 jeewangautam02261995/springbootapp:v1
Enter fullscreen mode Exit fullscreen mode

Output
Image description
Options
imageID βˆ’ This is the ImageID which needs to be tagged to the repository.
Repositoryname βˆ’ This is the repository name to which the ImageID needs to be tagged to.

Step 4 βˆ’ Issue the Docker login command to login into the Docker Hub repository from the command prompt. The Docker login command will prompt you for the username and password to the Docker Hub repository.

Image description
Step 4 βˆ’ use docker push. This command allows one to push images to the Docker Hub.

docker push jeewangautam02261995/springbootapp:v1
Enter fullscreen mode Exit fullscreen mode

Output
Image description
If you go back to the Docker Hub page and go to your repository, you will see the tag name in the repository.

Image description

We have successfully dockerize java springboot application and published in Docker Hub. In next blog we will discuss how to deploy this image into kubernetes cluster.

Top comments (0)