DEV Community

Cover image for CONTAINERIZATION
Victoria Oludamilola Oyewole
Victoria Oludamilola Oyewole

Posted on

CONTAINERIZATION

WHAT IS CONTAINERIZATION?

Containerization is a method of packaging an application along with its dependencies, libraries, and configuration files into a single unit called a container. This ensures that the application runs consistently across different computing environments.

In software engineering, containerization is operating-system-level virtualization or application-level virtualization over multiple network resources so that software applications can run in isolated user spaces called containers in any cloud or non-cloud environment, regardless of type or vendor.

Containerization is a software deployment process that bundles an application’s code with all the files and libraries it needs to run on any infrastructure. Traditionally, to run any application on your computer, you had to install the version that matched your machine’s operating system. For example, you needed to install the Windows version of a software package on a Windows machine. However, with containerization, you can create a single software package, or container, that runs on all types of devices and operating systems.

Key Principles of Containerization

Isolation: Containers provide a sandboxed environment, isolating the application from the host system and other containers. This isolation is achieved through OS-level virtualization, where containers share the same host kernel but have separate namespaces and resource control mechanisms.

Portability: Containers can run on any system that supports containerization, such as Linux, Windows, and Mac operating systems. This makes it easier to develop, test, and deploy applications across different environments without worrying about compatibility issues.

Efficiency: Containers are lightweight and have minimal overhead compared to traditional virtual machines (VMs). They share the host OS kernel, which reduces the need for duplicating the OS in each container, leading to better resource utilization and faster startup times.

Scalability
Containers are lightweight software components that run efficiently. For example, a virtual machine can launch a containerized application faster because it doesn't need to boot an operating system. Therefore, software developers can easily add multiple containers for different applications on a single machine. The container cluster uses computing resources from the same shared operating system, but one container doesn't interfere with the operation of other containers.

Fault tolerance
Software development teams use containers to build fault-tolerant applications. They use multiple containers to run microservices on the cloud. Because containerized microservices operate in isolated user spaces, a single faulty container doesn't affect the other containers. This increases the resilience and availability of the application.

Agility
Containerized applications run in isolated computing environments. Software developers can troubleshoot and change the application code without interfering with the operating system, hardware, or other application services. They can shorten software release cycles and work on updates quickly with the container model.

What are containerization use cases?

The following are some use cases of containerization.

Cloud migration
Cloud migration, or the lift-and-shift approach, is a software strategy that involves encapsulating legacy applications in containers and deploying them in a cloud computing environment. Organizations can modernize their applications without rewriting the entire software code.

Adoption of microservice architecture
Organizations seeking to build cloud applications with microservices require containerization technology. The microservice architecture is a software development approach that uses multiple, interdependent software components to deliver a functional application. Each microservice has a unique and specific function. A modern cloud application consists of multiple microservices. For example, a video streaming application might have microservices for data processing, user tracking, billing, and personalization. Containerization provides the software tool to pack microservices as deployable programs on different platforms.

IoT devices
Internet of Things (IoT) devices contain limited computing resources, making manual software updating a complex process. Containerization allows developers to deploy and update applications across IoT devices easily.

How does containerization work?
Containerization involves building self-sufficient software packages that perform consistently, regardless of the machines they run on. Software developers create and deploy container images—that is, files that contain the necessary information to run a containerized application. Developers use containerization tools to build container images based on the Open Container Initiative (OCI) image specification. OCI is an open-source group that provides a standardized format for creating container images. Container images are read-only and cannot be altered by the computer system.

Container images are the top layer in a containerized system that consists of the following layers.

Infrastructure
Infrastructure is the hardware layer of the container model. It refers to the physical computer or bare-metal server that runs the containerized application.

Operating system
The second layer of the containerization architecture is the operating system. Linux is a popular operating system for containerization with on-premise computers. In cloud computing, developers use cloud services such as AWS EC2 to run containerized applications.

Container engine
The container engine, or container runtime, is a software program that creates containers based on the container images. It acts as an intermediary agent between the containers and the operating system, providing and managing resources that the application needs. For example, container engines can manage multiple containers on the same operating system by keeping them independent of the underlying infrastructure and each other.

Application and dependencies
The topmost layer of the containerization architecture is the application code and the other files it needs to run, such as library dependencies and related configuration files. This layer might also contain a light guest operating system that gets installed over the host operating system.

STEPS ON HOW TO CONTAINERIZE AN APPLICATION WITH THE USE OF VISUAL CODE

  • Download the Visual code application, allow it to run then create a new folder for your work.

  • Make a Directory(mkdir) to the new file.

Image description

  • Type the command cd(Change directory)to get into the folder you're working in.

  • Copy the repository for the application into the visual code to clone it.

What is a Repository?
A repository (often abbreviated as repo) is a centralized storage location where all the files and resources of a project are kept. It acts as a folder on the cloud, containing a set of programming files that collectively make up an application. Repositories are essential for collaborative software development, allowing multiple developers to work on the same project simultaneously.

What is Cloning a Repository?
Cloning a Git repository involves creating a local copy of a remote repository on your computer. This allows you to work on the project locally, make changes, and then push those changes back to the remote repository.

Before you can run the application, you need to get the application source code onto your machine.

Clone the getting-started-app repository using the following command:

git clone https://github.com/docker/getting-started-app.git

  • After the repo has been copied on the VS code to run.

Image description

  • All the files needed for the application has been clone after it has been run. That is, view the contents of the cloned repository. You should see the following files and sub-directories. The files are ones which appeared after red thick box.

Image description

             ** BUILD THE APPLICATION IMAGE**
Enter fullscreen mode Exit fullscreen mode

To build the image, you'll need to use a Dockerfile. A Dockerfile is simply a text-based file with no file extension that contains a script of instructions. Docker uses this script to build a container image.

  • In the getting-started-app directory, the same location as the package.json file, create a file named Dockerfile with the following contents:

syntax=docker/dockerfile:1

FROM node:lts-alpine
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "src/index.js"]
EXPOSE 3000

Image description

  • Build the image using the following commands:

In the terminal, make sure you're in the getting-started-app directory. Replace /path/to/getting-started-app with the path to your getting-started-app directory. That is, right click on the directory "getting-started-app directory" then select the option of Open in integrated terminal, its terminal will be opened.

Image description

  • Build the image. Type the command below to build the image.

docker build -t getting-started .

Image description

Image description

  • The file running on the Docker Engine.

Image description

Image description

               **START AN APP CONTAINER**
Enter fullscreen mode Exit fullscreen mode

Now that you have an image, you can run the application in a container using the docker run command.

Run your container using the docker run command and specify the name of the image you just created:

   docker run -d -p 127.0.0.1:3000:3000 getting-started
Enter fullscreen mode Exit fullscreen mode

Image description

  • If you take a quick look at your containers, you should see at least one container running that's using the getting-started image and on port 3000. The image below shows the visual code CLI

Image description

  • The running Container of the Docker Desktop

Image description

              **  PART 2**

         _** UPDATE THE APPLICATION**_
Enter fullscreen mode Exit fullscreen mode
  • Update the source code In the following steps, you'll change the "empty text" when you don't have any todo list items to "You have no todo items yet! Add one above!"

1.
In the src/static/js/app.js file, update line 56 to use the new empty text.

  • No items yet! Add one above!

  • You have no todo items yet! Add one above!

Image description

2. Build your updated version of the image, using the docker build command.

docker build -t getting-started .

Image description

3. Start a new container using the updated code.

docker run -dp 127.0.0.1:3000:3000 getting-started.

However, you probably saw an error like this:

docker: Error response from daemon: driver failed programming external connectivity on endpoint laughing_burnell

(bb242b2ca4d67eba76e79474fb36bb5125708ebdabd7f45c8eaf16caaabde9dd): Bind for 127.0.0.1:3000 failed: port is already allocated.

The error occurred because you aren't able to start the new container while your old container is still running. The reason is that the old container is already using the host's port 3000 and only one process on the machine (containers included) can listen to a specific port. To fix this, you need to remove the old container.

Image description

** Remove the old container**

To remove a container, you first need to stop it. Once it has stopped, you can remove it. You can remove the old container using the CLI or Docker Desktop's graphical interface. Choose the option that you're most comfortable with.

  • Remove a container using the CLI Get the ID of the container by using the docker ps command.

*docker ps

Image description

  • Use the docker stop command to stop the container. Replace with the ID from docker ps.

*docker stop

  • Once the container has stopped, you can remove it by using the docker rm command.

           ** Start the updated app container**
    

1.
Now, start your updated app using the docker run command.

docker run -dp 127.0.0.1:3000:3000 getting-started

Image description

2.Refresh your browser on http://localhost:3000 and you should see your updated help text.

Top comments (1)

Collapse
 
nevodavid profile image
Nevo David

pretty cool seeing this spelled out so clear, honestly makes me want to mess around with containers asap - you think actually running through it a few times is the only way to get past feeling lost with all these tools?