Introduction to Containers
1. Traditional Computing Challenges in Software Development
Traditional computing environments pose several issues that hinder efficient software development and deployment:
- Lack of Application Isolation: Applications running on physical servers can interfere with each other due to shared resources.
- Resource Underutilization/Overutilization: Physical servers are either underused or overloaded, leading to poor ROI and performance bottlenecks.
- High Provisioning & Maintenance Costs: Setting up infrastructure is time-consuming and expensive.
- Limited Scalability: On-premises setups often lack the flexibility to scale rapidly during peak loads.
- Poor Portability: Applications built on one OS or environment may not work seamlessly in another.
- Manual Deployment Processes: Automation is difficult, especially when deploying across multiple platforms.
2. What Are Containers?
Containers are lightweight, isolated, portable units of software that encapsulate an application along with its runtime dependencies (libraries, binaries, system tools, and configuration files). They allow developers to build, ship, and run applications consistently across different computing environments.
Key Characteristics of Containers
- Lightweight: Containers share the host OS kernel and do not require a full OS per instance.
- Isolated: Each container runs in its own user space, preventing interference between apps.
- Portable: Can run on any platform supporting the container engine (cloud, desktop, on-prem).
- Consistent: Ensure “it works on my machine” doesn’t become a problem.
- Fast: Start almost instantly and use minimal memory.
3. Container Engine Functionality
A container engine virtualizes the operating system and manages the lifecycle of containers. It ensures that all required components (code, runtime, libraries) are bundled together and executed in isolation.
Examples:
- Docker
- Podman
- LXC
- Vagrant
4. Benefits of Containerization
Benefit | Description |
---|---|
Faster Deployment | Automate builds and deployments for rapid release cycles. |
Improved Resource Utilization | Multiple containers can run on a single host, maximizing CPU and memory usage. |
Portability | Run consistently across Windows, Linux, Mac, cloud providers, etc. |
Support for Modern Architectures | Ideal for microservices, CI/CD pipelines, and cloud-native applications. |
Environment Consistency | Eliminates discrepancies between dev, test, staging, and production environments. |
5. Container Use Cases
- Microservices architecture
- Continuous Integration / Continuous Delivery (CI/CD)
- Hybrid and multi-cloud deployments
- Scalable web services
- Data processing pipelines
6. Container Challenges
Despite their benefits, containers also introduce challenges:
- Security Risks: Vulnerabilities in the host OS can affect all containers.
- Container Management Complexity: Managing thousands of containers at scale requires orchestration tools like Kubernetes.
- Legacy App Migration: Refactoring monolithic apps into containers can be complex and time-consuming.
- Right-Sizing: Determining optimal resource allocation for each container without over or under-provisioning.
7. Popular Container Vendors & Tools
Vendor | Features |
---|---|
Docker | Most popular container platform; easy-to-use CLI and ecosystem support. |
Podman | Daemon-less, more secure alternative to Docker; integrates well with systemd. |
LXC / LXD | Full system containers ideal for data-intensive applications and long-running processes. |
Vagrant | Focuses on development environments; offers high levels of isolation using VMs or containers. |
8. Containers vs Virtual Machines (VMs) (Advanced Note)
Feature | Containers | Virtual Machines |
---|---|---|
OS Dependency | Share host OS kernel | Run full guest OS |
Startup Time | Milliseconds | Seconds |
Resource Usage | Lightweight | Heavyweight |
Isolation Level | Process-level | Kernel-level |
Use Case | Microservices, CI/CD | Legacy apps, full OS environments |
9. Role in Cloud-Native Development
Containers are foundational to cloud-native development, enabling:
- Modularity (via microservices)
- Resilience and scalability
- Automated deployment via orchestration tools
- Infrastructure as code (IaC) integration
They align perfectly with modern practices such as GitOps, CI/CD, Infrastructure as Code (IaC), and immutable infrastructure.
10. Conclusion
Containers have revolutionized how software is developed, deployed, and managed. They provide a consistent, lightweight, and scalable solution for overcoming many limitations of traditional computing models. However, successful adoption requires addressing security, management, and architectural considerations.
As organizations move toward DevOps maturity, embracing containerization—alongside orchestration tools like Kubernetes—is essential for building robust, agile, and future-proof systems.
Further Reading & Tools
- Docker Hub: https://hub.docker.com
- Kubernetes: Orchestration for managing containerized applications at scale.
- Helm Charts: Package managers for Kubernetes deployments.
- OCI (Open Container Initiative): Standard for container formats and runtime.
Introduction to Docker
1. What is Docker?
Docker is an open-source platform introduced in 2013 that enables developers to develop, ship, and run applications inside containers — lightweight, isolated, and portable environments.
Key Features of Docker
- Built using the Go (Golang) programming language.
- Leverages Linux kernel features such as namespaces and cgroups for container isolation and resource control.
- Provides a consistent environment across development, testing, staging, and production.
2. Docker Architecture Overview
Components of Docker Engine
Component | Description |
---|---|
Docker Daemon (dockerd ) | Background process managing images, containers, networks, and storage. |
Docker Client (docker CLI) | Interface used by users or scripts to interact with the daemon. |
REST API | Enables interaction between clients and daemons via HTTP endpoints. |
Underlying Technologies
- Namespaces: Isolate processes, networking, user IDs, etc., within each container.
pid
,net
,mnt
,uts
,ipc
,user
namespaces
- Control Groups (cgroups): Limit and monitor resource usage (CPU, memory, disk I/O).
- Union File Systems (UnionFS): Layers filesystems to build efficient image layers and copy-on-write behavior.
3. The Docker Process Flow
- Develop Application Locally
- Create a Dockerfile – Defines the image structure.
- Build Image – Using
docker build
, creates a reusable image. - Push Image – To a registry like Docker Hub or private registry.
- Pull Image – On another machine/environment.
- Run Container – Using
docker run
to instantiate the image into a running container. - Orchestrate & Scale – Using Docker Compose or Kubernetes for multi-container apps and scaling.
4. Docker Ecosystem & Tools
Tool | Description |
---|---|
Docker CLI | Command-line interface to manage Docker objects. |
Docker Compose | Define and run multi-container Docker apps using a YAML file. |
Docker Swarm | Native clustering and orchestration tool for Docker. |
Docker Hub / Registry | Centralized repository for Docker images. |
Plugins & Extensions | Support for volume drivers, network plugins, logging drivers, etc. |
Prometheus | Monitoring integration for container metrics. |
5. Benefits of Docker Containers
Benefit | Description |
---|---|
Consistent Environments | ”It works on my machine” problem eliminated. |
Fast Deployment | Containers start in seconds due to shared OS kernel. |
Lightweight Images | Use copy-on-write and layering to keep size minimal. |
Improved CI/CD Pipeline | Integrates seamlessly with automation tools. |
Version Control | Supports tagging, rollback, and versioned deployments. |
Portability | Run on any system supporting Docker—local, cloud, hybrid. |
Modular Development | Ideal for microservices-based architecture. |
6. Docker in DevOps Practices
Docker supports modern DevOps methodologies, including:
- Agile Development: Rapid iteration and deployment cycles.
- CI/CD Pipelines: Automated builds, tests, and deployments using Docker images.
- Immutable Infrastructure: Deploy new versions without modifying existing containers.
- GitOps: Sync infrastructure and application state with Git repositories using Helm + Kubernetes.
7. Challenges of Docker Containers
Challenge | Description |
---|---|
Security Concerns | Shared OS kernel can expose vulnerabilities; requires hardening and least privilege policies. |
Stateful Applications | Docker is better suited for stateless apps; persistent data needs external volumes or StatefulSets in Kubernetes. |
Performance Overhead | Not ideal for high-performance computing (HPC) or GPU-intensive tasks. |
Monolithic Applications | Harder to containerize and scale monoliths compared to microservices. |
GUI-Based Applications | Docker is not optimized for rich GUI apps like desktop software. |
Container Sprawl | Managing many containers at scale becomes complex without orchestration tools. |
8. When NOT to Use Docker
Avoid Docker if your application:
- Requires high performance or low-latency execution.
- Depends heavily on native hardware access.
- Is a monolithic legacy app difficult to modularize.
- Uses rich GUI interfaces (e.g., traditional desktop apps).
- Has strict security compliance requirements without proper hardening.
9. Real-World Use Cases
Use Case | Description |
---|---|
Microservices Architecture | Each service runs in its own container, independently scalable. |
CI/CD Automation | Standardized test/build/deploy pipelines using Docker images. |
Hybrid Cloud Deployments | Consistent environments across on-premises and cloud platforms. |
Local Development Environments | Replicate production setup locally using Docker Compose. |
Service Mesh Integration | Combine with Istio or Linkerd for advanced networking and observability. |
10. Conclusion
Docker has transformed how modern applications are developed, deployed, and maintained. Its ability to provide consistent, isolated, and portable runtime environments makes it a cornerstone of cloud-native and DevOps practices.
However, successful implementation requires understanding its strengths and limitations, especially when scaling, managing security, or integrating with orchestration tools like Kubernetes.
Further Reading & Resources
- Docker Documentation
- Docker Hub
- Docker Compose Reference
- OCI (Open Container Initiative)
- Kubernetes + Docker Integration
Building and Running Containers with Docker
1. Container Lifecycle Overview
The process of building and running containers follows a structured lifecycle:
- Create a Dockerfile – Defines the blueprint for your container image.
- Build an Image – Using
docker build
from the Dockerfile. - Run a Container – Instantiate the image into a running container using
docker run
. - Manage Images & Containers – Use commands like
docker images
,docker ps
,docker push
, anddocker pull
.
2. Dockerfile: The Blueprint of a Container
A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.
Sample Dockerfile
FROM ubuntu:latest
CMD ["echo", "Hello World"]
Key Dockerfile Instructions
Instruction | Purpose |
---|---|
FROM | Specifies the base image (e.g., Ubuntu, Alpine). |
RUN | Executes commands during the build phase (e.g., installing packages). |
CMD | Default command executed when running the container. |
ENTRYPOINT | Configures a container to run as an executable. |
COPY / ADD | Copies files/directories into the image. |
WORKDIR | Sets the working directory for subsequent instructions. |
EXPOSE | Documents which ports the container listens on. |
ENV | Sets environment variables. |
3. Building a Docker Image
Command Syntax
docker build [OPTIONS] PATH
Example Command
docker build -t my-app:v1 .
-t
→ Tags the image with a name and version..
→ Build context (current directory).
What Happens During Build?
- Docker reads the Dockerfile.
- Each instruction creates a new layer in the image.
- Layers are cached for faster rebuilds.
- Final output: A tagged image ready to be run or pushed to a registry.
Verify Image Creation
docker images
This lists all local images including:
- Repository name
- Tag
- Image ID
- Created date
- Size
4. Running a Container
Command Syntax
docker run [OPTIONS] IMAGE[:TAG]
Example Command
docker run my-app:v1
This:
- Creates a new container instance from the image.
- Runs the default command (
CMD
in Dockerfile). - Outputs:
Hello World
5. Managing Containers
Common Docker CLI Commands
Command | Description |
---|---|
docker build | Builds an image from a Dockerfile. |
docker run | Runs a new container from an image. |
docker ps | Lists running containers (-a shows stopped ones). |
docker stop <container> | Gracefully stops a running container. |
docker rm <container> | Removes a stopped container. |
docker images | Lists all available images. |
docker rmi <image> | Removes an image. |
docker pull <image> | Pulls an image from a registry. |
docker push <image> | Pushes a locally tagged image to a registry. |
docker inspect | Shows detailed info about a container/image. |
docker logs <container> | Displays logs from a running/stopped container. |
6. Advanced Tips for Building Efficient Images
Best Practices
- Use multi-stage builds to reduce final image size.
- Prefer
COPY
overADD
unless you need archive extraction. - Combine
RUN
commands to minimize layers. - Use
.dockerignore
to exclude unnecessary files from build context. - Always tag images meaningfully (
<app-name>:<version>
or<commit-hash>
).
Multi-Stage Build Example
# Stage 1: Build
FROM golang:1.20 AS builder
WORKDIR /app
COPY . .
RUN go build -o myapp
# Stage 2: Runtime
FROM alpine:latest
COPY --from=builder /app/myapp /myapp
CMD ["/myapp"]
7. Real-World Workflow Example
Step-by-step CI/CD Integration
- Developer commits code to Git.
- CI pipeline triggers
docker build
. - Tests are run inside the container.
- If tests pass,
docker push
to registry (e.g., Docker Hub, ECR). - Deployment system pulls image and runs it via
docker run
.
8. Troubleshooting Containers
- Check running containers:
docker ps
- Inspect container details:
docker inspect <container_id>
- View logs:
docker logs <container_id>
- Enter a running container:
docker exec -it <container_id> sh
9. Summary
Topic | Key Points |
---|---|
Dockerfile | Blueprint defining how to build an image. |
Image | Immutable template built from Dockerfile. |
Container | Running instance of an image. |
Commands | build , run , images , ps , pull , push |
Use Case | Automate application packaging, testing, and deployment. |
Best Practices | Multi-stage builds, layer optimization, meaningful tagging. |
Introduction to Docker Objects
1. Overview of Docker Objects
Docker uses several core objects to manage and run applications in containers. These include:
Object | Description |
---|---|
Dockerfile | A text file containing instructions for building an image. |
Image | A read-only template with instructions for creating a container. |
Container | A runnable instance of an image. |
Network | Enables communication between containers and the outside world. |
Volume / Bind Mount | Manages persistent or shared data between containers and host. |
Plugin | Extends Docker functionality (e.g., networking, storage drivers). |
2. Dockerfile: The Foundation of an Image
A Dockerfile is a blueprint used to build a Docker image.
Essential Dockerfile Instructions
Instruction | Purpose |
---|---|
FROM | Specifies the base image (must be the first instruction). |
RUN | Executes commands during image build (e.g., installing packages). |
CMD | Sets the default command to run when the container starts (only the last CMD is effective). |
ENTRYPOINT | Configures the container to run as an executable. |
COPY | Copies files from the host into the image. |
ADD | Similar to COPY , but supports remote URLs and automatic unpacking of archives. |
WORKDIR | Sets the working directory for subsequent instructions. |
EXPOSE | Documents which ports the container listens on (does not publish them). |
ENV | Sets environment variables inside the image/container. |
⚠️ Best Practice: Use
.dockerignore
to exclude unnecessary files from the build context.
3. Docker Images: Read-Only Templates
An image is a static, read-only snapshot that includes:
- Application code
- Runtime dependencies
- Libraries and binaries
- Configuration files
Image Layers
- Each instruction in the Dockerfile creates a new layer.
- Layers are cached — only changed layers rebuild, improving efficiency.
- Multiple images can share common layers, saving disk space and network bandwidth.
Image Naming Convention
Format:
[<registry-host>:<port>/]<repository>:<tag>
Example:
docker.io/ubuntu:18.04
Part | Description |
---|---|
docker.io | Hostname of the registry (e.g., Docker Hub) |
ubuntu | Repository name (group of related images) |
18.04 | Tag indicating version or variant |
📌 If the registry is omitted (e.g.,
nginx:latest
), Docker defaults to Docker Hub (docker.io
).
4. Containers: Runnable Instances of Images
A container is a live, running instance of a Docker image.
Key Features of Containers
- Writable Layer: On top of the read-only image layers, allowing runtime changes.
- Isolation: Containers are isolated from each other and the host OS.
- Portability: Can be moved across environments without configuration changes.
Common Container Operations
Command | Description |
---|---|
docker run | Creates and starts a container. |
docker stop | Gracefully stops a running container. |
docker start | Starts a stopped container. |
docker rm | Removes a container. |
docker exec | Runs a command inside a running container. |
5. Docker Networking: Communication Between Containers
Docker provides virtual networks to allow containers to communicate securely.
Types of Docker Networks
Network Type | Description |
---|---|
Bridge (Default) | Default network where containers connect unless otherwise specified. |
Host | Uses the host’s network stack directly (less isolation). |
Overlay | Enables multi-host networking (used with Docker Swarm or Kubernetes). |
None | Disables all networking for a container. |
Managing Networks
docker network create my-network
docker network ls
docker network inspect my-network
You can attach a container to one or more networks at runtime:
docker run --network=my-network my-app
6. Docker Storage: Managing Persistent Data
By default, data inside a container is ephemeral — it’s lost when the container is removed.
Persistent Data Options
Option | Description |
---|---|
Volumes | Managed by Docker; best for persistent data (e.g., databases). |
Bind Mounts | Mounts a file or directory from the host machine into the container. |
tmpfs Mounts | Stores data in memory only (not persisted to disk). |
Using Volumes
docker volume create my-data
docker run -v my-data:/app/data my-app
This mounts the volume my-data
into /app/data
inside the container.
7. Docker Plugins & Add-ons
Plugins extend Docker’s capabilities beyond its core features.
Popular Plugin Types
Plugin Type | Example Use Case |
---|---|
Storage Plugins | Connect to external storage like AWS EBS, NFS, etc. |
Network Plugins | Enable custom networks using tools like Weave or Calico. |
Authorization Plugins | Enforce access control policies. |
Logging Plugins | Send logs to external systems like Fluentd or Datadog. |
Plugins are usually managed via the Docker CLI or API and can be installed dynamically.
8. Summary of Docker Objects
Object | Function |
---|---|
Dockerfile | Blueprint for building an image. |
Image | Immutable, read-only template built from a Dockerfile. |
Container | Running instance of an image with a writable layer. |
Network | Isolates and manages container communication. |
Volume / Mount | Persists data beyond container lifecycle. |
Plugin | Extends Docker functionality (storage, logging, networking, etc.). |
9. Best Practices
- Always use meaningful tags for images (e.g.,
myapp:1.0
,myapp:commit-abc123
). - Minimize layers by combining
RUN
commands. - Use
.dockerignore
to avoid including unnecessary files in the build context. - Prefer named volumes over bind mounts for production workloads.
- Use multi-stage builds to reduce image size.
- Secure your containers using user namespaces and least privilege principles.
Docker Architecture: A Comprehensive Overview
1. Docker Architecture Overview
Docker follows a client-server architecture, enabling developers to build, run, and manage containers across various environments.
Core Components of Docker Architecture
Component | Description |
---|---|
Docker Client | Interface used by users or applications (CLI, REST API) to send commands to the Docker daemon. |
Docker Host (Server) | Contains the Docker Daemon (dockerd ), which processes client requests and manages containers, images, networks, and storage. |
Docker Registry | Centralized service where container images are stored and distributed (e.g., Docker Hub, private registries). |
2. Docker Client
The Docker Client is the primary interface for interacting with Docker.
Key Features
- Accepts user input via:
- Command Line Interface (CLI) – e.g.,
docker run
,docker build
- REST API – used by tools like CI/CD pipelines or GUI apps
- Command Line Interface (CLI) – e.g.,
- Sends commands to the Docker Daemon for execution.
- Can connect to:
- Local Docker Daemon (same machine)
- Remote Docker Daemon (over network)
🛠 Example CLI Command:
docker run hello-world
3. Docker Host (Server)
The Docker Host is where containers actually run. It runs the Docker Daemon (dockerd
).
Main Responsibilities of Docker Daemon
- Listens for API requests from clients.
- Manages Docker objects such as:
- Images
- Containers
- Networks
- Volumes
- Plugins
- Handles low-level operations like building, running, and distributing containers.
Key Technologies Used by Docker Daemon
Technology | Purpose |
---|---|
Namespaces | Provides isolation for processes, networking, mounts, etc. |
Control Groups (cgroups) | Limits and monitors resource usage (CPU, memory, disk I/O). |
Union File Systems (UnionFS) | Enables layered image builds and copy-on-write functionality. |
Storage Drivers | Manages how layers are stacked and stored (e.g., overlay2 , aufs ). |
4. Docker Registry
A registry is a centralized location where Docker images are stored and shared.
Types of Registries
Type | Description |
---|---|
Public Registry | E.g., Docker Hub, accessible to everyone. |
Private Registry | Secured registry hosted internally or by third-party cloud providers (e.g., AWS ECR, Azure ACR, IBM Cloud Container Registry). |
Image Lifecycle in the Registry
- Build: Developer creates an image using
docker build
. - Tag: Assign a name and version using
docker tag
. - Push: Upload image to registry using
docker push
. - Pull: Retrieve image from registry using
docker pull
.
📦 Example:
docker build -t my-app:v1 .
docker tag my-app:v1 registry.example.com/myteam/my-app:v1
docker push registry.example.com/myteam/my-app:v1
5. Containerization Process Flow
Here’s a step-by-step breakdown of how Docker builds and runs a container:
Step 1: Build Image
- Use a Dockerfile to define image structure.
- Run
docker build
to create a new image.
Step 2: Tag & Push Image
- Tag the image with a name and optional version.
- Push it to a local or remote registry using
docker push
.
Step 3: Pull Image on Target Machine
- If the image doesn’t exist locally, use
docker pull
to fetch it from the registry.
Step 4: Run Container
- Execute
docker run <image-name>
to start a container from the image.
Visual Workflow
[Developer Machine]
↓ (docker build)
[Docker Image Created]
↓ (docker push)
[Registry - Store Image]
↓ (docker pull)
[Production / Deployment Machine]
↓ (docker run)
[Running Container]
6. Communication Between Docker Components
Client ↔ Daemon
- Communication happens via:
- Local Unix socket (default)
- TCP over network (for remote hosts)
- Uses Docker CLI or Docker SDKs/APIs.
Daemon ↔ Registry
- Pulls and pushes images from/to public or private registries.
- Authenticates when accessing secured registries.
Daemon ↔ Daemon (Swarm Mode)
- In Docker Swarm or Kubernetes environments, daemons communicate with each other to manage services and orchestrate containers.
7. Docker Object Management
The Docker host manages several types of objects:
Object | Description |
---|---|
Images | Templates used to create containers. |
Containers | Running instances of images. |
Networks | Virtual networks for container communication. |
Volumes | Persistent storage for data. |
Plugins | Extend Docker’s capabilities (networking, logging, storage). |
8. Summary Table
Component | Function |
---|---|
Docker Client | Interface for sending commands (CLI or API). |
Docker Daemon (dockerd ) | Processes commands and manages containers. |
Docker Registry | Stores and distributes container images. |
Containerization Process | Build → Tag → Push → Pull → Run |
Underlying Tech | Namespaces, cgroups, UnionFS, Storage Drivers |
Networking | Supports bridge, host, overlay, none |
Persistent Storage | Volumes, bind mounts, tmpfs mounts |
Extensibility | Plugins for storage, logging, networking |
9. Best Practices
- Always tag images meaningfully (e.g.,
app:1.0
,app:commit-hash
). - Use
.dockerignore
to reduce image size. - Prefer multi-stage builds to minimize final image footprint.
- Secure your registry access with authentication and encryption.
- Monitor and log containers using centralized systems (e.g., Prometheus + Grafana, ELK stack).
Review of Docker Concepts and Understanding a Dockerfile
📚 Overview
Docker is a powerful platform that enables developers to build, ship, and run applications using containers — lightweight, isolated, and portable environments.
This guide will walk you through essential Docker concepts and how to understand and write a Dockerfile, the blueprint for building Docker images.
🧱 Key Docker Concepts
Term | Description |
---|---|
Dockerfile | A text file containing instructions to build a Docker image. |
Image | A read-only template used to create containers. Built from a Dockerfile. |
Container | A running instance of an image. Isolated and lightweight. |
Registry | A storage system for Docker images (e.g., Docker Hub, AWS ECR). |
Local Machine | Where you build and test your Docker images before pushing to a registry. |
Volume / Bind Mount | Used to persist data beyond the container’s lifecycle. |
🛠️ Understanding a Dockerfile
A Dockerfile defines how to assemble a Docker image. Each instruction in a Dockerfile creates a new layer in the image.
✅ Sample Dockerfile (Node.js App)
# Use the official Node.js image as the base image
FROM node:14
# Set environment variables
ENV NODE_ENV=production
ENV PORT=3000
# Set the working directory
WORKDIR /app
# Copy package.json and package-lock.json files
COPY package*.json ./
# Install dependencies
RUN npm install --production
# Copy the rest of the application code
COPY . .
# Add additional file
ADD public/index.html /app/public/index.html
# Expose the port on which the application will run
EXPOSE $PORT
# Specify the default command to run when the container starts
CMD ["node", "app.js"]
# Labeling the image
LABEL version="1.0"
LABEL description="Node.js application Docker image"
LABEL maintainer="Your Name"
# Healthcheck to ensure the container is running correctly
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 CMD curl -fs http://localhost:$PORT || exit 1
# Set a non-root user for security purposes
USER node
📋 Breakdown of Dockerfile Instructions
Instruction | Purpose | Example |
---|---|---|
FROM | Sets the base image. Must be the first line. | FROM node:14 |
ENV | Sets environment variables inside the container. | ENV PORT=3000 |
WORKDIR | Sets the working directory for subsequent commands. | WORKDIR /app |
COPY | Copies files from host to image. | COPY package*.json ./ |
RUN | Executes commands during image build. | RUN npm install --production |
ADD | Similar to COPY but supports URLs and archive extraction. | ADD public/index.html /app/ |
EXPOSE | Documents which ports the container listens on. | EXPOSE $PORT |
CMD | Default command executed when container runs. Only one takes effect. | CMD ["node", "app.js"] |
LABEL | Adds metadata like version or maintainer info. | LABEL maintainer="John Doe" |
HEALTHCHECK | Checks if the container is healthy at runtime. | HEALTHCHECK CMD curl ... |
USER | Switches to a non-root user for security. | USER node |
🔁 Docker Build & Run Process Flow
- Write Dockerfile: Define all steps needed to build the image.
- Build Image:
docker build -t my-node-app:1.0 .
- Tag Image: Optionally tag it for a specific registry.
docker tag my-node-app:1.0 registry.example.com/myteam/my-node-app:1.0
- Push to Registry: Share with others or deploy elsewhere.
docker push registry.example.com/myteam/my-node-app:1.0
- Pull Image: On another machine/environment.
docker pull registry.example.com/myteam/my-node-app:1.0
- Run Container:
docker run -p 3000:3000 registry.example.com/myteam/my-node-app:1.0
🛡️ Best Practices for Writing Dockerfiles
- Use
.dockerignore
to exclude unnecessary files from the build context. - Combine RUN commands where possible to reduce image layers.
- Use multi-stage builds to minimize final image size.
- Avoid running as root by using the
USER
directive. - Label images for better traceability.
- Include health checks to monitor container status.
- Keep Dockerfiles clean and readable with comments.
📦 Summary
Concept | Description |
---|---|
Dockerfile | Blueprint for building Docker images. |
Image | Read-only snapshot built from Dockerfile. |
Container | Running instance of an image. |
Registry | Centralized location to store and share images. |
Docker Commands | build , run , push , pull , images , ps , etc. |
Security | Always use a non-root user and keep images minimal. |
Extensibility | Use labels, health checks, and plugins to enhance functionality. |
Hands On Lab: Pull, Build and Push the image to Docker Hub
🧾 Summary: Understanding the Benefits of Containers
✅ What is a Container?
- A container is a lightweight, standalone, and executable package that includes everything needed to run an application:
- Code
- Runtime
- Libraries
- Environment variables
- Configuration files
Containers allow applications to run reliably across different computing environments.
🔧 Key Features & Benefits of Containers
Feature | Benefit |
---|---|
Isolation | Each container runs independently. Processes inside one don’t interfere with others. |
Portability | Run the same container on your laptop, testing server, or production cloud. |
Lightweight | Uses fewer resources than virtual machines (shares OS kernel). |
Fast Startup | Starts in seconds, ideal for dynamic scaling. |
Consistency | ”It works on my machine” problem is eliminated. |
🚀 Why Use Containers?
- Faster Deployment: Build once, deploy anywhere.
- Cost Efficiency: Better resource use means lower infrastructure costs.
- Automation & CI/CD: Easily integrate into DevOps pipelines.
- Microservices Architecture: Ideal for building scalable, distributed systems.
- Scalability: Quickly spin up or down containers based on demand.
🐳 About Docker
- Docker is the most popular platform for developing, shipping, and running containerized applications.
- It provides tools like:
docker build
– To create imagesdocker run
– To start containersdocker push/pull
– To share images via registries like Docker Hub
Docker Architecture Overview:
- Docker Client: Where you type commands (
docker run
,docker build
) - Docker Host: Runs the Docker daemon, manages containers, images, networks, volumes
- Registry: Central repository (like Docker Hub) to store and share images
⚠️ When Not to Use Docker
- Monolithic Applications: Harder to manage if not designed for modularity
- High-Performance Needs: Some overhead due to abstraction layer
- Security-Critical Systems: Containers are less isolated than VMs (unless hardened)
💾 Data Management in Docker
- Volumes: Preferred method for persisting data outside a container’s lifecycle
- Bind Mounts: Mount a file or directory from the host machine into a container
- tmpfs Mounts: Store data only in memory (not persisted)
🔌 Networking in Docker
- Containers communicate through networks
- Docker supports:
- Default networks
- Custom bridge networks
- Overlay networks (for multi-host setups)
🧩 Extensibility with Plugins
- Docker supports plugins for:
- Storage drivers (e.g., connect to AWS S3 or Azure Blob Storage)
- Network drivers
- Authorization plugins
📌 Quick Recap Table
Topic | Key Points |
---|---|
What is a container? | Lightweight, portable, self-contained environment |
Why use containers? | Speed, consistency, scalability, cost savings |
Docker components | Client, Host (daemon, images, containers), Registry |
Container vs VM | Containers share OS; VMs emulate hardware |
Data persistence | Volumes, bind mounts |
Networking | Isolated by default; customizable |
Best use cases | Microservices, CI/CD, cloud-native apps |
Not suitable for | Monolithic apps, high-performance/security needs |
🐳 Docker CLI Cheat Sheet
🛠️ Image Management
Command | Description |
---|---|
docker build . | Builds an image from the Dockerfile in current directory |
docker build -t <name>:<tag> . | Builds and tags the image (e.g., myapp:latest ) |
docker images | Lists all local Docker images |
docker rmi <image> | Removes one or more images |
docker pull <image> | Pulls an image from a registry (like Docker Hub) |
docker push <image> | Pushes an image to a registry |
docker tag <source> <target> | Tags an image (e.g., before pushing to Docker Hub) |
▶️ Container Management
Command | Description |
---|---|
docker run <image> | Runs a new container from an image |
docker run -d <image> | Runs container in detached mode (background) |
docker run -p <host-port>:<container-port> <image> | Maps port from host to container |
docker ps | Lists running containers |
docker ps -a | Lists all containers (including stopped ones) |
docker stop <container> | Stops a running container |
docker stop $(docker ps -q) | Stops all running containers |
docker rm <container> | Removes one or more stopped containers |
docker rm $(docker ps -aq) | Removes all containers (stopped or exited) |
docker logs <container> | Shows logs of a container |
🔍 General Info & Troubleshooting
Command | Description |
---|---|
docker --version | Shows Docker CLI version |
docker info | Displays system-wide information about Docker |
docker inspect <container/image> | Shows detailed configuration of a container or image |
docker exec -it <container> sh | Opens a shell inside a running container |
curl localhost:<port> | Tests a locally running web service |
ls | Lists files in current directory |
exit | Exits a container shell session |
💡 Bonus: IBM Cloud Container Registry Commands
Make sure you’re logged in with
ibmcloud login
first.
Command | Description |
---|---|
ibmcloud cr login | Logs Docker CLI into IBM Cloud Container Registry |
ibmcloud cr images | Lists images in your IBM Cloud registry |
ibmcloud cr namespaces | Views available namespaces |
ibmcloud cr region-set <region> | Sets the region for image operations |
ibmcloud target | Shows current IBM Cloud account/target info |
ibmcloud version | Shows IBM Cloud CLI version |
🧠 Environment Variables
Command | Description |
---|---|
export MY_NAMESPACE=my-namespace | Sets an environment variable for use in tagging images |
📦 Git Integration
Command | Description |
---|---|
git clone <url> | Clones a Git repository containing Docker artifacts |
✅ Quick Tip: Clean Up Unused Resources
# Stop and remove all containers
docker stop $(docker ps -aq)
docker rm $(docker ps -aq)
# Remove all unused images, networks, and build caches
docker system prune -a
📘 Glossary: Container Basics
Term | Definition |
---|---|
Agile | An iterative approach to project management and software development that helps teams deliver faster and with fewer issues. |
Client-server architecture | A distributed structure where tasks are divided between clients (requesters) and servers (providers). |
Container | A standard unit of software that encapsulates application code, runtime, tools, libraries, and settings. Built using a container engine like Docker. |
Container Registry | A service used to store and distribute container images. Basic functions include storing and retrieving images. |
CI/CD pipelines | Automated processes that help developers build, test, and deploy applications quickly and reliably. Stands for Continuous Integration / Continuous Deployment. |
Cloud Native | Applications designed specifically to run in cloud environments, taking full advantage of scalability, resilience, and automation. |
Daemon-less | A type of container runtime that does not rely on a background process (daemon) to manage containers or images. |
DevOps | A set of practices, tools, and cultural philosophies that automate and integrate processes between software development and IT operations. |
Docker | An open platform for developing, shipping, and running applications in containers. |
Dockerfile | A text file containing instructions to build a Docker image automatically. |
Docker Client | The primary interface through which users interact with Docker via CLI commands like docker run . |
Docker CLI (Command Line Interface) | Tool used to send commands like build, run, stop to a Docker daemon. |
Docker Daemon (dockerd ) | Background process that manages Docker objects like images, containers, networks, and volumes. |
Docker Hub | A public registry where developers can find, share, and manage container images. |
Docker Localhost | Refers to a container sharing the host’s network stack, so localhost inside the container refers to the host machine. |
Docker Remote Host | A remote machine running Docker Engine that can be accessed via its exposed API port. |
Docker Networks | Used to isolate and manage communication between containers. |
Docker Plugins | Extend Docker functionality — e.g., storage plugins connect to external platforms like AWS or Azure. |
Docker Storage | Uses volumes and bind mounts to persist data beyond the life of a container. |
LXC (LinuX Containers) | An OS-level virtualization method allowing multiple isolated Linux environments on one host. Predecessor to modern container engines. |
IBM Cloud Container Registry | A fully managed private registry for securely storing and distributing Docker images in IBM Cloud. |
Image | A read-only template used to create containers. Contains source code, libraries, and dependencies. |
Immutability | Once created, images cannot be changed — any change creates a new image. |
Microservices | Architectural style where applications are made up of small, loosely coupled services that can be developed and deployed independently. |
Namespace | A Linux kernel feature that isolates system resources like networking, processes, and filesystems. Used by Docker for container isolation. |
Operating System Virtualization | Kernel-level virtualization that allows multiple isolated user-space instances (like containers) on one host. |
Private Registry | A secure registry that restricts access to authorized users only. |
REST API | An API that follows REST principles and allows interaction with web services — used by Docker to communicate with registries and daemons. |
Registry | A hosted service containing repositories of Docker images. Responds to the Docker Registry API. |
Repository | A collection of Docker images, often versioned using tags. Can be pushed to or pulled from a registry. |
Server Virtualization | Process of dividing a physical server into multiple virtual servers that run independently. |
Serverless | A cloud-native model where developers write and deploy code without managing infrastructure like servers. |
Tag | A label applied to an image to distinguish versions (e.g., myapp:latest , myapp:v1 ). |
📌 Summary Table
Category | Key Terms |
---|---|
Development & Methodology | Agile, DevOps, CI/CD Pipelines, Cloud Native |
Architecture | Client-server, Microservices, Serverless |
Container Platforms | Docker, LXC, Daemon-less Runtimes |
Docker Components | Dockerfile, Docker Client, Docker Daemon, Docker CLI |
Networking & Storage | Docker Networks, Docker Storage, Volumes, Bind Mounts |
Image Management | Image, Repository, Tag, Immutability |
Registries | Docker Hub, Private Registry, IBM Cloud Container Registry |
Security & Isolation | Namespace, Plugin Architecture |