Sneak view into Docker for Java devs| Part-1

Welcome to this section to learn about Docker for Java developers. In case, you are directly landing here, it’s recommended to have an idea about the docker and it’s recommended to first visit this page.

What we have seen so far is that, the docker-engine sits on top of the host machine and is responsible for spawning multiple containers. In this blog, we shall be creating a Java based project and demonstrating the “Build-Ship-Run” philosophy. So let’s take a look at what how this can be done.

Step #1 :- We have setup github-desktop application in order to push/pull the project to our personal github repository and we have created our first repository with name : “Hello-World-Java”.

Step #2 :- Let’s now push this blank repository to our personal github repository :-

Let’s also verify from the github online, whether the project did reached there or not :-

Step #3 :- Let’s create a maven based project inside this repository, using archetype value as “quickstart”:-

Step #4 :- Let’s create a simple java file inside this project. This is the file, that we shall be running through the container and observe output :-

Step #5 :- Let’s next create a “Dockerfile”, using which we shall be building a docker image for our task. Pl note here that, the image from which we are building our own customised image is : “openjdk:8”.

Step #6:- docker build This command will build the Docker image from aforementioned Dockerfile. --tag , -t calls out to have the Name and optionally a tag in the 'name:tag' format. Here, we have defined the name of our image as “aditya-first-java-docker”.

Step #7:- docker images This command will show all top level images, their repository and tags, and their size. Pl note here that, we have just built an docker-image with name “aditya-first-java-docker”.

Step #8:- docker run This command creates a container from a given image and starts the container. Pl note that “-d” option represents detached mode i.e. even after terminal is exited, still the container shall be running. Here, we shall be launching single container with name as “aditya-first-java-docker-container”.

Step #9:- docker ps -a This command shows all the containers that we have currently :-

Now, the above output indicates that, the container is running well and we can access it now. So, let’s quickly see the logs of our application.

Step #10:- docker logs -ft <container_id>This command shows the logs of the container that we had launched recently :-

Next, ideal goal that we can achieve are :-

  • Build this image.
  • Ship this Image to GitHub.
  • Git is going to automatically build this image in backend & keep it ready.

Now say, any other team wants to download this latest image, can very well use it for their own purpose too. Imagine the number of hours or man hours wasted in trying to install download one to set up the virtual machines etc. So now that we are shipping a docker-image and we can very well do the continuous integration with help of GIT.

References :-