Virtual machines are heavy, since each carries a full operating system. Containers give isolation with far less weight, and have changed the way software is built and run.

Containers

A container packages an application with all that it needs to run, such as libraries and settings, but shares the kernel of the host operating system instead of running its own. Containers start in seconds, use little memory, and run the same everywhere, which solves the problem of software that works on one machine but not on another. They use features of Linux, namespaces for isolation and cgroups to limit resources.

Definition: Container. A lightweight, isolated package that holds an application and its dependencies and runs on the shared kernel of a host operating system.

Virtual Machines and Containers

A VM virtualizes the hardware and has a full guest operating system, so it is large, takes minutes to start, and gives strong isolation. A container virtualizes the operating system and shares the kernel, so it is small, starts in seconds, and gives lighter isolation. Many containers can run in one VM. They are often used together, with containers inside VMs.

Docker

Docker is the most popular container platform. A Dockerfile is a text file of instructions to build an image, a read-only template with the application and its dependencies. A container is a running instance of an image. Images are stored in a registry, such as Docker Hub. The Docker Engine builds and runs containers. The basic commands are docker build to make an image, docker run to start a container, docker ps to list containers, and docker push and docker pull to share images.

Kubernetes

Running a few containers is easy. Running thousands across many machines needs a system to manage them, called an orchestrator. Kubernetes (K8s), created by Google, is the standard one. A cluster has a control plane, which makes decisions, and worker nodes, which run the containers. The smallest unit is a pod, which holds one or more containers. A deployment keeps the desired number of pods running, a service gives a steady address and load balancing to a set of pods, and namespaces divide a cluster. Kubernetes gives self-healing, by restarting failed containers, auto-scaling, rolling updates, and load balancing.

Table 2.2: Virtual machines and containers

Feature Virtual machine Container
Virtualizes Hardware Operating system
Size Gigabytes Megabytes
Start time Minutes Seconds
Isolation Strong Lighter
Operating system Own guest OS Shares host kernel

Example 2.2: A simple Dockerfile

Problem: Write a Dockerfile to run a Python script app.py using Python 3.12, and give the commands to build and run it.

Solution:

  1. Start from a Python base image.
  2. Copy the script into the image.
  3. Set the command to run the script.
  4. Build with docker build, then start with docker run.

Result: The Dockerfile below, with the two commands shown after it.

# Dockerfile
FROM python:3.12-slim
WORKDIR /app
COPY app.py .
CMD ["python", "app.py"]

# Commands (in a shell)
# docker build -t myapp .
# docker run myapp

Common mistakes

  • Treating a container as a full virtual machine. It shares the host kernel.
  • Storing important data inside a container, which is lost when the container is removed.
  • Confusing a Docker image, the template, with a container, the running instance.

Key takeaways

  • Containers share the host kernel and are light and fast.
  • Docker builds images from a Dockerfile and runs containers.
  • Kubernetes orchestrates containers across a cluster.
  • A pod is the smallest unit in Kubernetes.

Practice questions

  1. What is a container?
  2. Distinguish a Docker image and a container.
  3. What does Kubernetes do?

Answers to the practice questions

  1. A lightweight, isolated package of an application and its dependencies that shares the host kernel.
  2. An image is a read-only template, and a container is a running instance of an image.
  3. It orchestrates containers across a cluster, with scheduling, self-healing, scaling and load balancing.

Sources

  • Official BCA syllabus Cloud Computing, course code 25BCA405DS03, Unit II