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:
Application code
Runtime (e.g., Python, Node.js)
System tools and libraries
Configuration
All containers are isolated from each other but share the same host operating system.
Example analogy:
A virtual machine is like a house (separate infrastructure).
A container is like an apartment (isolated, but shares the same building).
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
You write a Dockerfile describing your environment.
You build an image from it using:
docker build -t myapp .
You run a container from that image:
docker run -d -p 8080:80 myapp
Docker uses the OS kernel to isolate this container from others.
5. Common Docker Commands
Command | Description |
---|
docker ps | List running containers |
docker ps -a | List all containers (including stopped ones) |
docker images | Show available images |
docker build -t name . | Build an image from a Dockerfile |
docker run -it image | Run container interactively |
docker stop container_id | Stop a running container |
docker rm container_id | Remove a container |
docker rmi image_id | Remove an image |
docker exec -it container_id bash | Access 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:
bridge: Default network for standalone containers.
host: Shares the host network stack.
overlay: Used in Docker Swarm for multi-host networking.
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?
Consistency: Works identically across environments.
Speed: Faster startup than VMs.
Isolation: Each app runs independently.
Portability: Runs anywhere Docker is installed.
Scalability: Works seamlessly with Kubernetes and cloud services.
10. Best Practices
Use .dockerignore
to exclude unnecessary files.
Keep images small by using minimal base images (e.g., alpine
).
Tag images with versions (e.g., myapp:v1
).
Don’t store secrets in Dockerfiles.
Use multi-stage builds for optimization.
11. Common Pitfalls
Forgetting to clean up unused containers or images (docker system prune
).
Running multiple containers on the same port without mapping.
Hardcoding environment variables.
Not using volumes for persistent data.
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.