Deploy a Jar Application into Docker Container

2021-06-11

Docker is a tool designed to make it easier to create, deploy, and run applications by using containers.

This blog is about running java instance in a docker container and deploying .jar in the container. First, download the Docker in your local machine from the Docker page. We will focus on deploying your .jar in docker container only… we can find the docker installation process on the Docker page. So if you are here… it means you have successfully installed Docker in your local machine. So let’s get started… Open your terminal and start typing commands…. Let’s clone a java image in your local machine from Docker Hub.

docker image pull java:8
docker image ls # it will list all images in your docker

Now let’s create and start a java container from the image

docker container create --publish 260:8080 --name my-java-container java:8.0
docker container ls -a # it will list all the containers
docker container start my-java-container

Java application can be accessed in http://localhost:260 Now let’s create a docker image file for your .jar and deploy it in docker…

Create a file with name Dockerfile in root directory of your application without any extension and add following lines in it.

# we are extending everything from java:8.0 image ...
FROM java:8.0
MAINTAINER yilmazsen# 
#COPY path-to-your-application-jar path-to-webapps-in-docker-jar
COPY myproject-name_0.0.1_SNAPSHOT.jar /app/myproject_name.jar
CMD ["java", "-Djava.security.egd=file:/dev/./urandom","-jar","/app/myproject_name.jar"]

Now build a docker image file for your application from Dockerfile open a terminal in the root of your application directory

# docker build -t your_registry_name/my-app location_of_dockerfile
docker build -t atavussoft.registry.com/my-app-image .
# now if you check list of images in your docker, you will see as
# your_registry_name/some-app-image image in it...
docker image ls
# REPOSITORY  
            
TAG    IMAGE ID      CREATED      SIZE
# atavussoft.registry.com/my-app-image latest 5154ca16b1dd  1 hours ago  777MB

You can create and start your new container for your application as described above or you can simply run by using the following command.

#a container is creating and running with the docker command
docker container run -it — publish 260:8080 atavussoft.registry.com/my-app-image:latest

Your application can be accessed in http://localhost:260

Thank you for reading my article :)

🔙 Go Back