Table of Contents
- Docker Core Concepts
- Virtual Machines vs. Containers
- Docker Architecture
- Docker Installation on Linux
- Docker Command Reference
- Dockerfile Authoring
- Reference Documentation
1. Docker Core Concepts
What is a Container?
A container is a lightweight, isolated process running on a shared operating system kernel.
It is packaged with everything it needs to run, including code, libraries, environment variables, runtime, and configuration files.
- No Separate OS: Shares the host operating system kernel directly.
- Isolated Environment: Runs as a self-contained process.
- Fast Lifecycle: Quick to create, start, and tear down.
- Repeatable: Behaves identically across different environments.
- Portable: Works seamlessly on local laptops, VMs, or the cloud.
- Scalable: Easily multiplied to handle increased traffic.
Container Engines
To build, ship, and run containers, you need a container runtime engine installed
- Docker: is the most popular container engine.
- Other Engines: Linux Containers (LXC), containerd, and CRI-O.
2. Virtual Machines vs. Containers
The structural divergence between Virtual Machines (VMs) and Containers defines their respective resource profiles and deployment speeds:
| Feature | Virtual Machines (VMs) | Containers |
|---|---|---|
| Architecture | Runs via a Hypervisor. | Shares the host OS kernel. |
| Weight | Heavyweight | Lightweight |
| Scalability | Slow boot times | Near-zero startup times |
| Portability | Bound to hypervisor standards | Highly portable across any OS |
| OS Overhead | Redundant guest OS overhead | Shares the host OS kernel |
| Image Management | Inefficient image management | Efficient, layered image management |
3. Docker Architecture
Docker uses a client-server architecture to simplify building, running, managing, and distributing applications. It is an open-source platform developed by Docker Inc.
Architectural Components
- Docker Client: The primary interface (docker CLI) that enables users to interact with Docker by sending commands.
- Docker Host: The physical server or Virtual Machine (VM) on which the Docker engine is installed and running.
- Docker Daemon (dockerd): The heart of Docker. It listens for Docker API requests from the client and manages containers, images, networks, and volumes.
- Docker Images: Read-only binary templates containing the application and its dependencies used to build containers.
- Docker Containers: The encapsulated, runnable instances of a Docker image where applications actually execute.
- Docker Registries: Storage repositories used to hold and share Docker images (e.g., Docker Hub).
4. Docker Installation on Linux
Official Reference: Docker Ubuntu Installation Guide
Step 1: Add Docker's Official GPG Key
sudo apt update
sudo apt install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
Step 2: Add the Repository to Apt Sources
sudo tee /etc/apt/sources.list.d/docker.sources <<EOF
Types: deb
URIs: https://download.docker.com/linux/ubuntu
Suites: $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}")
Components: stable
Architectures: $(dpkg --print-architecture)
Signed-By: /etc/apt/keyrings/docker.asc
EOF
sudo apt update
Step 3: Install Docker Engine
sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Step 4: Verify Installation
# Check if the Docker system service is active
sudo systemctl status docker
# Start Docker manually if it is not running
sudo systemctl start docker
# Verify installed versions and system configurations
docker version
docker info
5. Docker Command Reference
Image Management
-
docker pull: Download an image from a registry.-
Syntax:
docker pull [OPTIONS] IMAGE[:TAG|@DIGEST]
-
Syntax:
-
docker images: List locally stored images.-
Syntax:
docker images [OPTIONS] -
Example:
docker images -qLists only local image IDs
-
Syntax:
-
docker rmi: Remove one or more local images.-
Syntax:
docker rmi [OPTIONS] IMAGE [IMAGE...] -
Example:
docker rmi -f myimageForcefully removes the image
-
Syntax:
-
docker image prune: Remove unused, dangling images to safely clear host disk space. -
docker history: Show the build history and layers of a specific image.-
Syntax:
docker history IMAGE
-
Syntax:
Container Lifecycle
-
docker ps: List containers currently tracked by the engine.-
Syntax:
docker ps [OPTIONS] -
Example:
docker psShows active containers -
Example :
docker ps -aShows all containers, including stopped ones
-
Syntax:
-
docker start: Start one or more stopped containers.-
Syntax:
docker start [OPTIONS] CONTAINER [CONTAINER...]
-
Syntax:
-
docker stop: Gracefully stop a running container.-
Syntax:
docker stop [OPTIONS] CONTAINER [CONTAINER...] -
Example:
docker stop -t 30 mycontainerAllows 30 seconds to stop gracefully before forcing termination
-
Syntax:
-
docker rm: Remove one or more stopped containers from the host filesystem.-
Syntax:
docker rm [OPTIONS] CONTAINER [CONTAINER...] -
Example:
docker rm -f mycontainerForcefully removes a running container
-
Syntax:
-
docker inspect: Return low-level configuration and state information on Docker objects in raw JSON format.-
Syntax:
docker inspect [OPTIONS] OBJECT [OBJECT...]
-
Syntax:
Executing Commands & Interactivity
-
docker exec: Run a new command process inside an actively running container.-
Syntax:
docker exec [OPTIONS] CONTAINER COMMAND [ARG...] -
Example:
docker exec -t my-container ls /app -
Example (View File):
docker exec -i mycontainer cat /etc/hostname -
Example:
docker exec -it mycontainer /bin/bash
-
Syntax:
-
docker run: Create and start a brand new container instance from a target image.-
Example:
docker run -it --rm --name cont3 ubuntu /bin/bashautomatically deletes the containerβs ephemeral file layer as soon as it exits, keeping your host system clean.
-
Example:
System Utilities
-
docker --help: Displays help documentation, global options, and sub-commands for the Docker CLI.
6. Dockerfile Authoring
What is a Dockerfile?
A Dockerfile is a text document containing all the sequential commands a user could call on the command line to assemble a Docker image.
Essential Dockerfile Instructions
| Instruction | Function |
|---|---|
FROM: |
Sets the base image for subsequent instructions. |
RUN: |
Executes commands in a new image layer and commits the results. |
CMD: |
Specifies the default command to execute when the container starts. |
COPY: |
Copies local files and directories from the host context into the filesystem of the image. |
WORKDIR: |
Sets the working directory for any subsequent instructions. |
ENV: |
Defines environment variables inside the container environment. |
LABEL: |
Adds metadata key-value pairs to organize your images. |
Image Layers & Building
Each instruction in a Dockerfile maps directly to a read only Docker image layer.
-
docker build: Reads the Dockerfile instructions and builds the image layers.-
Syntax:
docker build [OPTIONS] PATH | URL | - -
Example:
docker build -t myapp .Builds an image named myapp using the Dockerfile in the current directory
-
Syntax:
Docker Image Layers Best Practice Tips
- Group Wisely: Combine commands (like apt update && apt install) to optimize layer caching performance.
- Order Strategically: Place less frequently changed instructions (such as dependency installation) early in the file.
- Minimize Size: Avoid unnecessary RUN commands to reduce layer count and images size
7. Reference
Official Documentation: Learn Docker Documentation
Official Reference: Docker Ubuntu Installation Guide









