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

Popular posts from this blog

How to create Annotation in Spring boot

 To create Custom Annotation in JAVA, @interface keyword is used. The annotation contains :  1. Retention :  @Retention ( RetentionPolicy . RUNTIME ) It specifies that annotation should be available at runtime. 2. Target :  @Target ({ ElementType . METHOD }) It specifies that the annotation can only be applied to method. The target cane be modified to:   @Target ({ ElementType . TYPE }) for class level annotation @Target ({ ElementType . FIELD }) for field level annotation @Retention ( RetentionPolicy . RUNTIME ) @Target ({ ElementType . FIELD }) public @ interface CustomAnnotation { String value () default "default value" ; } value attribute is defined with @ CustomAnnotation annotation. If you want to use the attribute in annotation. A single attribute value. Example : public class Books {           @CustomAnnotation(value = "myBook")     public void updateBookDetail() {         ...

Kafka And Zookeeper SetUp

 Kafka And Zookeeper SetUp zookeeper download Link : https://www.apache.org/dyn/closer.lua/zookeeper/zookeeper-3.8.3/apache-zookeeper-3.8.3-bin.tar.gz Configuration: zoo.conf # The number of milliseconds of each tick tickTime =2000 # The number of ticks that the initial # synchronization phase can take initLimit =10 # The number of ticks that can pass between # sending a request and getting an acknowledgement syncLimit =5 # the directory where the snapshot is stored. # do not use /tmp for storage, /tmp here is just # example sakes. dataDir =/tmp/zookeeper # the port at which the clients will connect clientPort =2181 4 char whitelist in command arguments 4lw.commands.whitelist =* Start ZooKeeper Server $ bin/zkServer.sh start Check zookeeper status dheeraj.kumar@Dheeraj-Kumar bin % echo stat | nc localhost 2181 stat is 4 character whitelisted argument  Check Kafka running status : echo dump | nc localhost 2181 | grep broker Responsibility of Leader in Zookeeper: 1. Distrib...

Auto retries in REST API clients On Java On Ease

  Writing REST clients to consume API endpoints has become commonplace. While consuming REST endpoints, we sometimes end up in a situation where a downstream service throws some kind of transient error that goes away when the API call is retried. In such situations, we ask ourselves — “What if my API client was smart enough that knew how to retry a failed call?” Some of us go the extra mile to implement our own custom code that can retry API calls on error. But mind you, it is not only about knowing how to retry. The client also has to know when to retry and when not to. If the error is irrecoverable such as 400 — Bad Request, there is no point in retrying. It might also have to know how to back off and how to recover from the error. Implementing all this by hand, and then repeating it over and over again in every API client is cumbersome. It also adds a lot of boilerplate code and makes things even worse. But if you are a Spring/Spring Boot developer, you will be surprised to know...