Logo Sujal Magar
Docker Basics

Docker Basics

January 26, 2026
9 min read
Table of Contents

Docker Basics: Overview

Docker is an open-source platform for developing, shipping, and running applications inside containers. Containers are lightweight, portable, and isolated environments that package code and dependencies together, ensuring consistency across development, testing, and production. Unlike virtual machines, containers share the host OS kernel, making them efficient. Docker simplifies app development by solving “it works on my machine” issues.

The key Docker concepts are explain in simple terms, covering what it is, key sub-concepts, basic CLI (Command Line Interface) commands, and real-world examples. To get started:

  • Install Docker from the official website docker for your OS (e.g., Docker Desktop for Windows/Mac).
  • Verify installation: docker --version
  • Docker runs on Linux natively and on Windows/Mac, it uses a VM.
  • Commands are run in your terminal (e.g., bash, PowerShell).
  • Assume you have Docker installed and running. ALways check Docker docs for the latest details, as versions evolve (e.g., current version for me is 29.1.4).

1. Docker Images

What it is

Images are read-only templates used to create containers. They include the app code, runtime, libraries, and configurations. They are like a blueprint for you app.

Key Concepts

  • Base Image: Starts from an official image (e.g., ubuntu, node).
  • Layers: Images are built in immultable layers for efficiency (caching during builds).
  • Tags: Versions like ngingx:latest or nginx:1.29.4.
  • Registry: Where images are stored (e.g., Docker Hub, private registries).
  • Pull/Push: Download/upload images from registries.
  • Size Optimization: Use multi-stage builds to reduce final image size.

Basic CLI Commands

  1. Search for an image
docker search nginx
  1. Pull an image
docker pull nginx:latest
  1. List local images
docker images
# or
docker image ls
  1. Build an image from Dockerfile
docker build -t my-image:tag .  # from current directory
  1. Tag an image
docker tag my-image:tag username/my-repo:tag
  1. Push to registry
docker push username/my-repo:tag
  1. Remove an image
docker rmi my-image:tag
# or
docker image rm my-image:tag
  • Examples

    • Web Server Setup: Pull nginx:latest to quickly spin up a web server for testing static sites.
    • Custom App Packaging: Build an image for a Node.js app, including dependencies, to deploy consistently on any server.
    • Version Control: Tag images with Git commit hashes (e.g., my-app:abc123) for rollback in production.

2. Docker Containers

What it is

Containers are runnable instances of images. They are isolated processes that can start, stop, and be managed independently.

Key Concepts

  • Lifecycle: Create, start, stop, restart, remove.
  • Ports: Map host ports to container ports (e.g., expose app on localhost:8080).
  • Environment Variables: Pass configs like API keys via -e.
  • Detached Mode: Run in background with -d.
  • Interactive Mode: Access shell with -it.
  • Resource Limits: Set CPU/memory with --cpus or --memory.

Basic CLI Commands

  1. Run a container
docker run -d -p 80:80 --name my-container nginx:latest
  1. List running containers
docker ps
# or
docker container ls
  1. List all container (including stopped)
docker ps -a
  1. Stop a container
docker stop my-container
  1. Start a stopped container
docker start my-container
  1. Remove a container
docker rm my-container  # must be stopped
  1. Exec into a running container
docker exec -it my-container /bin/sh
# or
docker exec -it my-container bash
  1. Logs
docker logs my-container
  • Examples

    • Database Testing: Run docker run -d -p 5432:5432 --name db postgres to test a PostgreSQL instance locally without installing it.
    • Microservices: Run multiple containers (e.g., one for API, one for DB) and link them for a full-stack app.
    • Debugging: Exec into a container to inspect files or run commands during troubleshooting.

3. Dockerfile

What it is

A Dockerfile is a text file with instructions to build a Docker image automatically.

Key Concepts

  • Instructions: Commands like FROM (base image), RUN (execute commands), COPY (add files), CMD (default command), ENTRYPOINT (fixed command).
  • Build Context: The directory where Dockerfile lives; files are copied from here.
  • Multi-Stage Builds: Use multiple FROM statements to build and then slim down the image.
  • Best Practices: Minimize layers, use .dockerignore to exclude files, cache wisely.
  • Arguments: Use ARG for build-time variables.

Basic CLI Commands

  1. Build from Dockerfile
docker build -t my-image:tag -f Dockerfile .
  1. Build with args
docker build --build-arg MY_VAR=value -t my-image:tag .
  • Examples

    • Simple Node App: Dockerfile for a backend service.
    FROM node:alpine  # image
     
    COPY . /app  # copying the whole content to /app directory
     
    RUN npm install  # install dependencies
     
    CMD ["node", "app.js"]
    • Multi-State for Go App: First stage compiles binary, second copies it to a minimal runtime image to reduce size.

    • CI Integration: In a pipeline, build and push images on code commit for automated deployments.


4. Docker Volumes

What it is

Volumes provide persistent storage for containers, allowing data to survive container restarts or deletions.

Key Concepts

  • Types: Named volumes (managed by Docker), bind mounts (host directories), tmpfs (in-memory).
  • Persistence: Data in volumes isn’t deleted when containers stop.
  • Mounting: Attach to containers with -v or --mount.
  • Sharing: Multiple containers can share a volume.
  • Backups: Use docker cp or tools like docker volumne export.

Basic CLI Commands

  1. Create a volume
docker volume create my-volume
  1. List volumes
docker volume ls
  1. Inspect a volume
docker volume inspect my-volume
  1. Run with volume
docker run -d -v my-volume
  1. Bind mount host dir
docker run -d -v /host/path:/container/path --name my-container my-image
  1. Remove a volume
docker volume rm my-volume  # not in use
  1. Prune unused
docker volume prune
  • Examples

    • Database Persistence: Mount a volume for /var/lib/postgresql/data in a Postgres container to keep data after restarts.
    • Log Storage: Bind mount host logs to a container for easy access and analysis.
    • Shared Configs: Multiple app containers share a config volume for consistent settings.

5. Docker Networks

What it is

Networks allow containers to communicate securely and isolate traffic.

Key Concepts

  • Drivers: Bridge (default for single host), host (use host networking), overlay (multi-host).
  • Default Network: All containers join ‘bridge’ unless specified.
  • Container Linking: Use network names or aliases for service discovery.
  • Ports Exposure: Networks control which ports are accessible.
  • Isolation: Custom networks for app-specific traffic.

Basic CLI Commands

  1. Create a network
docker network create my-network
  1. List networks
docker network ls
  1. Inspect a network
docker network inspect my-network
  1. Run on network
docker run -d --network my-network --name my-container my-image
  1. Connect existing container
docker run -d --network my-network existing-container
  1. Disconnect
docker network disconnect my-network my-container
  1. Remove
docker network rm my-network
  • Examples

    • Frontend-Backend Communication: Create a network for a web app container to talk to a DB container via name (e.g., db:5432).
    • Multi-Host Setup: Use overlay networks with Docker Swarm for distributed apps.
    • Security: Isolate sensitive services on a private network, exposing only the API.

6. Docker Compose

What it is

Docker Compose is a tool for defining and running multi-container apps using a YAML file (docker-compose.yml).

Key Concepts

  • Services: Define containers (e.g., web, db).
  • Volumes/Networks: Declare in the file for persistence and communication.
  • Dependencies: Use depends_on for startup order.
  • Environment Files: Load vars from .env.
  • Scaling: Run multiple instances with --scale.
  • Versions: Compose file format (e.g., version: ‘3.8’).

Basic CLI Commands

  1. Start services
docker compose up -d  # from directory with docker-compose.yml
  1. Stop services
docker compose down
# or
docker compose down -v  # remove volume
  1. Build services
docker compose build
  1. Logs
docker logs -f
  1. Exec into service
docker compose exec web /bin/sh
  1. Pull images
docker compose pull
  1. List services
docker compose ps
  • Examples

    • Full-Stack App: docker-compose.yml with services for Nginx, Node.js API, and MongoDB, linked via networks and volumes.
    • Development Environment: Quickly spin up a local stack for testing with hot-reloading.
    • Production Deployment: Use with overrides (docker-compose.prod.yml) for env-specific configs.

7. Docker Hub and Registries

What it is

Docker Hub is a public registry for sharing images; private registries exist for secure storage.

Key Concepts

  • Repositories: Like Git repos for images (e.g., username/my-app).
  • Official Images: Trusted ones like python, mysql.
  • Authentication: Login for private pushes/pulls.
  • Scanning: Check for vulnerabilities.
  • Alternatives: ECR (AWS), GCR (Google), self-hosted like Harbor.

Basic CLI Commands

  1. Login
docker login  # enter credentials
  1. Search repos
docker search username/repo
  1. Pull from private
docker pull private-repo/image:tag
  1. Push
docker push username/repo:tag
  1. Logout
docker logout
  1. For scanning, use Docker Scout
docker scout cves my-image
  • Examples:

    • Sharing Open-Source: Push a custom tool image to Docker Hub for community use.
    • CI/CD Integration: In a pipeline, build and push to ECR for AWS deployments.
    • Vulnerability Checks: Scan images before deployment to ensure security.