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
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

Enterprise AI Trend Report: Gain insights on ethical AI, MLOps, generative AI, large language models, and much more.

2024 Cloud survey: Share your insights on microservices, containers, K8s, CI/CD, and DevOps (+ enter a $750 raffle!) for our Trend Reports.

PostgreSQL: Learn about the open-source RDBMS' advanced capabilities, core components, common commands and functions, and general DBA tasks.

AI Automation Essentials. Check out the latest Refcard on all things AI automation, including model training, data security, and more.

Related

  • Docker Image Building Best Practices
  • An Introduction to BentoML: A Unified AI Application Framework
  • How To Use the Node Docker Official Image
  • Building a Flask Web Application With Docker: A Step-by-Step Guide

Trending

  • The Power of Generative AI: How It Is Revolutionizing Business Process Automation
  • Deploying Heroku Apps To Staging and Production Environments With GitLab CI/CD
  • The Data Streaming Landscape 2024
  • 10 Tips To Improve Python Coding Skills in 2024
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  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
Like (22)
Save
Tweet
Share
255.9K 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) application

Opinions expressed by DZone contributors are their own.

Related

  • Docker Image Building Best Practices
  • An Introduction to BentoML: A Unified AI Application Framework
  • How To Use the Node Docker Official Image
  • Building a Flask Web Application With Docker: A Step-by-Step Guide

Partner Resources


Comments

ABOUT US

  • About DZone
  • Send 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: