DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Ways To Reduce JVM Docker Image Size
  • How To Create a Stub in 5 Minutes
  • Java CI/CD: From Local Build to Jenkins Continuous Integration
  • How to Create a Development Environment on Alpine Linux

Trending

  • Accelerating AI Inference With TensorRT
  • Mastering Advanced Aggregations in Spark SQL
  • Enhancing Security With ZTNA in Hybrid and Multi-Cloud Deployments
  • Scaling DevOps With NGINX Caching: Reducing Latency and Backend Load
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Run a Simple .jar Application in a Docker Container

Run a Simple .jar Application in a Docker Container

This tutorial shows you how you can run a Hello World .jar application in a Docker container from the command line, without a server.

By 
Julia Bondarchuk user avatar
Julia Bondarchuk
·
Jul. 20, 17 · Tutorial
Likes (22)
Comment
Save
Tweet
Share
264.3K Views

Join the DZone community and get the full member experience.

Join For Free


While I was studying Docker, I was challenged with running a basic “Hello World” .jar application in a container. Almost all resources and tutorials were about how to do it with Maven and run it on a server. I was interested in running it without a server, just out of the command line. So first, a little bit about Docker. Read DZone’s related tutorial covering how to publish Maven Artifacts using Jenkins.

Docker is an open platform for building, shipping, and running distributed applications. Basically, it wraps your application from your environment and contains all that is needed to run this application locally on a developer's machine. It can be deployed to production across a cloud-based infrastructure. This guarantees that the software will always run the same, regardless of its environment. That’s pretty cool to have the possibility to pass your application with all the needed set up for a running environment. Docker is a crucial part of CI/CD processes, and is something every DevOps engineer should know.

To start with, you need to install the Docker platform for your OS and have Java installed on your machine. Once you have everything installed, you are ready to start and you may go through basic Docker commands. 

The first thing you need is to create a basic.java file, HelloWorld.java, and add these lines into it:

public class HelloWorld {
  public static void main(String[] args){
    System.out.println("Hello World :) ");
  }
}


Save and compile it in the command line. From the directory in which you have created your HelloWorld.java, run the command javac HelloWorld.java.

Once you do this, you will get the HelloWorld.class file, which later we will build in .jar. But before that, we need to create a simple manifest.txt to make it packed right.

So now, in the same directory, create manifest.txt and place the following lines:

Manifest-Version: 1.0

Created-By: Me

Main-Class: HelloWorld

Then, in the command line, run the following: jar cfm HelloWorld.jar manifest.txt HelloWorld.class.

And to check if everything works correctly, type java -jar HelloWorld.jar.


If everything is okay, you should see the following:

The next step is to start Docker and create a Dockerfile, a text file that contains the instructions (or commands) used to build a Docker image. With version 1.12 you can now configure Docker health checks into the image definition.

To do that, create the file with the name "Dockerfile" and place the following text in it:

FROM java:8
WORKDIR /
ADD HelloWorld.jar HelloWorld.jar
EXPOSE 8080
CMD java - jar HelloWorld.jar

Don’t forget to leave the empty line at the end of the file.

Related: Applying CI/CD to Java App.

Now you are ready to create a Docker image, the result of building a Dockerfile and executing the Dockerfile's commands. It is constructed from a root operating system, installed applications, and commands executed in such a way that it can run your application. A Docker image serves as the basis for Docker containers and is the static template from which they are created. Related Tutorial: Setup a Java Pipeline with Azure Devops and Docker.

You need to run in command line the following: docker build -t helloworld  

As a result, you should see this:

Then you have to create an account on dockerhub and create the repository "hello-world" to push your image to your repository. Once you register and create a repository, go to command line and log in there with docker login.

Then pull that repository: docker pull /hello-world  

To push your Docker image to DockerHub you need to figure out your Docker_Image_ID. Run the following: docker images  


So you may find your image and see you Image_Id. Now you need to tag and push your image:  docker tag 4b795844c7ab /hello-world 

To read more about getting started with Docker, read our Refcard.

Now you are ready to upload your Docker Image to DockerHub.
Just type: docker push /hello-world:latest 

To check if everything works fine enter: docker run /hello-world 

You must see the output: Hello World :)

Congratulations! You deployed your java.jar to Docker.

Docker (software) Java (programming language) Continuous Integration/Deployment JAR (file format)

Opinions expressed by DZone contributors are their own.

Related

  • Ways To Reduce JVM Docker Image Size
  • How To Create a Stub in 5 Minutes
  • Java CI/CD: From Local Build to Jenkins Continuous Integration
  • How to Create a Development Environment on Alpine Linux

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!