KAHIBARO
Discord Login Register

21.1. Introduction to Containers

Why Containers Matter for Backend Developers

Containers are a way to package your application with everything it needs so it runs the same way everywhere, from your laptop to production servers.

Traditional deployments often suffer from the “it works on my machine” problem. Your code might rely on specific versions of Python, system libraries, or tools that are not present, or are different, on another machine.

Containers solve this by putting your app and its environment into a single, portable unit.

A container is a lightweight, isolated runtime environment that packages your application code together with its dependencies, but shares the host machine’s operating system kernel.

You will use containers a lot in backend development, especially when:

This chapter focuses on core ideas. Details about Docker, images, and Dockerfiles come later in this section of the course.

Containers vs Virtual Machines

Many beginners confuse containers with virtual machines (VMs). Both isolate applications, but in very different ways.

Conceptual Comparison

FeatureVirtual MachineContainer
IsolationFull OS isolationProcess level isolation
Runs its own kernelYesNo, shares host kernel
Boot timeSlow, often tens of seconds or minutesVery fast, often under a second
Resource usageHeavy, each VM needs full OSLight, many containers per host possible
Typical useRunning many different OSs on one hostRunning many isolated apps on one OS

With a VM, you install a full operating system inside a virtual machine. With containers, you only package your app plus its user space libraries. The kernel is shared.

An analogy:

Container Isolation Basics

Containers rely on operating system features such as:

From inside a container, it looks like you have your own system, but behind the scenes you are just a set of processes on the host with restricted visibility and resources.

Images and Containers: The Core Concepts

You will see two terms all the time: image and container.

Image vs Container

TermWhat it isAnalogy
ImageRead-only template that describes a filesystemClass definition or a cake recipe
ContainerRunning instance created from an imageObject instance or a baked cake

An image includes:

A container is:

Rule: You build images and you run containers from those images.

In Docker commands, for example:

Layered Images

Container images are built in layers. Each step in the build process adds a new layer.

Example conceptually:

  1. Start from official base image: python:3.12-slim
  2. Install system packages
  3. Copy your application code
  4. Install Python dependencies
  5. Set default command

If multiple images share the same base layers, the host can reuse them. This makes images efficient in storage and faster to transfer.

You will see how to define these layers with Dockerfiles in the "Dockerfiles" chapter.

How Containers Help Backend Development

Containers are especially useful for backend work, where many services and dependencies must work together.

Consistent Environments

Backend apps often depend on:

Without containers, setting all this up on each developer’s machine is error prone.

With containers you can:

Example scenario:

Easy Dependency Management

Inside a container you control exactly:

Example:

You are not relying on what is installed on the host machine.

Isolation and Security Benefits

Containers are not a full security boundary like a VM, but they provide isolation that helps:

For backend developers, this means you can:

Basic Container Lifecycle

Even before using Docker commands, it is useful to know the typical lifecycle of a container.

Lifecycle Stages

  1. Build image
    • Define steps in a Dockerfile
    • Run a build command to create an image
  2. Store image
    • Locally on your machine
    • Remotely in a registry (for example Docker Hub, GitHub Container Registry)
  3. Run container
    • Start a new container from the image
    • Attach ports, volumes, environment variables
  4. Stop container
    • Graceful or forced shutdown
  5. Remove container
    • Remove the stopped container if no longer needed
  6. Update image
    • Change code or dependencies, rebuild image, redeploy

Example flow for a FastAPI backend:

  1. Build image: mycompany/task-api:1.0.0
  2. Push image to registry
  3. On server, pull the image and run container
  4. When you release version 1.1.0, build a new image and replace the old container with a new one

Important: Containers are ephemeral. Anything you want to keep permanently (databases, uploaded files, logs) should use volumes or external storage, not the container’s internal filesystem.

Containers and Networking

Backend services need to talk to each other and to the outside world. Containers have their own isolated network view.

Key ideas:

Common pattern:

You will learn concrete Docker networking commands and Docker Compose configuration in later chapters, but keep this idea: containers talk over virtual networks and explicit port mappings.

Containers vs Bare-Metal Deployment

Before containers, many backend deployments were done by:

This approach still works, but has drawbacks:

With containers:

This is why containers are so common in modern backend stacks.

Common Container Use Cases in This Course

Throughout this course you will meet containers in many places.

Databases

You will often run PostgreSQL in a container:

Example usage (conceptual):

Redis and Message Brokers

For caching and background jobs you will use Redis or other brokers. Containers let you:

Application Servers

Your FastAPI app will often be containerized:

Limitations and Misconceptions

Containers are powerful, but they are not magic.

Misconception 1: “Containers are just small virtual machines.”

They are not VMs. They share the host kernel and are typically lighter and faster to start. They are closer to isolated processes than full operating systems.

Misconception 2: “Containers automatically make apps scalable.”

Containers are easy to start and stop, which helps scaling. But you still need:

Misconception 3: “Anything inside the container is permanent.”

If a container is removed, its internal filesystem is lost. Important data must be stored in volumes or external services.

Misconception 4: “Containers are always more secure.”

Containers add isolation, but they also add complexity. Misconfigured images, open ports, or running as root inside the container can still be serious security risks.

How Containers Fit into DevOps and CI/CD

Containers are a key building block for continuous integration and deployment.

Typical workflow:

  1. Developer pushes code to GitHub
  2. CI pipeline builds a container image
  3. Tests run inside containers
  4. On success, image is pushed to a registry
  5. Deployment system pulls the image and runs new containers

This approach has several benefits:

You will see this in detail in the CI/CD and Deployment sections of the course.

Summary

In this chapter you learned that:

Next, you will learn about Docker itself, the most common container platform, and how to build and run your own containers for backend applications.

Views: 8

Comments

Please login to add a comment.

Don't have an account? Register now!