Docker Containerization - Part 1
Tags: containerization, docker, deployment, architecture
Published: 2021-12-14 06:42:21

## Overview
Here in this post you will get understanding of Docker and how to deploy applications developed in different languages (Java and Python), before going a head please make sure you have hands-on experience on Java or Python, in this post just we are going to deploy the applications those are running in your local.

<u>Design Patterns:</u>
**The monolithic architecture** is considered to be a traditional way of building the applications. A monolithic application is self-contained, and independent from other computing applications.

**Microservice architecture** It’s an architectural style, microservice breakdown an application in collection of smaller independent unites that are :-

- Highly maintainable and easy to test
- Loosely coupled
- A small team can manage

This architecture provide rapid, frequent and reliable delivery solution of complex and large applications.

<u>What are containers?</u>

- **Standard:** Containers are portable anywhere and it fits to industry standards by docker. 

- **Lightweight:** It share the machines Operating System kernel (because of this it doesn't require an OS per application), it reduce other costs like licensing and servers cost.

- **Secure:** Docker has the strongest default isolation capabilities and this is the reason applications are safer in containers.

## Docker Feature
Docker is a platform for developers and sysadmins to build, run, and share applications with containers. The use of containers to deploy applications is called containerization. Containers are not new, but their use for easily deploying applications is.

Why containerization is getting popular because container is:

- **Flexible:** Even the most complex applications can be containerized.
- **Lightweight:** Containers leverage and share the host kernel, making them much more efficient in terms of system resources than virtual machines.
- **Portable:** You can build locally, deploy to the cloud, and run anywhere.
- **Loosely coupled:** Containers are highly self sufficient and encapsulated, allowing you to replace or upgrade one without disrupting others.
- **Scalable:** You can increase and automatically distribute container replicas across a datacenter.
- **Secure:** Containers apply aggressive constraints and isolations to processes without any configuration required on the part of the user. 

## Docker Engine

![One](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mqlo9sbhqn1tp9hq52hv.JPG)

- **Docker Daemon –** Persistent background process that manage image, network, storage volumes other processes. 
- **REST API –** This is the API used by application to interact with docker daemon.  
- **Docker CLI (Client) –** Command line interface to interact with docker daemon.

## Docker Architecture For the Applications

![Two](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4oijh4oz6n7h9vx932u4.JPG)

## Docker Components

![Three](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/44zzsxsbloe1guc1jjep.png)
 
## Container Order & Dependencies in Docker

![Four](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i1pv4la46u70uqg53uak.png)

**Docker Compose** 
Using above sequence we can control the order of service startup and shutdown the **'depends on'** option. Compose always start and stop containers in the order of dependent, where dependencies are determined by 

```
$ depends_on
$ links
$ volumes_from
$ network_mode

services....
```

> I will cover **docker-compose** in next post along with few more containers (Active-MQ and PostgreSQL).

## Docker File (Dockerfile)

![Five](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y3gwvy890cd4b6exu15w.png)
  

- Dockerfile is a text document a user write all the commands that can run from command line to assemble an image.
- A user can compose multiple images to include system libraries, tools and other files to make it executable code.
- The advantage of dockerfile is - it can build images automatically by reading the instructions written inside and it reduce the extra effort of firing multiple commands on command line.

## Deployment

**Step 1: Pre-Deployment**
Follow the below steps to complete pre-deployment process:

- Pull the code (developed in java spring MVC/boot) from git repository on server instance using 

```
$ git clone <git-url-here>
```
**Note:** _if in case your code is not on git repository, then copy and paste it on your server machine at your desired location._

- Go inside base project directory 

```
$ cd <your-project-directory>
```
- Run below command to package your java application

```
 $ mvn clean install –DskipTests
```
Once installation and packaging process done successfully, lets understand the docker containerization in the next steps.

**Step 2: Java Application Containerization**

> Create a Dockerfile and write code as given in below image, once completion save Dockerfile to your root folder of your application/project.

![Java Dockerfile](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bxeeyn7y2x5paq8ij3wz.JPG)

**Dockerfile detailed information**

- Update docker file name as per application name: 
Dockerfile-{your-app-name-here}

- Docker images start with a base image, although a user can build one entirely from scratch, if desired. 

- Docker runs the instructions in a Dockerfile in order. The first instruction must be FROM in order to specify the Base Image from which we are building.

> **Note :** _Do not run below command if you have any dependency enabled in properties file on any other application (container). In next Post we will cover container dependency using ‘Docker Compose’._
  
- To build an image 

```
$ docker build -t <IMAGE_NAME>:<TAG>
```
- To see the list of images: 

```
$ docker images ls -a
```
- To create and run container: 
      

```
$ docker run –p 80:8080 –name <DESIRED_CONTAINER NAME> <IMAGE_NAME>
```
- To see the current status of container:

```
$ 'Run below command to list running containers'
$ docker ps
```
 

```
$ 'Run below command to list all containers'      
$ docker ps -a
```


**Step 3: Python Application Containerization**


Create a file with the name of 'Dockerfile-python' and write code as given below in the file and save this file under the root folder of your project/application.

> **Note:** _This code will work with flask based application._ 


![Python Dockerfile](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5l6mn8n9fo7uycp0tn5d.JPG)

**Docker file detailed information**

Update docker file name as per application name: 

> Dockerfile-{your-app-name-here}
Docker images start with a base image, although a user can build one entirely from scratch, if desired. 

Docker runs the instructions in a **Dockerfile** in order. The first instruction must be **FROM** in order to specify the Base Image from which we are building.

**Note:** _Do not run below command if you have any dependency enabled in properties file on any other application (container). In Part-II we will cover container dependency using ‘Docker Compose’._ 
 
- **base image** here we are using python 3.7.1 as base image for our python application.

- **label** For each and every deployment user can provide new labels to differentiate the application information.

- **env** set environment variables for application

- **requirments.txt** list all the required libraries into file, this file will be used during docker containerization process.
- **expose** expose work as described in previous slides

- **cmd** cmd used to execute any command in docker container (Linux based)
 
Lets _Build_ and _Run_ Docker container for your python application:

- To build an image 

```
$ docker build -t <IMAGE_NAME>:<TAG>
```
- To see the list of images: 

```
$ docker images ls -a
```
- To create and run container: 
      

```
$ docker run –p 5000:5000 –name <DESIRED_CONTAINER NAME> <IMAGE_NAME>
```
- To see the current status of container:

```
$ 'Run below command to list running containers'
$ docker ps
```
 

```
$ 'Run below command to list all containers'      
$ docker ps -a
```


_**Thanks for your reading and time. I really appreciate it!**_