Dockerizing angular application

In this post , we will dockerize a simple application designed in angular 7.

Tools used –

  • Docker Toolbox
  • Visual Code Studio
  • npm

Steps required to dockerize the simple angular application are as below -Go to the folder where you want to create angular project

Step 1: Go to the folder where you want to create angular project

Step 2: You need to first install NodeJS and then angular CLI using below command –

npm install -g @angular/cli

Step 3: Once you install angular CLI package from node package manager, you will be able to execute angular commands on console. With below commands you can create new angular project and build it.

  • Create new angular project –

ng new angular-docker-demo

  • Switch to newly created folder by

cd angular-docker-demo

  • You can run the angular project using belwo command –

ng serve

Running application can be access from – http://localhost:80/

Step 4: Now in this step , we will move towards dockerizing the application created in above steps. For that, you need to create docker file as below –

FROM nginx:1.13.3-alpine

## Remove default code for nginx website
RUN rm -rf /usr/share/nginx/html/*

## Copy all build artifacts in dist folder generated after building project into default nginx public folder
COPY /dist /usr/share/nginx/html

#Expose port 80 of container
EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]

First line in docker file i.e. FROM nginx:1.13.3-alpine will download nginx server from docker hub. NGINX (Pronounced as Engine-X) is an open source, lightweight, high-performance web server or proxy server. We would need this server to host our application.

Second line will remove default NGINX website code present under /usr/share/nginx/html/ folder.

After we build our angular project ,build artifacts will be generated under /dist folder . Third line will copy contents of /dist under
/usr/share/nginx/html/ folder and will serve this web content on browser.

Step 5: Below commands will first build the angular application which is PROD ready. Then build the docker image and run that image.

  • ng build --prod
  • docker build -t angular-docker-demo.
  • docker run -p 8082:80 --name angular-docker-demo angular-docker-demo

Connecting to MYSQL docker container

Creating a MYSQL server container in Docker

Step 1 : Pull MySql image from docker hub. Following command will pull the MYSQL version 5.6 image.

docker pull mysql:5.6

Step 2: Run a container from this image. ‘-name’ gives a name to the container. ‘ -e’ specifies run time variables you need to set. Set the runtime variables for the MySQL container-

MYSQL_ROOT_PASSWORD=root

MYSQL_DATABASE=testdb

MYSQL_USER=test

MYSQL_PASSWORD=test

These options are self explanatory. ‘-d’ tells the docker to run the container in background.

docker run --name=mysql-dev-server -e MYSQL_ROOT_PASSWORD=root -e MYSQL_USER=test -e MYSQL_PASSWORD=test -e MYSQL_DATABASE=testdb -d mysql

This will output a container id as shown below, which means that the container is started in the background properly.

Step 3: Then check the status of the container by issuing, ‘docker ps -a’ command

docker ps -a

Now you should be able to see that MySQL is running on port 3306.

Step 4: Find the IP of the container using following. Check out the “IPAddress” from the output, this will tell you the ip address.

docker inspect mysql-dev-server

Now you should be able to connect to MySQL using this ip address on port 3306.

Step 5: You can check the logs of the running container use the following command

docker logs mysql-dev-server

Isn’t it easy? You have created a MYSQL database server with just couple of commands.

 

Accessing MYSQL database through WEB Interface (phpMyAdmin)

Step 1: In order to link the MYSQL container to phpMyAdmin , you need to fire below docker command –

docker pull phpmyadmin/phpmyadmin

docker run --name myphpadmin -d --link mysql-dev-server:db -p 8080:80 phpmyadmin/phpmyadmin

First command will pull the phpmyadmin image and second will create the phpmyadmin container named ‘myphpadmin’ and link it to ‘mysql-dev-server’ container which we created earlier –

Step 2: Access the MYSQL database from phpMyAdmin

Now you can connect to this MYSQL database docker container from phpmyadmin . This will show up below page once you hit the URL (http://192.168.99.100:8080)

Note : 192.168.99.100 is the default IP of docker machine . This can be seen on the docker Toolbox console once it loaded. In case of windows please refer below screenshot to get the docker machine’s default IP address –

Once you hit the URL http://192.168.99.100:8080 , you can see phpmyAdmin login screen . You need to enter database details like username and password in order and click go –

Now you can access the testdb database which you have created using MYSQL docker image.

Dockerizing a Spring boot Application with MySQL Database

Dockerizing a Spring boot Data JPA Application with MySQL Database

Here we are going to deploy Spring boot with MYSQL application on AWS EC2 (Ubuntu) instance. So before to that we need to adhere to pre-requisites of installing Java , Git and maven

We will have two docker containers ,

      Container 1: One for hosting MYSQL database i.e. Database server .

      Container 2: One for hosting Spring boot application i.e. Web/Application server . Spring boot application has embedded tomcat                                          server  for our deployment.

Before going to dokerize our web application , we need to first create a MYSQL database server. So you need to visit below link in order to Create MYSQL database docker container.

Once the database server is up and running , we will create separate container for our spring boot web application following below steps –

 

Step 1:  Download the application to be Dockerized

Create folder docker under /usr/docker

sudo mkdir docker

Then cd into it –

cd /usr/docker

Then download application source code from repository to local system. Here in our case we have git repository.

sudo git clone https://github.com/AnupBhagwat7/TodoListManagerJPA_Docker.git

This is a spring boot application connecting MYSQL database through data JPA .

Step 2: Change the application.properties file

Change the application.properties file to point to the MYSQL database server which we created earlier .

spring.mvc.view.prefix: /WEB-INF/views/
spring.mvc.view.suffix: .jsp
server.port=8081

## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.url = jdbc:mysql://mysql-dev-server:3306/testdb
spring.datasource.username = test
spring.datasource.password = test

## Hibernate Properties

# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update

server.error.whitelabel.enabled=false

Step 3: Maven clean and package the application 

 cd TodoListManagerJPA_Docker/

mvn clean package 

Step 4:  Define a docker image with Dockerfile

touch Dockerfile

Dockerfile is where we define the docker image and specify all the configurations required to run the app. Following is the Dockerfile for our spring boot application –

# Start with a base image containing Java runtime
FROM openjdk:8-jdk-alpine

# Add Maintainer Info
LABEL maintainer="[email protected]"

# Add a volume pointing to /tmp
VOLUME /tmp

# Make port 8081 available to the world outside this container
EXPOSE 8081

# The application's jar file
ARG JAR_FILE=target/TodoListManagerJPA_Docker.jar

# Add the application's jar to the container
ADD ${JAR_FILE} TodoListManagerJPA_Docker.jar

# Run the jar file
ENTRYPOINT ["java","-jar","/TodoListManagerJPA_Docker.jar"]

FROM openjdk:8-jdk-alpine – A docker image can use another image available in the docker registry as its base or parent image. In the above example, we use the openjdk:8-jdk-alpine image as our base image.

LABEL maintainer – The LABEL instruction is used to add metadata to the image. In the above Dockerfile, we have added some info about the maintainer of the image through LABEL instruction.

VOLUME: Volumes are a mechanism to persist data generated by the container on the Host OS, and share directories from the Host OS with the container.

You can mount a VOLUME with path /var/log in the Dockerfile, and then specify the directory on the Host OS to which this mount point will be mapped to while running the container. After that, you’ll be able to access the logs from the mapped directory on the Host OS.

In the above Dockerfile, we created a mount point with path /tmp because this is where the spring boot application creates working directories for Tomcat by default. Although it’s not required for this spring boot application because who cares about tomcat directories. But if you want to store stuff like tomcat access logs, then VOLUMES are very useful.

EXPOSE: As the name suggests, this instruction allows you to expose a certain port to the outside world. You can skip this step as we are going to user -p option while running the image for mapping the docker container port with host machine’s port.

ARG: The ARG instruction defines a variable with a default value.

ADD: The ADD instruction is used to copy new files and directories to the docker image.

ENTRYPOINT: This is where you configure how the application is executed inside the container.

 

Step 5: Build the Docker image

Now that we have defined the Dockerfile, let’s build a docker image for our application. Type the following command from the root directory of the project to build the docker image .

   docker build -t spring-boot-mysql .     

spring-boot-mysql is name of the docker image and . is to define path which contains Dockerfile to be referred to build image from.

 

Step 4: Link and Run the docker image

Once you have a docker image, you can run it using docker run command like so –

          docker run --name spring-boot-mysql -d --link mysql-dev-server:db -p 8081:8081 spring-boot-mysql        

Output:

In the run command, we have specified that the port 8081 on the container should be mapped to the port 8081 on the Host OS(Local Machine).

Once the application is started, you should be able to access it at http://192.168.99.100:8081/

192.168.99.100 – I have used Windows 10 docker toolbox for this demo . This is the default IP of the Docker machine which can be seen after docker toolbox is loaded as below –

-it option – interactive mode .

-p option- mapping containers port to host machine’s port .

mysql-dev-server – Its the MYSQL database container .

spring-boot-mysql – Its Spring boot application container.

–link – To link these two containers together

-d option –  To run the container in the background or Detached mode.

 

Step 5: Check the output of above Application in browser

http://192.168.99.100:8081/

 

Step 6: Push the docker image to docker hub

We can push our image to docker hub so that other people can use it . Below steps are required to push the image to docker hub –

  • Login with your docker ID –

[email protected]:~$ docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: anupbhagwat7
Password:
Login Succeeded

  • Tag the image –

docker tag image username/repository:tag

We will tag our current image –

docker tag spring-boot-mysql anupbhagwat7/spring-boot-mysql:0.0.1-SNAPSHOT

Replace my name(anupbhagwat7) with your respective docker ID. Once the image is tagged , you need to push it to docker hub.

  • Push the image to docker hub

docker push anupbhagwat7/spring-boot-mysql:0.0.1-SNAPSHOT

That’s it, You can now access this image on docker hub through below link –

https://hub.docker.com/r/anupbhagwat7/spring-boot-mysql/

 

Step 7: Pull the docker image from docker hub and run it

After we publish the image to docker hub, anyone can pull this image and run it in their environment. Type the following command to pull and run the image on your machine that we just pushed to docker hub –

docker run -it -p 8080:8080 anupbhagwat7/spring-boot-mysql:0.0.1-SNAPSHOT

The docker run command pulls the image from docker hub if it is not available locally, and then runs it.

OR

You can pull the image and run it with two different commands –

docker pull anupbhagwat7/spring-boot-mysql

docker run -it -p 8081:8081 anupbhagwat7/spring-boot-mysql:0.0.1-SNAPSHOT

You see how easy it is to share your image with others. People don’t need to install anything whatsoever to run your application. They just need to pull the image and run it with docker.

 

Using dockerfile-maven-plugin to Automate the Docker image creation and publishing

We can automate everything from building the docker image to publishing it on docker hub using dockerfile-maven-plugin.

Add below plugin code to you projects pom.xml file –

<!-- This plugin is used to create a docker image and publish the image to docker hub-->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<!-- replace `anupbhagwat7` with your docker id-->
<repository>anupbhagwat7/spring-boot-websocket-chat-demo</repository>
<tag>${project.version}</tag>
<buildArgs>
<JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
<executions>
<execution>
<id>default</id>
<phase>install</phase>
<goals>
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
</plugin>

The plugin’s configuration includes the remote repository name and the repository tag. Please replace the username anupbhagwat7 with your docker Id in the <repository> element.

We’re also passing the JAR_FILE argument inside <buildArgs> element. Remember we had added ARG JAR_FILE instruction to the Dockerfile? The argument passed here will override that value.

Here is how you can build the docker image using docker-file-maven plugin –

mvn package dockerfile:build

This command will package the application into jar file and then creates docker image.

Finally, you can push the docker image to the docker registry using dockerfile:push command –

mvn dockerfile:push

Now to automate it further, we need register the dockerfile:build and dockerfile:push goals to the install phase of maven build life cycle using the <executions> tag.

So whenever you run mvn install, the build and push goals of dockerfile-maven-plugin are executed, and your docker image is built and pushed to docker hub.

The dockerfile-maven-plugin uses the authentication information stored in any configuration files ~/.dockercfg or ~/.docker/config.json to push the docker image to your docker profile. These configuration files are created when you login to docker via docker login.

You can also specify authentication details in maven settings.xml or pom.xml files.

Dockerizing Spring boot Rest Webservice Application

Dockerizing a Spring boot Rest Webservice Application

Here we are going to deploy Spring boot application on AWS EC2 (Ubuntu) instance. So before to that we need to adhere to pre-requisites of installing Java , Git and maven

Step 1:  Download the application to be Dockerized

Create folder docker under /usr/docker

sudo mkdir docker

Then cd into it –

cd /usr/docker

Then download application source code from repository to local system. Here in our case we have git repository.

sudo git clone https://github.com/AnupBhagwat7/SpringBootRestful.git

This is a simple spring boot Restful Webservice application project.

Step 2: Maven clean and package the application 

 cd SpringBootRestful/

mvn clean package 

Step 3:  Define a docker image with Dockerfile

touch Dockerfile

Dockerfile is where we define the docker image and specify all the configurations required to run the app. Following is the Dockerfile for our spring boot application –

# Start with a base image containing Java runtime
FROM openjdk:8-jdk-alpine

# Add Maintainer Info
LABEL maintainer="[email protected]"

# Add a volume pointing to /tmp
VOLUME /tmp

# Make port 8080 available to the world outside this container
EXPOSE 8080

# The application's jar file
ARG JAR_FILE=target/SpringBootRestful-0.0.1-SNAPSHOT.jar

# Add the application's jar to the container
ADD ${JAR_FILE} SpringBootRestful.jar

# Run the jar file
ENTRYPOINT ["java","-jar","/SpringBootRestful.jar"]

FROM openjdk:8-jdk-alpine – A docker image can use another image available in the docker registry as its base or parent image. In the above example, we use the openjdk:8-jdk-alpine image as our base image.

LABEL maintainer – The LABEL instruction is used to add metadata to the image. In the above Dockerfile, we have added some info about the maintainer of the image through LABEL instruction.

VOLUME: Volumes are a mechanism to persist data generated by the container on the Host OS, and share directories from the Host OS with the container.

You can mount a VOLUME with path /var/log in the Dockerfile, and then specify the directory on the Host OS to which this mount point will be mapped to while running the container. After that, you’ll be able to access the logs from the mapped directory on the Host OS.

In the above Dockerfile, we created a mount point with path /tmp because this is where the spring boot application creates working directories for Tomcat by default. Although it’s not required for this spring boot application because who cares about tomcat directories. But if you want to store stuff like tomcat access logs, then VOLUMES are very useful.

EXPOSE: As the name suggests, this instruction allows you to expose a certain port to the outside world. You can skip this step as we are going to user -p option while running the image for mapping the docker container port with host machine’s port.

ARG: The ARG instruction defines a variable with a default value.

ADD: The ADD instruction is used to copy new files and directories to the docker image.

ENTRYPOINT: This is where you configure how the application is executed inside the container.

 

Step 3: Build the Docker image

Now that we have defined the Dockerfile, let’s build a docker image for our application. Type the following command from the root directory of the project to build the docker image .

docker build -t spring-rest .

spring-rest is name of the docker image and . is to define path which contains Dockerfile to be referred to build image from.

Output:

Step 4: Run the docker image

Once you have a docker image, you can run it using docker run command like so –

docker run -it -p 8080:8080 spring-rest

Output:

In the run command, we have specified that the port 8080 on the container should be mapped to the port 8080 on the Host OS(Local Machine).

Once the application is started, you should be able to access it at http://EC2_INSTANCE_IP:8080/tasks

-it option – interactive mode .

-p option- mapping containers port to host machine’s port .

spring-rest – Its Spring boot application container.

The container runs in the foreground, and pressing CTRL + C will stop it. Let’s now see how to run the container in the background.

Running the docker image in the background, in detached mode.
You can use the -d option in docker run command to run the container in the background –

docker run -d -it -p 8080:8080 spring-rest

 

Step 5: Check response in POSTMAN tool

Below is the response from webservice –

 

Step 6: Push the docker image to docker hub

We can push our image to docker hub so that other people can use it . Below steps are required to push the image to docker hub –

  • Login with your docker ID –

[email protected]:~$ docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: anupbhagwat7
Password:
Login Succeeded

  • Tag the image –

docker tag image username/repository:tag

We will tag our current image –

docker tag spring-rest anupbhagwat7/spring-rest:0.0.1-SNAPSHOT

Replace my name(anupbhagwat7) with your respective docker ID. Once the image is tagged , you need to push it to docker hub.

  • Push the image to docker hub

docker push anupbhagwat7/spring-rest:0.0.1-SNAPSHOT

That’s it, You can now access this image on docker hub through below link –

https://hub.docker.com/r/anupbhagwat7/spring-rest/

 

Step 7: Pull the docker image from docker hub and run it

After we publish the image to docker hub, anyone can pull this image and run it in their environment. Type the following command to pull and run the image on your machine that we just pushed to docker hub –

docker run -it -p 8080:8080 anupbhagwat7/spring-rest:0.0.1-SNAPSHOT

The docker run command pulls the image from docker hub if it is not available locally, and then runs it.

OR

You can pull the image and run it with two different commands –

docker pull anupbhagwat7/spring-rest

docker run -it -p 8080:8080 anupbhagwat7/spring-rest:0.0.1-SNAPSHOT

You see how easy it is to share your image with others. People don’t need to install anything whatsoever to run your application. They just need to pull the image and run it with docker.

 

Using dockerfile-maven-plugin to Automate the Docker image creation and publishing

We can automate everything from building the docker image to publishing it on docker hub using dockerfile-maven-plugin.

Add below plugin code to you projects pom.xml file –

<!-- This plugin is used to create a docker image and publish the image to docker hub-->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<!-- replace `anupbhagwat7` with your docker id-->
<repository>anupbhagwat7/spring-boot-websocket-chat-demo</repository>
<tag>${project.version}</tag>
<buildArgs>
<JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
<executions>
<execution>
<id>default</id>
<phase>install</phase>
<goals>
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
</plugin>

The plugin’s configuration includes the remote repository name and the repository tag. Please replace the username anupbhagwat7 with your docker Id in the <repository> element.

We’re also passing the JAR_FILE argument inside <buildArgs> element. Remember we had added ARG JAR_FILE instruction to the Dockerfile? The argument passed here will override that value.

Here is how you can build the docker image using docker-file-maven plugin –

mvn package dockerfile:build

This command will package the application into jar file and then creates docker image.

Finally, you can push the docker image to the docker registry using dockerfile:push command –

mvn dockerfile:push

Now to automate it further, we need register the dockerfile:build and dockerfile:push goals to the install phase of maven build life cycle using the <executions> tag.

So whenever you run mvn install, the build and push goals of dockerfile-maven-plugin are executed, and your docker image is built and pushed to docker hub.

The dockerfile-maven-plugin uses the authentication information stored in any configuration files ~/.dockercfg or ~/.docker/config.json to push the docker image to your docker profile. These configuration files are created when you login to docker via docker login.

You can also specify authentication details in maven settings.xml or pom.xml files.

Basic Docker commands

  • docker run hello-world 

Docker looks for this image on our local system. If its not on local then,it downloads hello-world image from docker hub and runs               it .

  • docker run -it ubuntu bash

We downloaded an Ubuntu Linux image and started a login shell as root inside it. The -it flags allow us to interact with the shell.

  • docker ps -a

It lists all running containers on our system.

[email protected]:/etc/bash_completion.d$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
830992bb76cd hello-world "/hello" 2 minutes ago Exited (0) 2 minutes ago sleepy_fermi

From this, we can see that the hello-world container is still in memory. The status column tells us that it’s exited. The names column has a name, sleepy_fermi, that Docker assigned to the container for us.

Let’s run this image again with docker run hello-world. This time console output will not be same as image will not be downloaded again as its already downloaded.

 Run docker ps -a again , what it will show?

We see two stopped instances of hello-world, with two different names. Docker created an additional container. It didn’t reuse the first. When we told Docker to run an image named hello-world, it did exactly that; it ran a new instance of the image. If we want to reuse a container, we refer to it by name.

  • docker stop sleepy_fermi

This will stop the running instance in a container. We need to pass CONTAINER NAME as a parameter to this command.

  • docker rm sleepy_fermi

This will remove the container from docker . We need to pass CONTAINER NAME as a parameter to this command.

  • docker image ls

This produces a listing of images on our system. We see Ubuntu and the single hello-world image since we only needed that single image to run two containers.

  • docker image rm <<IMAGE_NAME>>

Removes the image which we have downloaded on local .

  • docker build -t <<IMAGE_NAME>> .

This will build the image for any custom application from the configuration done in Dockerfile

-t  – This option is for

<<IMAGE_NAME>> – We can assign any name as per our wish to image.

dot(.) – This is reference to the current directory which might be used for building war/jar file.

  • docker run -v /full/path/to/html/directory:/usr/share/nginx/html:ro -p 8080:80 -d nginx 

Sharing local resources with Container –

As of now , we have run independent images. But we have nginx image which can share the data on host OS with the containers.

Here we an share the data on host OS or Network resources with containers.

[email protected]:/var/test$ docker run -v /var/test/:/usr/share/nginx/html:ro -p 8080:80 -d nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
be8881be8156: Pull complete
32d9726baeef: Pull complete
87e5e6f71297: Pull complete
Digest: sha256:d85914d547a6c92faa39ce7058bd7529baacab7e0cd4255442b04577c4d1f424
Status: Downloaded newer image for nginx:latest
ee6c7a8fd3a7964c9ea6da35793a7244bf878d367d2d0930061da140380cd14d

Command:   docker run -v /full/path/to/html/directory:/usr/share/nginx/html:ro -p 8080:80 -d nginx

When we execute this command line, we see Docker download the Nginx image and then start the container.

/full/path/to/html/directory – Path on host OS where we can keep our files to be hosted.

/usr/share/nginx/html:ro – Path to container

-p 8080:80 – maps network service port 80 in the container to 8080 on our host system.

-d  – detaches the container from our command line session.

nginx – the name of the image.

Installing Java, Git and Maven on Ubuntu

  • Java 

Java and the JVM (Java’s virtual machine) are widely used and required for many kinds of software. This article will guide you through the process of installing and managing different versions of Java using apt-get.

The easiest option for installing Java is using the version packaged with Ubuntu. Specifically, this will install OpenJDK 8, the latest and recommended version.

First, update the package index.

sudo apt-get update

Next, install Java. Specifically, this command will install the Java Runtime Environment (JRE).

sudo apt-get install default-jre

There is another default Java installation called the JDK (Java Development Kit). The JDK is usually only needed if you are going to compile Java programs or if the software that will use Java specifically requires it.

The JDK does contain the JRE, so there are no disadvantages if you install the JDK instead of the JRE, except for the larger file size.

You can install the JDK with the following command:

sudo apt-get install default-jdk

Setting the JAVA_HOME Environment Variable

Many programs, such as Java servers, use the JAVA_HOME environment variable to determine the Java installation location. To set this environment variable, we will first need to find out where Java is installed. You can do this by executing the same command as in the previous section:

sudo update-alternatives --config java

Copy Copy the path from your preferred installation and then open /etc/environment using nano or your favorite text editor.

sudo nano /etc/environment

At the end of this file, add the following line, making sure to replace the highlighted path with your own copied path.

JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64/jre/"

Save and exit the file, and reload it.

source /etc/environment

You can now test whether the environment variable has been set by executing the following command:

echo $JAVA_HOME

 

  • Git 

The easiest way of getting git installed and ready to use is by using Ubuntu’s default repositories. This is the fastest method, but the version may be older than the newest version. If you need the latest release, consider following the steps to compile git from source.

You can use the apt package management tools to update your local package index. Then, you can download and install the program:

sudo apt-get update

sudo apt-get install git

This will download and install  git to your system. You will still have to complete the configuration steps as below –

git config --global user.name "<span class="highlight">Your Name</span>"

git config --global user.email "<span class="highlight">[email protected]</span>"

We can see all of the configuration items that have been set by typing:

git config --list

  • Maven

The maven package always comes with latest Apache Maven.You can download and install maven using below commands:

sudo apt-get update

sudo apt-get install maven

Run command mvn –version to verify your installation.

Installation of Docker on Windows 10 and EC2 Ubuntu Instance

1. Installation on Windows

On windows , we have two different installers for docker . If you have Microsoft windows 10 Professional Or Enterprise 64 bit then you have default docker installer and for all previous versions of windows you need to install Docker toolbox.

Docker Installation Link

2. Installation on Ubuntu (EC2 Instance)                                                                                                                                                                       

 

 

 

  • Click the Select button for a 64Bit Ubuntu image. (i.e. Ubuntu Server 14.04 LTS)

 

 

  • Click the Next: Configure Instance Details button at the bottom right.
  • On the Configure Instance Details step, expand the Advanced Details section.
  • Under User data, select As text.
  • Enter #include https://get.docker.com into the instance User Data.  It will bootstrap Docker by running the shell script located at this URL.

 

 

 

 

  • We may need to set up our Security Group to allow SSH. By default all incoming ports to our new instance will be blocked by the AWS Security Group, so we might just get timeouts when we try to connect.

 

 

 

  • Creating a new key pair:

 

 

 

 

 

 

  • After a few more standard choices where defaults are probably ok, our AWS Ubuntu instance with Docker should be running!
  • Connect to created EC2 instance using MobaXterm tool . First you have to select SSH session and then put the details like EC2 instance public IP , username , .pem file address and hit “Ok” as below –

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Install Docker on Ubuntu – 

To install the latest Ubuntu package (may not be the latest Docker release):

[email protected]:~$ sudo apt-get update

[email protected]:~$ sudo apt-get install docker.io

Then, to enable tab-completion of Docker commands in BASH, either restart BASH or:

source /etc/bash_completion.d/docker

If this command doesn’t work  and throws error like “can’t read /etc/bash_completion.d/docker: No such file or directory” then create docker file inside /etc/bash_completion.d directory.

Now check if docker is running –

[email protected]:/etc/bash_completion.d$ service docker status
● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2018-08-15 23:59:39 UTC; 32min ago

If it’s not running, we can start it with start argument:

[email protected]:/etc/bash_completion.d$ service docker start

What is docker and why docker?

What is Docker?

Docker is a platform for packaging, deploying and running applications.

Docker applications run in containers that can be used on any system: a developer’s laptop, desktop, VM, Cloud etc.

It packages applications as images that contain everything needed to run them: Code, Runtime environment, Libraries and Configuration.

A running instance of a docker image is called a docker container.

Docker containers don’t run in their own virtual machines, but share a Linux kernel. Compared to virtual machines, containers use less memory and less CPU.

However, a Linux runtime is required for Docker. Implementations on non-Linux platforms such as macOS and Windows 10 use a single Linux virtual machine. The containers share this system.

 

Why Docker Containers?

  • Footprint: 100-1000 containers on one machine
  • Small Disk/CPU requirements as compared to VM’s
  • Portability
  • Supported by all Cloud providers
  • Speed:  Boots in seconds
  • Isolation and encapsulation of application dependencies.
  • Ability to scale efficiently, easily, and in real time.
  • Enhances efficiency via easy reuse of images.

Containerization is increasingly popular because containers are:

  • Flexible: Even the most complex applications can be containerized.
  • Lightweight: Containers leverage and share the host kernel.
  • Interchangeable: You can deploy updates and upgrades on-the-fly.
  • Portable: You can build locally, deploy to the cloud, and run anywhere.
  • Scalable: You can increase and automatically distribute container replicas.
  • Stackable: You can stack services vertically and on-the-fly.

Docker containers are built on top of Docker Engine. Each container operates on common Linux kernel of host machine.

  1. All needs for our App are defined in single tiny text file Dockerfile
  2. A sample of Dockerfile – the following few lines construct the whole environment which is production ready:                           
    FROM ubuntu:16:04
    RUN apt-get install -y redis-server
    EXPOSE 5379
    ENTRYPOINT ["/usr/bin/redis-server"]
  3. Containers contain everything our app needs:
    1. Binaries
    2. Libraries
    3. File system
  4. Containers use the following items from the host:
    1. Networks
    2. Kernel

Docker :

 

Please find below video explaining the above concept.

Difference between Virtual Machines and Docker Containers

Virtual Machines Vs Docker Containers – 

VMs can be compared with containers on below factors –

  • Execution – The VM model blends an application, a full guest OS, and disk emulation. In contrast, the container model uses just the application’s dependencies and runs them directly on a host OS.
  • Isolation – Containers do not launch a separate OS for each application, but share the host kernel while maintaining the isolation of resources and processes where required.
  • Boot time – The fact that a container does not run its own OS instance reduces dramatically the overhead associated with starting and running instances. Startup time can be drastically reduced to few seconds as compared to VMs which generally takes several minutes to boot up.
  • Scalability – The number of containers running on a typical server can reach dozens or even hundreds. The same server, in contrast, might support 10 to 15 VMs.

Below diagram show the differences between VMs and Containers –

 

 

 

CONTAINERS

Containers are an abstraction at the app layer that packages code and dependencies together. Multiple containers can run on the same machine and share the OS kernel with other containers, each running as isolated processes in user space. Containers take up less space than VMs (container images are typically tens of MBs in size), can handle more applications and require fewer VMs and Operating systems.

VIRTUAL MACHINES

Virtual machines (VMs) are an abstraction of physical hardware turning one server into many servers. The hypervisor allows multiple VMs to run on a single machine. Each VM includes a full copy of an operating system, the application, necessary binaries and libraries – taking up tens of GBs. VMs can also be slow to boot.

 

 

 

Please find below video explaining the above concept.

Docker History

Docker History

Docker was created as the internal tool in the platform as a service company, dotCloud .

In March 2013, it was released to the public as open source.Its source code is freely available to everyone on GitHub at: https://github.com/docker/docker.

Kubernetes is a tool developed by Google for deploying containers across clusters of computers.It compliments Docker when it comes to orchestration, automating deployment, managing and scaling containers; it manages workloads for Docker nodes by keeping container deployments balanced across a cluster.

Kubernetes also provides ways for containers to communicate with each other, without the need for opening network ports.

Kubernetes is also an open source project, living on the GitHub at https://github.com/ kubernetes/kubernetes.

 

The idea behind Docker

The idea behind Docker is to pack an application with all the dependencies it needs into a single standardized unit for the deployment called as image.Those dependencies can be binaries, libraries, JAR files, configuration files, scripts and so on.

Docker wraps up all of it into a complete filesystem that contains everything your Java application needs to run the virtual machine itself, the application server such as Wildfly or Tomcat, the application code, runtime libraries and basically everything you would install and deploy on the server to make your application run.

Packaging all of this into a complete image guarantees that it is portable; it will always run in the same way, no matter what environment it is deployed in.

With Docker, you can run Java applications without having to install a Java runtime on the host machine.

All the problems related to incompatible JDK or JRE, wrong version of the application server, and so on are gone.Upgrades are also easy and effortless, you just run the new version of your container on the host.

If you need to do some cleanup, you can just destroy the Docker image and it’s as though nothing ever happened.

Think about Docker, not as a programming language or a framework, but rather as a tool that helps in solving the common problems such as installing, distributing and managing the software. It allows developers and DevOps to build, ship, and run their code anywhere.

Having all of your application code and runtime dependencies packaged as a single and complete unit of software may seem the same as a virtualization engine, but it’s far from that, as we will explain difference between Virtualization  and Containerization in next section.

Bitnami