Docker Fundamentals

Master Docker containerization from basics to advanced concepts. Learn to build, run, and manage containers for your applications.

What is Docker?

Docker is an open-source platform that automates the deployment, scaling, and management of applications using containerization technology. Containers allow developers to package an application with all its dependencies into a standardized unit for software development.

Unlike virtual machines, containers share the host system's kernel, making them lightweight and fast. This means you can run many more containers on a given hardware combination than virtual machines.

Docker Architecture

Docker uses a client-server architecture with the following key components:

  • Docker Daemon - The background service running on the host that manages building, running, and distributing Docker containers
  • Docker Client - The command-line tool that allows users to interact with the Docker daemon
  • Docker Registry - A storage and distribution system for Docker images (Docker Hub is the default public registry)
  • Docker Images - Read-only templates used to create containers
  • Docker Containers - Running instances of Docker images

Key Docker Concepts

Images vs Containers

A Docker image is like a blueprint or template - it contains everything needed to run an application (code, runtime, libraries, environment variables, config files). A container is a running instance of an image. You can run multiple containers from the same image.

Dockerfile

A Dockerfile is a text document containing instructions to build a Docker image. Each instruction creates a layer in the image, making it efficient to rebuild images when only specific parts change.

Docker Compose

Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application's services, networks, and volumes.

Benefits of Docker

  • 🚀 Consistency - Same environment from development to production
  • ⚡ Speed - Containers start in seconds, not minutes
  • đŸ“Ļ Portability - Run anywhere Docker is installed
  • 🔧 Isolation - Each container is isolated from others
  • 💰 Efficiency - Better resource utilization than VMs
  • 🔄 Scalability - Easy horizontal scaling

Docker Best Practices

  • Use official base images when possible
  • Keep images small by using multi-stage builds
  • Don't run processes as root inside containers
  • Use .dockerignore to exclude unnecessary files
  • Tag images with meaningful version numbers
  • Scan images for security vulnerabilities
đŸ’ģ Essential Docker Commands
# Pull an image from Docker Hub
docker pull nginx:latest

# List all images
docker images

# Run a container
docker run -d -p 8080:80 --name my-nginx nginx:latest

# List running containers
docker ps

# Stop a container
docker stop my-nginx

# Remove a container
docker rm my-nginx

# Remove an image
docker rmi nginx:latest
Output
latest: Pulling from library/nginx Digest: sha256:abc123... Status: Downloaded newer image for nginx:latest REPOSITORY TAG IMAGE ID SIZE nginx latest 605c77e624dd 141MB CONTAINER ID IMAGE COMMAND NAMES a1b2c3d4e5f6 nginx ... my-nginx
💡 These are the fundamental Docker commands you will use daily. Practice these to become comfortable with Docker workflow.
đŸ’ģ Dockerfile for Python Application
# Use an official Python runtime as base
FROM python:3.11-slim

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

# Set work directory
WORKDIR /app

# Install dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Copy project
COPY . .

# Expose port
EXPOSE 8000

# Run the application
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
Output
Step 1/8 : FROM python:3.11-slim ---> abc123 Step 2/8 : ENV PYTHONDONTWRITEBYTECODE=1 ---> Using cache ... Successfully built def456 Successfully tagged myapp:latest
💡 This Dockerfile creates a production-ready Python container. It uses a slim base image for smaller size and follows best practices like setting environment variables.
đŸŽ¯

Test Your Knowledge

Answer these questions to check your understanding

1 What is the main difference between a Docker container and a virtual machine?
💡 Containers share the host OS kernel making them lightweight, while VMs include their own complete OS making them heavier.
2 What file is used to define how to build a Docker image?
💡 A Dockerfile contains instructions for building a Docker image, including base image, commands, and configuration.