DEV Community

Cover image for Docker Containerization - Part 1
Jitendra Thakur
Jitendra Thakur

Posted on • Updated on

Docker Containerization - Part 1

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.

Design Patterns:
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.

What are containers?

  • 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

  • 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

Docker Components

Three

Container Order & Dependencies in Docker

Four

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....
Enter fullscreen mode Exit fullscreen mode

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

Docker File (Dockerfile)

Five

  • 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>
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode
  • Run below command to package your java application
 $ mvn clean install –DskipTests
Enter fullscreen mode Exit fullscreen mode

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

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>
Enter fullscreen mode Exit fullscreen mode
  • To see the list of images:
$ docker images ls -a
Enter fullscreen mode Exit fullscreen mode
  • To create and run container:
$ docker run –p 80:8080 –name <DESIRED_CONTAINER NAME> <IMAGE_NAME>
Enter fullscreen mode Exit fullscreen mode
  • To see the current status of container:
$ 'Run below command to list running containers'
$ docker ps
Enter fullscreen mode Exit fullscreen mode
$ 'Run below command to list all containers'      
$ docker ps -a
Enter fullscreen mode Exit fullscreen mode

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

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>
Enter fullscreen mode Exit fullscreen mode
  • To see the list of images:
$ docker images ls -a
Enter fullscreen mode Exit fullscreen mode
  • To create and run container:
$ docker run –p 5000:5000 –name <DESIRED_CONTAINER NAME> <IMAGE_NAME>
Enter fullscreen mode Exit fullscreen mode
  • To see the current status of container:
$ 'Run below command to list running containers'
$ docker ps
Enter fullscreen mode Exit fullscreen mode
$ 'Run below command to list all containers'      
$ docker ps -a
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)