top of page
Writer's pictureTheNextHacker

How Containers actually works? Container Runtime

Updated: Apr 3, 2023


In the world of software development and deployment, container technology has become a popular buzzword in recent years. It offers a lightweight and efficient way to package an application and all its dependencies, making it portable and easily deployable across various computing environments. But, to truly harness the power of containers, it's important to have a deeper understanding of the technology behind it. In my previous blog, I explained some of the foundational concepts like cgroups and namespaces that enable containerisation in Linux. In this blog, I'll delve into the inner workings of the container runtime, which is responsible for creating and managing containers. By gaining a better understanding of this crucial component, you'll be better equipped to leverage containers for your development and deployment needs.


A container runtime, also known as a container engine, is a software component that manages the lifecycle of containers on a host system. It is responsible for starting, stopping, and managing the container's execution environment, as well as providing access to the underlying operating system's resources. When a container runtime, such as Docker or containerd, starts a new container, it uses the Linux kernel's namespaces and cgroups to provide the necessary isolation and resource management.

here are some examples of how container runtimes use namespaces and cgroups to start a container:

  1. Docker: When you run a Docker container, Docker creates a new set of namespaces for the container process. These namespaces include the network namespace, which provides the container with its own isolated network stack, and the PID namespace, which provides the container with its own process tree. Docker also uses cgroups to limit the amount of CPU, memory, and other system resources that the container can use.

  2. CRI-O: CRI-O is an open-source implementation of the Kubernetes Container Runtime Interface (CRI) that enables Kubernetes to run containerized applications using the Open Container Initiative (OCI) format. CRI-O is designed to be lightweight, secure, and optimized for Kubernetes environments. It utilizes OCI images, runc as the default runtime, and is based on the Go programming language.


Docker

Docker utilizes the capabilities of runc to manage containers. runC is an implementation of the Open Container Initiative (OCI) specification for container runtime. Docker creates a bundle of files, including a configuration file that specifies the container runtime to be used, and by default, Docker uses runC as the container runtime.

runC is responsible for creating, starting, stopping, and deleting containers. It uses various Linux kernel technologies such as namespaces and cgroups to provide an isolated environment for containerized applications.

Namespaces provide isolation for various system resources such as the network, process, and file system namespaces. runC uses namespaces to create an isolated environment for each container, ensuring that processes running inside the container are isolated from the host system.

cgroups are used by runC to manage the resources that are available to each container. cgroups allow the allocation of specific resources, such as CPU, memory, and I/O bandwidth, to each container.

When Docker creates a container, it creates a new set of namespaces for the container process. These namespaces include the network namespace, which provides the container with its own isolated network stack, and the PID namespace, which provides the container with its own process tree. Docker also uses cgroups to limit the amount of CPU, memory, and other system resources that the container can use.

Overall, runC provides the low-level functionality required for running containers securely and efficiently. It works closely with Docker to provide a standardized container runtime that is compatible with the OCI specification. The combination of Docker and runC allows developers to easily create, manage, and deploy containers while providing a secure and efficient runtime environment.


Here are some examples of runC

  • Creating a container using runc

This command creates a new container specification file named "config.json" in the "/tmp/mysandbox" directory. The container specification file defines the configuration settings for the container, such as the root filesystem, network settings, and process settings.

The --root flag specifies the root directory for the container, which is where the container's filesystem will be created. In this example, the root directory is set to "/tmp/mysandbox".

The run subcommand tells runc to create and start the container. The name of the container is specified as an argument to the command.

When you run these commands, runc will create a new container with the specified name and configuration settings, and start the container. You can then interact with the container as you would with any other running process.

  • Listing all containers

The sudo runc list command is used to list all the containers that are currently running on the system. When you run this command, runc will output a table that includes information about each running container, such as the container ID, the process ID of the container's init process, the current status of the container, and the directory containing the container's filesystem.

In this example, there is one container running named "mycontainer", which has a process ID of 4788 and is currently in a "running" state. The container's filesystem is located in the directory "/tmp/mysandbox/mycontainer", and it was created by the "root" user.

  • Starting and stopping a container

The sudo runc start mycontainer command is used to start a container that has been previously created with runc. When you run this command, runc will start the container's init process and any other processes specified in the container's configuration file.

The start subcommand is followed by the name of the container that you want to start (in this example, "mycontainer").

Here's an example of what the output might look like:

In this example, the container named "mycontainer" has been started successfully.


To stop a running container, you can use the sudo runc stop mycontainer command. When you run this command, runc will send a signal to the container's init process, causing it to shut down all of the container's processes and stop the container.

Here's an example of what the output might look like:

In this example, the container named "mycontainer" has been stopped successfully.


  • Executing a command inside a container

runc exec is a command that allows you to execute a command inside a running container. It starts a new process inside the container and runs the specified command with the given arguments. This is useful for tasks like debugging, running one-off commands, or interacting with a container's filesystem.


The sudo runc exec mycontainer ls / command is used to execute a command inside a running container. When you run this command, runc will start a new process inside the container and run the specified command (in this case, "ls /").

The exec subcommand is followed by the name of the container that you want to run the command inside (in this example, "mycontainer"), and then the command itself (in this example, "ls /").


In conclusion, container runtime is a fundamental technology that enables the creation and management of containers. It provides an abstraction layer that isolates containers from the underlying host system, allowing them to run reliably and securely. Docker is one of the most popular container runtime implementations, which utilizes runc to create and manage containers. Runc is an open-source, lightweight tool that conforms to the Open Container Initiative (OCI) specification and provides a simple interface for managing containers. Its ability to integrate with various Linux technologies such as namespace and cgroup makes it a powerful tool for running and managing containerized applications. Overall, container runtime and runc play a crucial role in the development and deployment of modern applications and are essential for achieving the benefits of containerization, such as portability, scalability, and resource efficiency.


Thank you for your support! If you find my content informative and helpful, please consider subscribing to my page/channel and sharing it with others who may find it valuable. Your support is greatly appreciated and helps me reach a wider audience. Facebook:-> https://www.facebook.com/thenexthacker

41 views0 comments

Recent Posts

See All

Comments


bottom of page