Run a Simple .jar Application in a Docker Container

DZone 's Guide to

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.

· DevOps Zone ·
Free Resource

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.

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.

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

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

FROM java:8
ADD HelloWorld.jar HelloWorld.jar
CMD java - jar HelloWorld.jar

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

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.

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 working with it, you can go here.

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.

container, devops, docker, docker images, java

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}