How to Dockerize a Java Application
Tags: beginners, programming, devops, java
Published: 2023-02-27 19:10:30

Docker makes it easy to build, manage, and deploy containerized applications. Using Docker during a development process helps to deliver a consistent environment across all machines. While working with a team, it is recommended to use Docker for your applications. This helps the dev cycle not to lag.

In this article, we are going to learn how to dockerize a Java application. However, the basic concepts apply to any technology you are using for your application.


## Prerequisite

- A conceptualized knowledge of [Docker](https://dev.to/shorla/how-to-dockerize-a-java-application-4hd1)

- An IDE 

- A Terminal — Linux or IDE Terminal

- A source code

## Steps to dockerizing a Java application

#### Creating a Java application

Let us create a simple “hello world” application in Java. We can do this by opening any IDE of our choice and creating a file named _helloWorld.java_. Then, we will copy and paste the code below into the file.

`public class helloWorld{
   public static void main(String[] args) {
     System.out.println("Hello world!");
   }
}`

On the terminal, we will compile the code by running:

`javac HelloWorld.java`

Compiling the code creates a new file — “HelloWorld.class”. The “.class” file runs on the JVM to execute the application. To execute the application, we run: 

`java helloWorld`

The terminal should print out the result _Hello world!_

### Dockerizing the application

We will take the application we built and run it in a Docker container. To do that, it is assumed we have Docker installed on our computer. If not, we can do that [here](https://docs.docker.com/get-docker/).

Once Docker is installed and started, the next step is to build a docker image for our application. We need a Dockerfile to build a docker image. To create the Dockerfile, we start by creating a new file and naming it _Dockerfile_. This file will include instructions on how to build the image for our application. The order of the command is important as each command will be executed consecutively.

We can copy and paste the following code to our Dockerfile. To learn more about keywords in Dockerfile, visit [here](https://dev.to/shorla/how-to-dockerize-a-java-application-4hd1).

`# use the openjdk as the base image
FROM openjdk
 # create a new app directory
RUN mkdir /app
 # copy the app files from the host machine to the image filesystem
ADD java /app
 # set the directory for executing future files
WORKDIR /app
 # run the command for executing the application
CMD java HelloWorld
`

The “#” is used to write comments in Dockerfile. We can now build the image by running:

`docker build -t  hello-world:1.0 .`

The “-t” option helps to tag the image with a name for easy access. In the code above, the name or tag assigned to the image is “hello-world:1.0”. The “.” at the end of the code is to specify that the Dockerfile is in the current directory.

if the error message below appears after running the code above, put _sudo_ before running each code.

`Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock:`

Alternatively, we can add user to the Docker group. Instructions on how to do that can be found [here](https://docs.docker.com/engine/install/linux-postinstall/).

The next step is to instantiate our image and run our container. This can be done by running:

`docker run hello-world:1.0`

The result 'Hello world!' should be printed on the terminal.

To see the list of images available, we can run:

`docker image`

To see the list of Docker containers currently running, we can run:

`docker ps`

Note: The container we just prompted will not be on the list. This is because it will be terminated after running.

## Conclusion

Following the steps listed above, dockerizing a Java application should be clearer. It is advised to try the steps out on other applications to become more familiar with them. 

I recommend reading this [article](https://spacelift.io/blog/docker-tutorial) for more information on docker.