Monday, 11 July 2022

How to Create your first Docker image using Java?

 


Check post for MAVEN commands and Interview Question:
MAVEN Commands

Docker Commands:Interview Question



Though we all must know about Docker, but let’s revise a brief introduction about Docker:

Docker Introduction:

- Docker is a set of platform as a service products that use OS-level virtualization to deliver software in packages called containers. 
- The service has both free and premium tiers. 
- The software that hosts the containers is called Docker Engine. 

Docker Image

Docker image is a file used to execute code in a Docker container. 

Docker images act as a set of instructions to build a Docker container, like a template. 

Docker images also act as the starting point when using Docker. An image is comparable to a snapshot in virtual machine (VM) environments.

Create your first image using Java

Create a simple Java application

Note

If you are running OpenJDK 9, mvn package may fail with

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project helloworld: Compilation failure: Compilation failure:
[ERROR] Source option 1.5 is no longer supported. Use 1.6 or later.
[ERROR] Target option 1.5 is no longer supported. Use 1.6 or later.

because support for Java 5 was dropped in JDK9.

You can add

  <properties>
    <maven.compiler.source>1.6</maven.compiler.source>
    <maven.compiler.target>1.6</maven.compiler.target>
  </properties>

to the generated pom.xml to target 1.6 instead. See also the Build a Docker Image for Java 9 chapter.

Create a new Java project:

mvn archetype:generate -DgroupId=org.examples.java -DartifactId=helloworld -DinteractiveMode=false

Build the project:

cd helloworld
mvn package

Run the Java class:

java -cp target/helloworld-1.0-SNAPSHOT.jar org.examples.java.App

This shows the output:

Hello World!

Let’s package this application as a Docker image.

Java Docker image


Run the OpenJDK container in an interactive manner:

docker container run -it openjdk

This will open a terminal in the container. Check the version of Java:

root@8d0af9da5258:/# java -version
openjdk version "1.8.0_141"
OpenJDK Runtime Environment (build 1.8.0_141-8u141-b15-1~deb9u1-b15)
OpenJDK 64-Bit Server VM (build 25.141-b15, mixed mode)

A different JDK version may be shown in your case.

Exit out of the container by typing exit in the container shell.


DevOps Related Posts

How to create DOCKER IMAGE from SCRATCH? Click above link for steps


Docker image repositories


Docker images get stored in private or public repositories, such as those in the Docker Hub cloud registry service, from which users can deploy containers and test and share images. Docker Hub's Docker Trusted Registry also provides image management and access control capabilities.

Check below link for question and answers with code:

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

SDET TRANING VIDEOS AVAILABLE with Live Doubt Session @4500/-(course-1,API TRaining Videos With ClassNotes and 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

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

All Time Popular Posts

Most Featured Post

Introduction to Web Testing with Playwright

  Introduction to Web Testing with Playwright 📌  Telegram Group:  https://t.me/+FTf_NPb--GQ2ODhl 📌  YouTube channel:  https://lnkd.in/gGUG...