Containerization with Docker

Rmag Breaking News

What is Containerization and Why do we need Containerization ?

Containerization is a concept where software can be isolated in an operating system. Now that sounds like a Virtual Machine. But not exactly. Virtual machine virtualizes an operating system firmware and software to provide a representation of a physical machine. Where virtualization takes in place of a virtual CPU, GPU, Disk, Kernel etc. and each and individual component of an actual computer is partitioned from your mother operating system to the virtual machine. So, a virtual machine shares the components from your operating system and lets you virtualize an entirely new operating system without the cost of buying them. But looking at a different angle, it still shares your computer’s resources.

Why the need for Virtual Machine?

Sometimes running an entire application in an existing operating system might cause functionality issues. So, companies recommend publishing software or applications in a fresh operating system. Buying a completely new computer can be costly for the company because it requires a lot of maintenance. so, a virtual computer takes the place to solve the issue. because it provides a fresh view of an actual operating system (virtualized).

What’s wrong with Virtual Machine?

While virtual machines replace the cost of an actual operating system. it still shares the mother computer’s CPU, GPU, RAM, DISC etc. and it has its completely new virtualized kernel just to run an application.

What’s the solution?

If the problem of a virtual machine is analyzed correctly it seems that to run an application it may require a little bit of CPU and Disk but it is not required to virtualize an entire operating system with firmware and software included in it. So, Containerization comes into view where it’s almost like a new operating system but it’s not. instead, it uses the mother’s kernel and shares a very minimum and required CPU and RAM. Now here comes the question, what is Docker?

What is Docker?

Docker is a tool to Containerize application. So, the term Containerization is a concept and the tool to implement it is Docker.

How to Containerize with Docker?

To Containerize application with Docker we must have to go through these steps:

Install Docker.
Run Docker.
Build an Image.
Create a Container.

How to Build an Image?

At first, let’s understand what is Image in docker. In docker, an image refers to a set of instructions which the container will follow. like which version of compiler, it needs or which version of Linux it requires. It is mainly a file that prescribes the instructions for the container.

Assuming that you have a simple node.js API running in port 8000. Now to Containerize it the container needs to know what application to run and which port to listen to. and what are the pre-conditions that must be taken to use this application.

To start the process of creating and building an image we have to create a file called Dockerfile inside the folder where our application is located. Given below is an example of an actual Dockerfile which runs the application and provides the pre-conditions before running it:

Here’s an example of a Dockerfile that runs a Node.js API on port 8000:

Dockerfile
# Use a slim Node.js base image
FROM node

# Set the working directory inside the container
WORKDIR /app

# Copy package.json and package-lock.json (or yarn.lock)
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy your application code
COPY . .

# Expose port 8000 (This doesn’t map the port, it just informs Docker)
EXPOSE 8000

# Start the application using the default command (usually specified in package.json)
CMD [ “npm”, “start” ]

and then run:

docker build -t my-node-api .

This will build the image successfully.

How to create a Container?

After the installation of image is complete. now it’s time to create the container using this command:

docker run -p 9999:8000 –name MyApp -itd my-node-api

here the -p 9999:8000 flag represents that going to mother computer’s 9999 port will be used as a proxy for the 8000 ports in the container. and the –name flag is used to represent what is the name of container. Lastly -itd represents it will be run interactively in a daemon and my-node-api is the name of the image.

Congrats! You have successfully created and ran a container.

Make sure to give your feedback.

Leave a Reply

Your email address will not be published. Required fields are marked *