1. Introduction

Docker is a platform that allows you to build, run, and manage applications in isolated environments called containers. Containers package your code with everything it needs to run — libraries, dependencies, system tools — ensuring that your application runs the same way everywhere: on your local machine, on a server, or in the cloud.

Before Docker, developers used virtual machines (VMs) to achieve isolation, but VMs are heavy because each one runs a full operating system. Docker containers, in contrast, share the host OS kernel, making them lightweight, faster, and more efficient.

2. What is a Container?

A container is a lightweight, standalone unit that includes:

All containers are isolated from each other but share the same host operating system.

Example analogy:

3. Key Components of Docker

a. Docker Engine

The runtime that builds and runs containers. It includes:

b. Docker Image

An image is a blueprint for a container. It’s a snapshot of the application and its environment.

c. Docker Container

A container is a running instance of an image. You can start, stop, or delete containers without affecting the image.

d. Dockerfile

A text file that defines how to build a Docker image.
Example:

# Use an official Python image
FROM python:3.10

# Set working directory
WORKDIR /app

# Copy files
COPY . .

# Install dependencies
RUN pip install -r requirements.txt

# Run the app
CMD ["python", "app.py"]

e. Docker Hub

A cloud-based registry where you can find and share images.
Command to pull an image:

docker pull nginx

4. How Docker Works

  1. You write a Dockerfile describing your environment.

  2. You build an image from it using:

    docker build -t myapp .
    
  3. You run a container from that image:

    docker run -d -p 8080:80 myapp
    
  4. Docker uses the OS kernel to isolate this container from others.

5. Common Docker Commands

CommandDescription
docker psList running containers
docker ps -aList all containers (including stopped ones)
docker imagesShow available images
docker build -t name .Build an image from a Dockerfile
docker run -it imageRun container interactively
docker stop container_idStop a running container
docker rm container_idRemove a container
docker rmi image_idRemove an image
docker exec -it container_id bashAccess a running container shell

6. Volumes and Data Persistence

Containers are temporary — once deleted, their data is lost.
Volumes solve this by storing data outside the container’s writable layer.

Example:

docker run -d -v mydata:/data myapp

Now, data inside /data persists even if the container is deleted.

7. Networking in Docker

Docker provides different types of networks:

To expose ports:

docker run -p 8080:80 nginx

This maps your host port 8080 to container port 80.

8. Docker Compose

Docker Compose is a tool to manage multi-container applications using a single YAML file.

Example docker-compose.yml:

version: '3'
services:
  web:
    image: nginx
    ports:
      - "8080:80"
  db:
    image: mysql
    environment:
      MYSQL_ROOT_PASSWORD: root

Run both containers:

docker-compose up

9. Why Use Docker?

10. Best Practices

11. Common Pitfalls

12. Summary

Docker allows developers to package applications into containers — lightweight, portable environments that work consistently across systems. Key elements include images, containers, Dockerfiles, and volumes. For multi-container setups, Docker Compose simplifies orchestration.

By learning these basics, you can containerize your applications, improve deployment speed, and eliminate “it works on my machine” problems.