Docker Essentials for Developers

Docker is very popular in IT world, software companies are continuously using Dockers for a production environment,

History before Dockers

Before Dockers, Developers have used virtual machines for running applications, but it has few disadvantages such as Virtual Machines are huge in size, it consume more memory for ram, take a lot of time to boot, and slow performance, to solve this problem, Dockers came into picture.

What is Dockers?

Dockers is a platform which bundle an application and all its dependencies all together and placed in a container.

In the above diagram, each application has separate containers, inside the container has the application files and all its dependencies and libraries, this confirms that each application works without any conflicts,

Using Docker a developer can build multiple containers for multiple applications, and they can place all the application dependencies and libraries inside the container, which will ensure that it will work in the testing and production environment.

What is Docker file, Image and Containers?

Dockerfile, Docker Images & Docker Containers are three important things, we need to learn about this before using Dockers.

 

In the above diagram, we can see Dockerfile, Dockerimage and Docker container

Dockerfile is a text document that contains all the dependencies and libraries details which help to build DockerImage, Docker will build a Docker image by reading the Dockerfile.

DockerImage for example, we can say Docker image is a template, which is used to create a Docker container.

DockerContainer

Docker Container is a running instance of a Docker Image, Containers are fast and boots quickly as it uses host operating system and shares the relevant libraries, Containers has separate application dependencies and libraries which the application needed to run, Docker container will work in developer, tester and production environment without any errors.

Why we use Docker Container?

I will explain by telling a story for the above question, A company needs to develop a java application, the developer will set up an environment with JDK and tomcat server installed in it, Once the application developed, it need send for testing, now the tester will again set up the JDK and tomcat environment for testing, once the testing is done, it will send to production again at production environment need to setup JDK and tomcat server.

Errors: there would be version mismatch in all three environments.

1. The application will work in the development environment but failed in the testing and production environment due to version mismatch.

Time: will take a lot of time for the dependency setup for the application.

To prevent these errors and time loss, Docker containers are used.

Java Hello World program running in Docker Container

If you have a windows pc, download and install the docker tool application from https://docs.docker.com/ website.

Create a folder name DockerJava     shape4        in any of your desired locations

Create a Dockerfile     shape5    inside that folder

To create a dockerfile go to cmd prompt go to you DockerJava folder location like below

Inside that folder need to create Dockerfile for that run the bellow command in windows

  • Now need to create a java file

  • In the same folder create a Home.java file

  • And copy the below java code inside the file

public class Home {
    public static void main(String[] args) {
        System.out.println(“Hello World”);
    }
}

Now need to create docker image by using Dockerfile

To create docker image copy the bellow content inside Dockerfile

#Download Linux Image

FROM alpine

WORKDIR /root/JavaDocker

COPY Home.java /root/ JavaDocker

# Install java jdk in alpine os

RUN apk add openjdk8

# Setting Up Environment Variable

ENV JAVA_HOME /usr/lib/jvm/java-1.8-openjdk

ENV PATH $PATH:$JAVA_HOME/bin

# Compile the java program

RUN javac Home.java

ENTRYPOINT java Home

The above docker file commands describe below

Step 1: installing Linux image name (alpine)

Creating  JavaDocker directory inside the root directory

Copy Home.java file inside JavaDocker folder

Step 2: installing java 8 jdk

Then setting up environment path

Step 3: Compiling and Running Home.java

Now need to create a docker image, to create an image run the below command

If you want to give name to your docker image the use below command

Now all the necessary dependency will be installed in the image

Now the image is successfully built

Now the last step we need to create and run the container

Run the below command to create and run the container

To see image list

 

The Output we will get