Pages

Friday, 5 August 2022

How to run Java Application as Docker Image? Deploy java application in docker?

 



Most Used Docker Commands:Interview Question

Pre-Requisite: 



Package and run Java application as Docker image

Docker builds images by reading instructions from a Dockerfile. A Dockerfile is a simple text file that contains instructions that can be executed on the command line.

What is Dockerfile?

A Dockerfile is a fundamental building block used when dockerizing your Java applications, and it is how you can create a Docker image that can be used to create the containers you need for automatic builds.


Create a new Dockerfile in helloworld directory and use the following content:

FROM openjdk:latest

COPY target/helloworld-1.0-SNAPSHOT.jar /usr/src/helloworld-1.0-SNAPSHOT.jar

CMD java -cp /usr/src/helloworld-1.0-SNAPSHOT.jar org.examples.java.App
TestNG Interview questions and answers

Build the image:

docker image build -t hello-java:latest .

Run the image:

docker container run hello-java:latest

This displays the output:

Hello World!

This shows the exactly same output that was printed when the Java class was invoked using Java CLI.

Package and run Java Application using Docker Maven Plugin

Docker Maven Plugin allows you to manage Docker images and containers using Maven. It comes with predefined goals:

GoalDescription

docker:build

Build images

docker:start

Create and start containers

docker:stop

Stop and destroy containers

docker:push

Push images to a registry

docker:remove

Remove images from local docker host

docker:logs

Show container logs

Complete set of goals are listed at https://github.com/fabric8io/docker-maven-plugin.

Create the Docker image:

mvn -f docker-java-sample/pom.xml package -Pdocker

This will show an output like:

[INFO] Copying files to /Users/argu/workspaces/docker-java-sample/target/docker/hellojava/build/maven
[INFO] Building tar: /Users/argu/workspaces/docker-java-sample/target/docker/hellojava/tmp/docker-build.tar
[INFO] DOCKER> [hellojava:latest]: Created docker-build.tar in 87 milliseconds
[INFO] DOCKER> [hellojava:latest]: Built image sha256:6f815
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

The list of images can be checked using the command docker image ls | grep hello-java:

hello-java                            latest              ea64a9f5011e        5 seconds ago       643 MB

Run the Docker container:

mvn -f docker-java-sample/pom.xml install -Pdocker

This will show an output like:

[INFO] DOCKER> [hellojava:latest]: Start container 30a08791eedb
30a087> Hello World!
[INFO] DOCKER> [hellojava:latest]: Waited on log out 'Hello World!' 510 ms

This is similar output when running the Java application using java CLI or the Docker container using docker container run command.

The container is running in the foreground. Use Ctrl + C to interrupt the container and return back to terminal.

**************************

Java Coding Interview Questions and Answers

**************************

Only one change was required in the project to enable Docker packaging and running. A Maven profile is added in pom.xml:

<profiles>
    <profile>
        <id>docker</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>io.fabric8</groupId>
                    <artifactId>docker-maven-plugin</artifactId>
                    <version>0.22.1</version>
                    <configuration>
                        <images>
                            <image>
                                <name>hello-java</name>
                                <build>
                                    <from>openjdk:latest</from>
                                    <assembly>
                                        <descriptorRef>artifact</descriptorRef>
                                    </assembly>
                                    <cmd>java -cp maven/${project.name}-${project.version}.jar org.examples.java.App</cmd>
                                </build>
                                <run>
                                    <wait>
                                        <log>Hello World!</log>
                                    </wait>
                                </run>
                            </image>
                        </images>
                    </configuration>
                    <executions>
                        <execution>
                            <id>docker:build</id>
                            <phase>package</phase>
                            <goals>
                                <goal>build</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>docker:start</id>
                            <phase>install</phase>
                            <goals>
                                <goal>start</goal>
                                <goal>logs</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>
How To Dockerize PostgreSQL ?

Special credit to arun gupta*

Learn (API-Microservice)Testing+(CoreJava+UI)-SDET with Self Paced Videos and one LIVE Doubt Session

TRANING VIDEOS AVAILABLE with Live Doubt Session @4500/-(course-1 below,API TRaining Videos With ClassNotes and Coding Set) and 6500/- (API+UI, both course-1 & 2 below) Check Training Page for Course Content or reach out @whatsapp +91-9619094122
Entire course content can be found below:  COURSE CONTENT


  • Interview questions for SDET/Automation Testing:
  • Top 21 GIT interview questions
  • Top 30 Selenium Webdriver Interview Q&A 
  • Top 20 API Testing Interview Q&ASelenium Interview Question SET-4 
  • Selenium Interview Question SET-1
  • Selenium Interview Question SET-2
  • Selenium Interview Questions SET-3
  • Selenium Interview Question SET-4

      
  •        Check GIT commands for Interview preparation SET-1:

    GIT Interview Question 1 to 11

    Check GIT commands for Interview preparation SET-2:

    GIT Interview Question 12 to 21

    Check GIT commands for Interview preparation SET-3:

    GIT Interview Question 21 to 30

    Check below link for question and answers with code:

    Top API Interview Question 1-10

    *************************************************

    SeleniumWebdriver Automation Testing Interview Questions:

    API Testing Interview Question Set:
    https://automationreinvented.blogspot.com/search/label/Rest-API

     
    Kubernetes Interview Question Set
    https://automationreinvented.blogspot.com/search/label/Kubernetes

     
    Docker Interview Question Set
    https://automationreinvented.blogspot.com/2020/02/top-18-docker-commands-for-aytomation.html

     
    Linux Interview question Set
    https://automationreinvented.blogspot.com/search/label/Linux

     
    Automation Testing/SDET Framework Design
    https://automationreinvented.blogspot.com/search/label/FrameworkDesign


    Java Related Interview Question Set

    https://automationreinvented.blogspot.com/search/label/Java


    GIT Interview Question Set:
    https://automationreinvented.blogspot.com/search/label/GIT


    Coding Interview Question Set:
    https://automationreinvented.blogspot.com/search/label/Coding%20Questions
    Mobile Testing Interview Question Set:
    https://automationreinvented.blogspot.com/search/label/Mobile%20Testing

    Python Interview Question Set for QAE - SDET - SDE:
    https://automationreinvented.blogspot.com/search/label/Python

    No comments:

    Post a Comment