Skip to main content

Docker Image build and Run

Docker image from a Spring boot application :




Docker is a  containerization platform that allows  to package  applications and dependencies into lightweight, portable containers. Containers are a lightweight and efficient way to package and deploy applications, as they provide a consistent and isolated runtime environment that can be easily moved between different computing environments.

Benefits of Using Docker :

  •  Easy build, ship, and run their applications on any platform, from local machine to the cloud.
  •  Docker containers are built from images, which are essentially snapshots of a specific   configuration of an application and its dependencies. These images can be shared and reused   across different teams and environments, allowing for greater collaboration and flexibility.
  •  Streamline development with Continuous Integration and Continuous Delivery (CI/CD) workflows.


Create Dockerfile :

to create an image of a spring boot application we need to create a file name called Dockerfile and this file should be present in project  directory.





FROM openjdk:8
EXPOSE 8080
ADD target/news-search-service.jar news-search-service.jar
ENTRYPOINT ["java", "-jar", "/news-search-service.jar"]

FROM: that specifies an image name which can be found on Docker Hub registry. The image name openjdk:8 is an official image created by Docker

EXPOSE: specifies the network port(s) that the container listens on at runtime.

COPY or ADD: copies files from the host system into the image, typically application code and configuration files.

CMD or ENTRYPOINT - specifies the command or script that runs when the container starts up. CMD is typically used to specify the default command, while ENTRYPOINT is used to provide a fixed command that can be overridden with additional arguments.


Docker Commands : 

To Build the image:

1. docker build -t news-search-service .

I am providing the image name as per my wish news-search-service

You should run the above command in the same folder where you have created the docker file

You should have the convention in file naming

The best is : Dockerfile

Once the build is successful, run the run command to execute the build image 



To get the list of images created:

run the command docker images - it will list you all the images created with other information like Repository name ,Tag, Image Id and Creation Time



To start the application:

docker run -p 8080:8080 news-search-service 

here, the 8080 is the exposed port that can be accessed from outside


To stop the container : 

docker stop 52be73a799be

52be73a799be : Its the container ID/Image ID

To stop or remove the image : 

docker rmi 52be73a799be

if using the above command the image does not get removed than use the below command

docker rmi -f 52be73a799be


Share Docker Image on Docker Hub

You can share your Docker image on Docker Hub (a public registry managed by Docker) so everyone can get the same container that works in the same way.

Sign in to https://hub.docker.com and click Create Repository button to create a new repository (choose the visibility is public). For example, I created the repository kdheeraj/news-search-service.

Then in the command line, type the following command to update tag name of the image according to the repository name:

docker tag news-search-service kdheeraj1512/news-search-service

Then type the following command to log into Docker Hub in command line:

docker login -u your_user_name

You need to provide password. Then type this command to share the image:

docker push kdheeraj1512/news-search-service

Done. Wait a while for the image being uploaded to Docker registry. Then you can switch to your Docker Hub account to verify the image pushed successfully.



Comments