Skip to main content

Containerization & Docker

What Is Containerization?

A method of packaging:

  • application
  • dependencies
  • runtime
  • configs into a single portable unit called a container. Containers run on a shared OS kernel instead of having their own full OS.

Why It Exists

Before containers:

  • dependency conflicts
  • different OS environments
  • manual setup errors
  • hard deployments

Containers solve:

  • environment consistency
  • portability
  • fast deployments
  • easy scaling

Core Components

Image → Blueprint (read-only template)
Container → Running instance of image
Registry → Storage for images (Docker Hub, ECR)
Runtime → Engine that runs containers


How It Works (Concept Flow)

Code → Image → Container → Deploy Anywhere

Build once → Run anywhere.


When to Use Containers

Use when:

  • Microservices
  • APIs
  • Background workers
  • CI/CD pipelines
  • Cloud deployments

Avoid when:

  • heavy GUI apps
  • kernel-level modifications
  • strict security isolation required

What is Docker?

Docker is a tool that packages your entire application into a standardized unit called a container.

The Problem Docker Solves

Developer A:  "It works on my machine!"
Developer B: "Not on mine..."
DevOps: "Why isn't it working on the server?"

Reason: Different OS, different Python versions, different dependencies,
different configurations

Solution: Docker packages EVERYTHING together

Docker Architecture

┌─────────────────────────────────────────────────────┐
│ Your Computer │
│ ┌───────────────────────────────────────────────┐ │
│ │ Docker Engine (Background) │ │
│ │ ┌─────────────┐ ┌─────────────────────┐ │ │
│ │ │ Docker │ │ Running Container │ │ │
│ │ │ Image │ │ ┌────────────────┐ │ │ │
│ │ │ (Recipe) │→ │ │ Your App │ │ │ │
│ │ └─────────────┘ │ │ + Libraries │ │ │ │
│ │ │ │ + OS files │ │ │ │
│ │ │ └────────────────┘ │ │ │
│ │ └─────────────────────┘ │ │
│ └───────────────────────────────────────────────┘ │
│ │
│ You interact via: Docker CLI commands │
│ (docker run, docker build, docker ps, etc.) │
└─────────────────────────────────────────────────────┘


DockerFile Syntax

Here is a concise cheat sheet for common Dockerfile syntax, based on the knowledge base:

InstructionDescriptionExample
FROMSets the base image for subsequent instructions.FROM ubuntu:22.04
RUNExecutes commands in a new layer and commits the results.RUN apt-get update && apt-get install -y curl
CMDSets the default command to run when the container starts.CMD ["echo", "Hello World"]
LABELAdds metadata to an image.LABEL maintainer="you@example.com"
EXPOSEDocuments the port(s) the container listens on.EXPOSE 80
ENVSets environment variables.ENV NODE_ENV=production
ADDCopies files/directories and can extract archives.ADD localfile.txt /app/
COPYCopies files/directories from source to destination.COPY . /app/
ENTRYPOINTConfigures a container to run as an executable.ENTRYPOINT ["python", "app.py"]
VOLUMECreates a mount point for persistent or shared data.VOLUME /data
USERSets the user name or UID to use when running the image.USER appuser
WORKDIRSets the working directory for instructions that follow.WORKDIR /app
ARGDefines a variable that users can pass at build-time.ARG VERSION=1.0
ONBUILDAdds a trigger instruction to be executed later.ONBUILD COPY . /app/src
STOPSIGNALSets the system call signal for stopping the container.STOPSIGNAL SIGKILL
HEALTHCHECKTells Docker how to test a container to check that it is still working.HEALTHCHECK CMD curl --fail [http://localhost:80](http://localhost/)
SHELLOverrides the default shell used by RUN instructions.SHELL ["/bin/bash", "-c"]

Special Syntax

  • Here-Documents: Use EOT syntax in RUN to write multi-line scripts or create inline files.

    RUN <<EOT
    echo "Hello, world"
    EOT
  • Shebang in Here-Documents: Use a shebang (e.g., #!/usr/bin/env python) to specify an interpreter for the here-document.

  • Multi-file Here-Documents: You can use multiple here-documents in a single RUN instruction to create several files.


Docker HUB

DOCKER HUB Docker Hub is a service provided by Docker for finding and sharing container images with your team. Learn more and find images at https://hub.docker.com

  • Login into Docker
    • docker login -u <username>
  • Publish an image to Docker Hub
    • docker push <username>/<image_name>
  • Search Hub for an image
    • docker search <image_name>
  • Pull an image from a Docker Hub
    • docker pull <image_name>

General commands

  • Start the docker demon
    • docker -d
  • Get help with docker. Can also use -help on all subcommands
    • docker --help
  • Display system-wide information
    • docker info
  • Get current installed docker version
    • docker --version

Images

Docker images are a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings.

  • Build an image for a DockerFile
    • docker build -t <image_name> .
  • Build an Image from a Dockerfile without the cache
    • docker build -t <image_name> . –no-cache
  • List local images
    • docker images
  • Delete an Image
    • docker rmi <image_name>
  • Delete all images
    • docker rmi -f $(docker images -q)
    • sudo docker rmi -f $(sudo docker images -q) with force
  • Remove all unused images
    • docker image prune

Containers

A container is a runtime instance of a docker image. A container will always run the same, regardless of the infrastructure. Containers isolate software from its environment and ensure that it works uniformly despite differences for instance between development and staging.

  • Create and run a container from an image, with a custom name:
    • docker run --name <container_name> <image_name>
  • Run a container with and publish a container’s port(s) to the host.
    • docker run -p <host_port>:<container_port> <image_name>
  • Run a container in the background
    • docker run -d <image_name>
  • Start or stop an existing container:
    • docker start|stop <container_name> (or <container_id>)
  • Remove a stopped container:
    • docker rm <container_name>
  • Remove all containers even the stopped one.
    • docker container rm -f $(docker container ls -aq)
    • sudo docker rm -f $(sudo docker ps -aq) with force
  • Open a shell inside a running container:
    • docker exec -it <container_name> sh
  • Fetch and follow the logs of a container:
    • docker logs <container_name>
    • docker logs -f <container_name>
  • To inspect a running container:
    • docker inspect <container_name> (or <container_id>)
  • To list currently running containers:
    • docker ps
  • List all docker containers (running and stopped):
    • docker ps --all
  • View resource usage stats
    • docker container stats

Docker compose

Key/SectionMeaningExample Usage
versionCompose file format versionversion: "3.8"
servicesDefines the containers (services) to runservices:
imageImage to use for the serviceimage: nginx:latest
buildBuild options for creating an image from a Dockerfilebuild: . or build: { context: . }
portsMaps host ports to container portsports: ["8080:80"]
environmentEnvironment variables for the serviceenvironment: ["VAR=value"]
volumesMounts host paths or named volumesvolumes: ["data:/data"]
networksConnects services to custom networksnetworks: ["mynet"]
depends_onSpecifies service startup orderdepends_on: ["db"]
commandOverrides the default commandcommand: ["npm", "start"]
container_nameSets a custom container namecontainer_name: myapp
hostnameSets a custom hostnamehostname: myhost
deployDeployment configuration (for Swarm mode)deploy: { restart_policy: { condition: on-failure } }
restartRestart policy for containersrestart: always

Docker Compose is an orchestration tool used to define and run multi-container applications. This used for project specific.

  • Get current version
    • docker compose version
  • Start all services
    • docker compose up
  • Run started services in background
    • docker compose up -d
  • Stop all services
    • docker compose down
  • Rebuild all images, use after code or dependency changes
    • docker compose up --build
  • Execute a command in running docker compose service
    • docker compose exec [OPTIONS] SERVICE COMMAND [ARGS...]
  • List all containers in your project
    • docker compose ps
  • Display logs output from all services
    • docker compose logs
    • docker compose logs -f to follow
  • Display the running processes for each service container.
    • docker compose top
  • List all images used by current project's containers.
    • docker compose images
  • Prints the public port mapping for a specific service
    • docker compose port [SERVICE] [PRIVATE_PORT]
  • Validates your YAML file and renders the final configuration
    • docker compose config
  • Lists all running compose projects on your system
    • docker compose ls
  • Stops|Start|Restarts|Temporarily freezes services (without stooping) containers.
    • docker compose stop|start|restart|pause|unpause <container_name>
  • Runs a one-off command by creating a new, temporary container for a service
    • docker compose run [SERVICE] [COMMAND]
  • Copies files or folders between a service container and your local file-system.
    • docker compose cp
  • Pulls the latest versions of image defined in your compose file without starting services
    • docker compose pull

Yes, you can create a cheat sheet for Docker Compose YAML file syntax, meanings, and examples. Here’s a concise guide based on the knowledge base, including a sample file and explanations for common keys:


Example: Simple Compose File

version: "3.8"

services:
web:
image: nginx:latest
ports:
- "8080:80"
db:
image: postgres:18
environment:
POSTGRES_USER: example
POSTGRES_DB: exampledb

This file defines two services:

  • web: Runs Nginx and maps port 8080 on the host to port 80 in the container.
  • db: Runs PostgreSQL with environment variables for user and database.

Example: Advanced Compose File

version: "3.8"

services:
app:
build:
context: .
container_name: rest-server
hostname: rest-server
networks:
- mynet
ports:
- "80:8080"
environment:
- PGUSER=${PGUSER:-totoro}
- PGPASSWORD=${PGPASSWORD:?database password not set}
- PGHOST=${PGHOST:-db}
- PGPORT=${PGPORT:-26257}
- PGDATABASE=${PGDATABASE:-mydb}
depends_on:
- db
deploy:
restart_policy:
condition: on-failure

db:
image: cockroachdb/cockroach:latest-v25.4
container_name: roach
hostname: db
networks:
- mynet
ports:
- "26257:26257"
- "8080:8080"
volumes:
- roach:/cockroach/cockroach-data
command: start-single-node --insecure

volumes:
roach:

networks:
mynet:
driver: bridge

This file demonstrates:

  • Multiple services with custom names and hostnames
  • Build context for building images
  • Environment variables with defaults and required values
  • Persistent storage with named volumes
  • Custom networks
  • Service dependencies and restart policies