Containers
A container is a lightweight, isolated environment used to run an application together with its required dependencies, libraries, configuration, and runtime.
Exam definition
Containerization is an OS-level virtualization technique in which multiple isolated user-space environments share the same host operating-system kernel.
The most popular container technology is Docker.
1. Basic Idea
Without containers:
Physical Computer ↓ OS ↓ Application ↓ Dependencies
With containers:
Physical Computer ↓ Host OS ↓ Container Engine ┌──────────┼──────────┐ ↓ ↓ ↓ Container Container Container ↓ ↓ ↓ App A App B App C
All containers normally share the host kernel.
2. Container vs Virtual Machine
This is the most important concept.
Virtual Machine
Physical Hardware ↓ Hypervisor ↓ ┌─────┴─────┐ VM VM ↓ ↓ Guest OS Guest OS ↓ ↓ App App
Container
Physical Hardware ↓ Host OS ↓ Container Engine ↓ ┌──────┼──────┐ ↓ ↓ ↓ C1 C2 C3 ↓ ↓ ↓ App App App
The major difference is:
VMs normally include a complete guest OS; containers share the host kernel.
3. Why Containers Are Used
Suppose you build a Python application.
Your application needs:
Python Flask Libraries Environment variables Configuration System dependencies
On your computer it works.
But another computer may have:
Different Python version Different libraries Different OS configuration
and the application may fail.
Containers solve this by packaging the application's environment.
Application + Dependencies + Configuration ↓ Container Image
Then:
Image ↓ Container ↓ Application
4. "Works on My Machine" Problem
Traditional development:
Developer PC ↓ "It works!" ↓ Server ↓ "It doesn't work!"
Containerized development:
Application + Dependencies ↓ Container Image ↓ Development ↓ Testing ↓ Production
The same image can be used across environments, although the host infrastructure still matters.
5. Docker
Docker is a platform for building, distributing, and running containers.
Basic architecture:
Docker CLI ↓ Docker Engine ↓ Container Runtime ↓ Linux Kernel ↓ Hardware
On Docker Desktop, the implementation uses a Linux environment/VM on non-Linux hosts, so the simplified diagram depends on the host OS.
6. Docker Architecture
A simplified Docker environment:
Docker Client │ ↓ Docker Daemon / | \ / | \ ↓ ↓ ↓ Images Containers Networks │ ↓ Volumes
Important components:
- Docker Client
- Docker Engine/Daemon
- Images
- Containers
- Networks
- Volumes
- Registry
7. Docker Client
The Docker CLI allows you to communicate with Docker.
Example:
docker ps
The command asks Docker to show running containers.
Another example:
docker images
shows available local images.
8. Docker Daemon
The Docker daemon manages Docker objects.
It handles operations such as:
- creating containers
- starting containers
- stopping containers
- managing images
- managing networks
- managing volumes
Conceptually:
User ↓ docker command ↓ Docker API ↓ Docker daemon ↓ Container
9. Container Image
A container image is a read-only packaged template used to create containers.
Example:
Python Image ↓ Create container ↓ Python container
An image can contain:
- application code
- runtime
- libraries
- filesystem content
- configuration metadata
10. Container
A container is a running or created instance of an image.
Think:
Image = Blueprint Container = Running instance
Example:
ubuntu:latest ↓ ┌────┴────┐ ↓ ↓ C1 C2
One image can be used to create multiple containers.
11. Image vs Container
| Image | Container |
|---|---|
| Template | Instance |
| Usually immutable/read-only layers | Has a writable container layer |
| Used to create containers | Runs the application |
| Can be stored in registry | Created from image |
Example: ubuntu | Example: ubuntu-container |
Memory trick
Image = Recipe
Container = Prepared Dish
12. Docker Registry
A registry stores container images.
Popular example:
Docker Hub
Conceptually:
Developer ↓ Build Image ↓ Push ↓ Container Registry ↓ Pull ↓ Server ↓ Run Container
Other registries include private organizational registries and cloud-provider registries.
13. Docker Pull
Download an image:
docker pull nginx
This obtains the image from a registry if available.
14. Docker Run
Run a container:
docker run nginx
Docker will generally:
Find image ↓ Download if necessary ↓ Create container ↓ Start container
15. List Containers
Running containers
docker ps
All containers
docker ps -a
Example:
CONTAINER ID IMAGE STATUS abc123 nginx Up def456 ubuntu Exited
16. Start and Stop
Start an existing container:
docker start mycontainer
Stop:
docker stop mycontainer
Restart:
docker restart mycontainer
17. Remove Container
docker rm mycontainer
This removes the container, not necessarily the image it came from.
18. Remove Image
docker rmi nginx
The image can only be removed when Docker's dependency rules allow it.
19. Interactive Container
You can start a shell inside a container:
docker run -it ubuntu bash
Meaning:
-i → interactive -t → terminal
Then:
Host ↓ Docker ↓ Ubuntu container ↓ bash
You can exit with:
exit
20. Detached Mode
To run a container in the background:
docker run -d nginx
-d means detached mode.
Terminal ↓ docker run -d ↓ Container runs in background
21. Port Mapping
Suppose an application inside the container listens on port 5000.
The container's port can be mapped to the host:
docker run -p 8080:5000 myapp
Meaning:
Host port 8080 ↓ Container port 5000
Then:
Browser ↓ localhost:8080 ↓ Container :5000 ↓ Application
Memory trick
HOST:CONTAINER
22. Environment Variables
Applications often need configuration through environment variables.
Example:
docker run -e APP_ENV=production myapp
Inside the container:
APP_ENV=production
Environment variables are commonly used for:
- application mode
- configuration
- service URLs
- credentials/secrets references
Sensitive secrets should preferably use proper secret-management mechanisms rather than hard-coding them into images or Dockerfiles.
23. Container Filesystem
A container has its own filesystem view.
Container ├── / ├── /app ├── /etc ├── /tmp └── ...
Changes to the container's writable layer are generally lost when the container itself is removed.
Therefore persistent data normally needs volumes or bind mounts.
24. Volumes
A Docker volume provides persistent storage managed by Docker.
Container ↓ Volume ↓ Host Storage
Example:
docker volume create mydata
Then:
docker run -v mydata:/data myapp
Now:
Container /data ↕ Docker Volume
25. Why Volumes Are Important
Without persistent storage:
Container ↓ Database data ↓ Container removed ↓ Data may be lost
With a volume:
Container ↓ Volume ↓ Data persists ↓ Container removed ↓ New container ↓ Same data
26. Bind Mount
A bind mount connects a specific host directory to a container directory.
Example:
docker run -v /home/user/project:/app myimage
Conceptually:
Host: ~/project ↕ Container: /app
Useful during development because changes made on the host can immediately appear inside the container.
27. Volume vs Bind Mount
| Volume | Bind Mount |
|---|---|
| Managed by Docker | Uses explicit host path |
| Good for persistent application data | Good for development/source sharing |
| Docker manages location | User controls host location |
| Easier portability | More dependent on host filesystem |
28. Dockerfile
A Dockerfile contains instructions for building an image.
Example:
FROM python:3.14 WORKDIR /app COPY requirements.txt . RUN pip install -r requirements.txt COPY . . CMD ["python", "app.py"]
Conceptual flow:
Dockerfile ↓ docker build ↓ Image ↓ docker run ↓ Container
29. Important Dockerfile Instructions
FROM
Defines the base image.
FROM python:3.14
WORKDIR
Sets the working directory.
WORKDIR /app
COPY
Copies files into the image.
COPY . .
RUN
Executes a command while building the image.
RUN pip install flask
CMD
Defines the default command when the container starts.
CMD ["python", "app.py"]
EXPOSE
Documents a port intended for the application.
EXPOSE 5000
Important:
EXPOSEby itself does not publish the port to the host. Port publishing is usually done withdocker run -p.
30. Docker Image Layers
Images are built in layers.
For example:
┌────────────────────┐ │ Application Code │ ├────────────────────┤ │ Python Packages │ ├────────────────────┤ │ Python Runtime │ ├────────────────────┤ │ Base Linux Layer │ └────────────────────┘
Layers can be reused, which helps make image building and distribution more efficient.
31. Copy-on-Write
Container images generally use layered filesystems.
Conceptually:
Image layers ↓ Read-only ↓ Container writable layer ↓ Changes
If a container modifies a file from a lower image layer, the storage system can use copy-on-write behavior.
32. Container Networking
Containers can communicate through virtual networks.
Basic architecture:
Container 1 │ ↓ Virtual Network ↑ │ Container 2
Common Docker network types include:
- bridge
- host
- none
- overlay
33. Bridge Network
The default networking model for many Docker installations is a bridge network.
Container 1 ─┐ │ Bridge │ Container 2 ─┘
Containers on the same suitable Docker network can communicate with each other.
34. Container DNS
Docker networks can provide service/container name resolution.
For example:
web ↓ app ↓ database
An application can often connect to another container using its service/container name rather than manually tracking an IP address.
This becomes particularly useful with Docker Compose.
35. Docker Compose
Docker Compose is used to define and run multi-container applications.
Example application:
Web Browser ↓ Web App ↓ Backend ↓ Database
Compose can manage:
web container backend container database container network volumes
A typical file is:
compose.yaml
36. Example Web Application
Imagine your Flask application:
Internet ↓ Web Container ↓ Flask Application ↓ Database Container
You can define the services in Compose.
Conceptually:
services: web: ... database: ...
Then:
docker compose up
starts the defined services.
37. Container Lifecycle
A container can move through states such as:
Created ↓ Running ↓ Stopped ↓ Removed
For example:
docker create ↓ docker start ↓ Running ↓ docker stop ↓ Stopped ↓ docker rm ↓ Removed
38. Container Isolation
Containers isolate processes using Linux kernel features such as:
Namespaces
Provide isolated views of system resources.
Examples include namespaces for:
- processes
- networking
- mounts
- users
- hostnames
Conceptually:
Host │ ├── Container 1 │ ├── Process view │ ├── Network view │ └── Filesystem view │ └── Container 2 ├── Process view ├── Network view └── Filesystem view
39. Control Groups — cgroups
Linux cgroups control and account for resource usage.
They can limit/manage:
- CPU
- memory
- processes
- I/O
Example:
Container ↓ cgroup ├── CPU limit ├── Memory limit └── Process limit
So:
Namespaces → isolation
cgroups → resource control
This is an important exam point.
40. Container Runtime
The container engine ultimately relies on container runtime components to create and execute containers.
Examples in modern Linux container ecosystems include:
- containerd
- runc
- CRI-O
Simplified:
Docker CLI ↓ Docker Engine ↓ containerd ↓ runc ↓ Linux kernel
The exact architecture varies by platform and version.
41. Container Security
Containers provide isolation, but they are not automatically secure.
Important security practices:
- use trusted images
- keep images updated
- run applications as non-root when possible
- minimize installed packages
- limit container capabilities
- use resource limits
- isolate networks
- avoid unnecessary host mounts
- protect secrets
- scan images for vulnerabilities
- keep the container runtime updated
42. Root Inside Container
A process running as root inside a container is not equivalent to being unrestricted root on a well-configured host, because container isolation mechanisms exist.
However:
Running containers as root can increase risk.
Prefer a non-root application user when practical.
43. Containers and Microservices
Containers are commonly used with microservices.
Instead of:
One huge application
you might have:
┌──────────────┐ │ Auth Service │ └──────────────┘ ┌──────────────┐ │ User Service │ └──────────────┘ ┌──────────────┐ │ API Service │ └──────────────┘ ┌──────────────┐ │ DB Service │ └──────────────┘
Each service can run in its own container.
44. Containers in DevOps
Containers are important in DevOps because they help standardize environments.
Typical pipeline:
Code ↓ Build Image ↓ Test ↓ Push Image ↓ Deploy ↓ Run Containers
This works well with:
- CI/CD
- cloud platforms
- Kubernetes
- automated deployment
45. Containers and Kubernetes
Docker can run containers, while Kubernetes is a platform for orchestrating containerized workloads across clusters.
Conceptually:
Kubernetes Cluster │ ┌─────┼─────┐ ↓ ↓ ↓ Node Node Node │ │ │ Pods Pods Pods │ Containers
Kubernetes can manage:
- scheduling
- scaling
- service discovery
- rolling deployments
- self-healing
- container orchestration
46. Container vs VM — Detailed Comparison
| Feature | Container | VM |
|---|---|---|
| Virtualization level | OS-level | Hardware/system level |
| Guest kernel | Shares host kernel | Usually separate |
| Size | Usually smaller | Usually larger |
| Startup | Usually fast | Usually slower |
| Resource usage | Lower | Higher |
| Isolation | Process/OS-level | Stronger system-level isolation |
| OS flexibility | Must be compatible with host kernel | Different guest OS possible |
| Portability | High | High |
| Best for | Apps/services | Full OS environments |
| Example | Docker | VirtualBox |
47. Example: Your Web Project
Suppose you have:
Flask Application requirements.txt templates/ static/ app.py
Without Docker:
Linux ↓ Python ↓ pip ↓ Flask ↓ Your App
With Docker:
Dockerfile ↓ Image ↓ Container ↓ Python + Flask + Dependencies + App
The environment becomes much easier to reproduce.
48. Important Docker Commands
Check Docker
docker --version
Show running containers
docker ps
Show all containers
docker ps -a
List images
docker images
Download image
docker pull ubuntu
Run container
docker run ubuntu
Run interactive Ubuntu
docker run -it ubuntu bash
Run in background
docker run -d nginx
Stop
docker stop <container>
Start
docker start <container>
Remove
docker rm <container>
View logs
docker logs <container>
Execute command inside running container
docker exec -it <container> bash
Build image
docker build -t myapp .
Run with port mapping
docker run -p 8080:5000 myapp
49. Complete Container Architecture
APPLICATION │ ↓ Container Image │ ↓ Container │ ┌──────────┴──────────┐ ↓ ↓ Namespaces cgroups Isolation Resource Control │ │ └──────────┬──────────┘ ↓ Container Runtime ↓ Host Kernel ↓ Physical Hardware
50. Most Important Exam Questions
What is a container?
A container is an isolated user-space environment that packages an application and its dependencies while sharing the host operating-system kernel.
What is Docker?
Docker is a platform for building, distributing, and running containerized applications.
What is a container image?
A container image is a packaged, usually immutable template from which containers are created.
Image vs Container?
Image = template; Container = instance created from the image.
What is a Dockerfile?
A Dockerfile contains instructions used to build a container image.
What are namespaces?
Linux namespaces isolate a process's view of system resources.
What are cgroups?
Linux cgroups control and account for resource usage such as CPU and memory.
Container vs VM?
A VM generally virtualizes a complete computer and runs a separate guest OS, while a container provides OS-level isolation and normally shares the host kernel.
🧠 Final Memory Trick
Remember the container architecture as:
IMAGE → CONTAINER → NETWORK → VOLUME → RUNTIME
And for Linux internals:
Namespaces = Isolation
cgroups = Resource Control
And the biggest difference:
VM: Hardware ↓ Hypervisor ↓ Guest OS ↓ Application Container: Hardware ↓ Host OS Kernel ↓ Container Runtime ↓ Container ↓ Application