Open In App

What is a Container ?

Last Updated : 16 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

One of the greatest challenges in software development is ensuring that an app works similarly in a variety of environments. In earlier times, this has been attended to by working through a virtual machine (VM), but it's quite a heavyweight solution. That's when containers came along, as a more lightweight and effective alternative for this challenge. They encapsulate an application and its dependencies in such a way that the same computing environment can run without running into problems.

Primary Terminologies

  • Container: An isolated, stand-alone unit that encapsulates an application and all its dependencies, it runs the same and consistently in any environment, independently of the host system, being unaffecting and not getting affected by it.
  • Docker: Docker is an open-source platform designed to make it easy for containers to be built, developed, and run. It provides one with all the software required, in addition to development capabilities, to build, run, and manage containers for maximum efficiency.
  • Image: A container image is a lightweight, read-only, executable file that includes everything needed to run a piece of software: the code, the runtime, the libraries, the environment variables, and configurations. It basically serves as a template for creating containers.
  • Containerization: The way of bundling the application together with all its dependencies into a container .In this way, the application acts in the same way in which it is executed.
  • Orchestration: It automatically takes care of the coordination, scheduling, and management of multi-container deployments running on a cluster of machines, container orchestration tools include Kubernetes and Docker Swarm.

What is Containers ?

  • A container is a light, stand-alone, and executable software package that wraps an application with all its dependencies, such as libraries, configuration files, and binaries, in order to be run. Containers make sure that no matter where an application is deployed, whether it is in a developer's laptop, the testing environment, or the production server, it behaves the same way.
  • Containers do this by isolating an application from the underlying system, and therefore they are not dependent on any setting or software installed in the operating system of the host. Instead everything an application requires to run is packed within the container itself. This isolation allows containers to be more efficient compared to traditional virtual machines (VMs), which contain a full operating system besides the application.

Containers vs. Virtual Machines (VMs)

Containers

  • Architecture: All containers share the host OS kernel; however, the running user spaces are isolated, making them lightweight.
  • Boot Time: Containers have much less boot time typically in seconds, as they do not need to boot a full OS.
  • Isolation: Containers provide isolation at the process level, which is less strong compared to VMs, but for many use cases this does not matter
  • Resource Usage: Containers consume fewer resources because they do not need an entire OS—only the necessary binaries and libraries.

Virtual Machines (VMs)

  • Architecture: A hypervisor that runs on the host OS includes a full guest OS with virtualized hardware.
  • Resource Usage: Very high, as the full OS overhead is incurred for each instance.
  • Isolation: Very good because each VM is a system on its own with its own OS.
  • Boot Time: VMs typically have longer boot times because the full OS in a VM needs to be initialized.

What is Containerization?

Containerization is the process of packing an application together with all its dependencies into a container in order to allow the application to run consistently from one computing environment to another, in simple terms containerization involves using the host OS kernel to run many isolated instances of applications on the same machine, making it very lightweight and efficient in deploying applications.

Use Cases for Containerization

  • Microservices Architecture: Containers are just the right fit for deploying microservices, where each service runs in its own container.
  • CI/CD Pipelines: Containers make CI/CD very easy by providing consistent environments from development to production.
  • Cloud-Native Applications: Containers are the underlying infrastructure for cloud-native applications, while at the same time being portable across various cloud providers.
  • Dev/Test Environments: Containers make it easier to set up consistent development and testing environments, significantly reducing the problems associated with "it works on my machine."
  • Legacy Application Modernization: Containers enable legacy applications to be packaged in a way that makes them easier to deploy and manage on modern infrastructure.

Step-by-Step Process: How Containers Work?

Step 1: Install Docker

  • Use the following command to install Docker on your local machine.
sudo yum -y install docker
Install Docker
  • Now start and enable docker
sudo systemctl start docker
sudo systemctl enable docker
sudo systemctl status docker
To Start,Enable & Check Status Of Docker

Step 2: Create a Dockerfile

  • A Dockerfile is a script containing a series of instructions on how to build a container image.
  • The Dockerfile typically starts with a base image (e.g., a specific version of Linux or an official image for a programming language like Python or Node.js).

Example Dockerfile:

# Use an official Node.js runtime as the base image.

FROM node:14

# Specify the working directory inside the container.

WORKDIR /app

# Copy package.json and package-lock.json files

COPY package*.json ./

# Install the application dependencies

RUN npm install

# Copy the rest of the application code

COPY . .

# Define the command to run the application

CMD ["node", "app.js"]

Dockerfile

Step 3: Create package.json file

  • Ensure that the express module is listed under the dependencies section.

{

"name": "my-node-app",

"version": "1.0.0",

"main": "app.js",

"dependencies": {

"express": "^4.17.1"

}

}

package.json file

Step 4: Create the app.js File

  • If you need to create the file, here's a basic example:

const express = require('express');

const app = express();

const port = 3000;

app.get('/', (req, res) => {

res.send('Hello, World!');

});

app.listen(port, () => {

console.log(`App running on http://localhost:${port}`);

});

Create the app.js File

Step 5: Build the Container Image

  • Use the Dockerfile to build the container image. This image includes the application code, all dependencies, and any necessary environment settings.

Command to build the image:

docker build -t my-node-app .
  • The -t flag tags the image with a name (my-node-app) and the . shows that the Dockerfile is located in the current directory.
Command to build the image

Step 6: Store the Image

  • After building the image, it can be stored locally or pushed to a container registry (like Docker Hub or a private registry).

Command to push the image to Docker Hub:

docker push my-node-app
Command to push the my-node-app Image to Docker Hub

Step 7: Run the Container

  • Once the image is built (and optionally stored in a registry), it can be run as a container.
  • The container runs the application in an isolated environment, ensuring consistency across different deployment environments.
  • Command to run the container:
docker run -d -p 3000:3000 my-node-app
  • The -d flag runs the container in detached mode (in the background), and -p 3000:3000 maps port 3000 on the host to port 3000 on the container.
Run the Container
  • We can check container list by using following command
docker ps
To Check Container List

Step 8: Access the Application

  • Finally, if everything is set up correctly, you should be able to access your Node.js application by navigating to http://<Public IP >:3000 in your web browser.
Access the Application

Benefits of Containers

  • Portability: Containers package all dependencies, enabling applications to run consistently across various environments.
  • Efficiency: Containers use fewer resources than VMs, allowing for higher density of applications on the same hardware.
  • Scalability: Containers can be easily scaled up or down, enabling efficient handling of dynamic workloads.
  • Faster Deployment: Due to their lightweight nature, containers can be deployed quickly, enhancing development and CI/CD processes.
  • Consistency: Containers ensure that the application runs the same way across development, testing, and production environments.

Conclusion

Software containers have revolutionized the way application software is developed and then deployed in a consistent, efficient, and scalable way across any computing environment. Containers package all applications with their dependencies, so most of the problems usually faced when deploying software in the old-fashioned way have been completely removed. This deems containers as a necessity in modern DevOps practice. Supported by original examples and diagrams, the paper goes in depth about containers to ensure its content is informative and unique.


Next Article
Article Tags :

Similar Reads