How to perform Containerization in Docker 🐳?

Nidhi Chaurasia
7 min readJul 20, 2021
“This reduces the risk of “worked in dev, now an ops problem.”
James Turnbull, The Docker Book: Containerization is the new virtualization

“If you need to run multiple processes or commands as part of starting a container you should use a service management tool like Supervisor.”
James Turnbull

The main purpose of mine to introduce this quoted line is to ensure my readers and encourage them to understand the vulnerabilities that we faced before the launch of Docker in 2013 jump and how drastically it started a revolution in application development — by democratizing software containers. We could observe the consistently increasing graph of Docker that developed a Linux container technology — one that is portable, flexible and easy to deploy. Docker open sourced libcontainer , partnered with a worldwide community of contributors to further its development. In June 2015, Docker donated the container image specification and runtime code now known as runc, to the Open Container Initiative (OCI) to establish standardization as the container ecosystem growth.

Following this evolution, Docker continues to give back with the containerd project, which Docker donated to the Cloud Native Computing Foundation (CNCF) in 2017. containerd is an industry-standard container runtime that leverages runc and was created with an emphasis on simplicity, robustness and portability. containerd is the core container runtime of the Docker Engine.

Following this core concept I have explained the methodology of Containerization in Docker using Ubuntu software and how it reduces the chance of slow or interrupted deployment of our built application on local server/machine , also I would be dealing with the importance of containers like how it is portable between different platforms and clouds on server . So, Let’s move ahead !✌

Tasks we’ll do : Deploy our web services , and each service will deploy a container php_web.

For web service I have used : — ->>a) php:rc-apache.

b) Container name is php_web.

c) Use image php with any apache tag. Click here for more ☞ details .

d) Map php_web container’s port 80 with host port 6000 .

e) Finally Map php_web container’s /var/www/html volume with host volume /var/www/html .

Before starting with Docker in the virtual machine first of all we need to install Docker into it / if present then check the docker-ce using below commands :-

yum list | grep docker-ce -y
yum install docker-ce -y > /dev/null #Docker Installation
rpm -q docker-ce #Check Docker installed successfully
docker version #Check Docker version
docker run php #Runs container name php
Now, to pull images from free repository into docker we have to refer dockerhub , in my case I have pulled php images and the command goes like :-
sudo docker pull php
Fig :- To view all the images present in your local machine , type :- docker images

In the below snippet I have pulled php:rc-apache images into the container . And then we check how many images are there in our docker :

sudo docker images             #lists all the docker images 
sudo docker ps #Shows how many containers are running

After the images are extracted from the source (PS : dockerhub) we pinged google.com and run our container over there.

sudo docker run php ping google.com
sudo docker run -d php ping google.com #Runs container in the background and detaches the google.com from container
Using echo command with the docker run , simply stops the whole process and prints the data after the echo command without running container .

To see the directories and folders present inside our php container we need to execute this command :-

sudo docker run php ls         #Here ls is default override command 

NOTE :- “docker run” command is made of two commands i.e. “docker create” and “docker start” which means while we execute “docker run” both these commands are automatically executed in the back-end and creates and starts the container at the same time .

Here -a attaches the container id and the associated processes with the container .
sudo docker ps -a  

This command lists all the stored containers in our local machine and their states whether exited or still running .

To check on which port no. our container is routing :-

docker port web    #Reading out the traffic from port no.80 from the host to container

Now, let’s create our own networks and deal with the processes :-

To inspect our container we need to do it this way 👇 followed by our container id :-

Then to list all the processes which are running in the container we execute this way(using top command) 👇 :-

docker top your containerid        #All processes pinging google.com will be listed(running processes in containers)
To go inside running container we executed “exec -it” command followed by container id /bin/bash

NOTE :- The command -i simply stands for “input” and -t is meant for “presentation” not to go inside the container terminal . The -i command can alone take us into the container terminal .

Sometimes containers don’t support the execution via /bin/bash as bash terminal is not accessed by them using this command .

sudo docker exec -it containerid /bin/bash #If supports bash terminal

Else we need to follow this command :-

sudo docker exec -it containerid /bin/sh    #Goes inside sh terminal
The technical name of dot(.) is build context in the first line of command .

To build docker file we use the following comand :-

sudo docker build .    #dot specifies the file location of container
We checked whether our newly created image is present in the container or not and it could be observed in the very first line having unique IMAGE ID and size of 19.1 MB.
We build the image again and tagged the image using -t command which executed in very short span of time as the image was already present in our container in the local machine .

NOTE :- The container images were fetched via cache as it was already present there . Hence, the execution time was negligible while running the docker container .

The docker ps command only shows running containers by default and the grep filter searches a php file for a particular pattern of characters, and displays all lines that contain that pattern. The pattern that is searched in the file is referred to as the regular expression (grep stands for globally search for regular expression and print out).

The default docker images will show all top level images, their repository and tags, and their size. Adding before it the sudo command which allows usto run programs with the security privileges of another user (by default, as the superuser). It prompts us for our personal password and confirms our request to execute a command by checking a file, called sudoers , which the system administrator configures.

mkdir command in Linux allows the user to create directories (also referred to as folders in some operating systems ). This command can create multiple directories at once as well as set the permissions for the directories.

The cat command is used to print the code that we have written inside the index.js file.

In the above figure I have made a directory Docker_project_php

Creating Ubuntu Container Using Dockerfile :-

Here , we built an image from Dockerfile and created the image using Ubuntu 20.04. The best practice document of Dockerfiles creation is if we are writing it for production and image deployment. We need to create a file using vim command and configure the basic commands along with pinging iputils and gradually install net tools . Like I have shown below -

This code can be used inside the php_web file :-

FROM ubuntu:20.04RUN apt-get update && apt-get install -y openssh-server
RUN apt-get install -y iputils-ping && apt-get install -y net-tools
RUN mkdir /var/run/sshd
RUN echo 'root:YOURPASSWORD' | chpasswd
RUN sed -i 's/#*PermitRootLogin prohibit-password/PermitRootLogin yes/g' /etc/ssh_config

Through this code we would be updating the prebuild software and it would install openssh server . While we are creating the file manually then we need to save it , else if we are using the vim command that opens the editor and we may use this code there and save it using :wq command.Well , Vim is an editor to create or edit a text file.

There are basically two modes in vim. One is the command mode and another is the insert mode.

In the command mode, user can move around the file, delete text, etc.

In the insert mode, user can insert text.

Changing mode from one to another

👉 From command mode to insert mode type a/A/i/I/o/O .

👉 From insert mode to command mode type Esc (escape key) .

Sometimes when we execute a command, it fails or prompts us for additional input then -f is used to force. This may be an effort to protect the files that we are trying to change or inform the user that a device is busy or a file already exists.
We could observe the image created using Dockerfile in Ubuntu Container .
Here we have listed all the containers present in our Dockerfile using (sudo docker ps ) command .
The server is started with our container images and their functionality on the web .

Thus , we saw that container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another. A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings.

Container images become containers at runtime and in the case of Docker containers — images become containers when they run on Docker Engine.

Thus, Containerization in Docker improves security by isolating applications from the host system and from each other thereby fastening app start-up and enhancing fast-paced scaling features among the network .

--

--

Nidhi Chaurasia

CSE(Major) | Google Cloud | Programming | Python Developer | Technical Writer | Open-Source Contributor