DEV Community

Cover image for The Ultimate Docker Guide: Core Concepts & Commands 🐳
Hosea Mutwiri
Hosea Mutwiri

Posted on

The Ultimate Docker Guide: Core Concepts & Commands 🐳

Table of Contents

  1. Docker Core Concepts
  2. Virtual Machines vs. Containers
  3. Docker Architecture
  4. Docker Installation on Linux
  5. Docker Command Reference
  6. Dockerfile Authoring
  7. Reference Documentation

1. Docker Core Concepts

What is a Container?

A container is a lightweight, isolated process running on a shared operating system kernel.

Uploading image
It is packaged with everything it needs to run, including code, libraries, environment variables, runtime, and configuration files.

Key Features of Containers

  • 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Step 3: Install Docker Engine

sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  1. Docker Command Reference Here is your content naturally enhanced and formatted into a clean, professional Markdown section. I have standardized the syntax formatting, added brief explanations for common flags, and organized the sub-sections into a more readable layout.

5. Docker Command Reference

Image Management

  1. docker pull: Download an image from a registry.
    • Syntax: docker pull [OPTIONS] IMAGE[:TAG|@DIGEST]
  2. docker images: List locally stored images.
    • Syntax: docker images [OPTIONS]
    • Example: docker images -q Lists only local image IDs
  3. docker rmi: Remove one or more local images.
    • Syntax: docker rmi [OPTIONS] IMAGE [IMAGE...]
    • Example: docker rmi -f myimage Forcefully removes the image
  4. docker image prune: Remove unused, dangling images to safely clear host disk space.
  5. docker history: Show the build history and layers of a specific image.
    • Syntax: docker history IMAGE

Container Lifecycle

  1. docker ps: List containers currently tracked by the engine.
    • Syntax: docker ps [OPTIONS]
    • Example: docker ps Shows active containers
    • Example : docker ps -a Shows all containers, including stopped ones
  2. docker start: Start one or more stopped containers.
    • Syntax: docker start [OPTIONS] CONTAINER [CONTAINER...]
  3. docker stop: Gracefully stop a running container.
    • Syntax: docker stop [OPTIONS] CONTAINER [CONTAINER...]
    • Example: docker stop -t 30 mycontainer Allows 30 seconds to stop gracefully before forcing termination
  4. docker rm: Remove one or more stopped containers from the host filesystem.
    • Syntax: docker rm [OPTIONS] CONTAINER [CONTAINER...]
    • Example: docker rm -f mycontainer Forcefully removes a running container
  5. docker inspect: Return low-level configuration and state information on Docker objects in raw JSON format.
    • Syntax: docker inspect [OPTIONS] OBJECT [OBJECT...]

Executing Commands & Interactivity

  1. 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
  2. docker run: Create and start a brand new container instance from a target image.
    • Example: docker run -it --rm --name cont3 ubuntu /bin/bash automatically deletes the container’s ephemeral file layer as soon as it exits, keeping your host system clean.

System Utilities

  1. 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.

  1. 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

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

Top comments (0)