Docker has become one of the most important tools in modern software development. It helps developers build, ship, and run applications consistently across different environments.
In this guide, you will learn what Docker is, why developers use it, and how it works step by step.
1. What is Docker?
Docker is a containerization platform that allows developers to package an application and all its dependencies into a container.
A container includes:
-
Application code
-
Runtime (PHP, Node.js, Python, etc.)
-
Libraries
-
System tools
-
Configuration files
This ensures the application runs the same way on every machine.
Example problem without Docker:
Developer machine: PHP 8.2
Production server: PHP 7.4
Result: Application breaks
Docker solves this by packaging everything together.
2. What is a Container?
A container is a lightweight environment that runs an application.
It is similar to a virtual machine, but much faster and smaller.
Comparison:
| Feature | Virtual Machine | Docker Container |
|---|---|---|
| Size | Large (GBs) | Small (MBs) |
| Startup time | Minutes | Seconds |
| Performance | Slower | Faster |
| OS included | Yes | No |
Containers share the host OS kernel, which makes them lightweight.
3. Docker Architecture
Docker has three main components.
1. Docker Client
The command line tool developers use.
Example:
docker build
docker run
docker pull
2. Docker Host
The machine where containers run.
It includes:
-
Docker Engine
-
Containers
-
Images
3. Docker Registry
A place to store Docker images.
Popular registries:
-
Docker Hub
-
GitHub Container Registry
-
AWS ECR
Example image:
nginx
mysql
redis
node
4. What is a Docker Image?
A Docker image is a blueprint for creating containers.
It contains:
-
Application code
-
Runtime
-
Dependencies
-
System libraries
Example image structure:
Node.js Runtime
↓
Application Code
↓
Configuration
When you run an image:
docker run nginx
Docker creates a container from the image.
5. What is a Dockerfile?
A Dockerfile is a script used to build a Docker image.
Example Dockerfile for a Node.js application:
FROM node:18
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]
Explanation:
| Instruction | Purpose |
|---|---|
| FROM | Base image |
| WORKDIR | Working directory |
| COPY | Copy files |
| RUN | Execute commands |
| EXPOSE | Open port |
| CMD | Start application |
6. Building a Docker Image
After creating a Dockerfile, build the image.
Command:
docker build -t my-app .
Explanation:
-
docker build→ builds image -
-t→ tag name -
.→ current directory
Check images:
docker images
7. Running a Container
Run a container from an image.
docker run -p 3000:3000 my-app
Explanation:
| Option | Meaning |
|---|---|
| run | Start container |
| -p | Port mapping |
| my-app | Image name |
Now your app runs at:
http://localhost:3000
8. Common Docker Commands
List containers
docker ps
Show all containers:
docker ps -a
Stop container
docker stop container_id
Remove container
docker rm container_id
Remove image
docker rmi image_name
9. Docker Volumes
Containers are temporary.
If a container stops, data may disappear.
Docker volumes store persistent data.
Example:
docker run -v mydata:/var/lib/mysql mysql
Use cases:
-
Database storage
-
Uploaded files
-
Logs
10. Docker Networking
Docker containers communicate using networks.
Example:
docker network create my-network
Run container with network:
docker run --network=my-network nginx
Useful for connecting:
-
Backend
-
Database
-
Cache
11. Docker Compose
When applications have multiple services, Docker Compose manages them.
Example stack:
-
Laravel
-
MySQL
-
Redis
-
Nginx
Example docker-compose.yml:
version: "3"
services:
app:
build: .
ports:
- "8000:8000"
db:
image: mysql:8
environment:
MYSQL_ROOT_PASSWORD: secret
redis:
image: redis
Start services:
docker-compose up
Stop services:
docker-compose down
12. Why Developers Use Docker
Main advantages:
1. Environment Consistency
Works the same on:
-
Local machine
-
Staging
-
Production
2. Easy Deployment
Applications can be deployed with a single command.
docker run my-app
3. Isolation
Each application runs in its own container.
No dependency conflicts.
4. Fast Setup
Instead of installing software manually:
docker run mysql
docker run redis
docker run nginx
Everything runs instantly.
13. Example Development Stack Using Docker
Modern developer stack:
Docker
│
├── Nginx
├── Laravel / Node / Python
├── MySQL
├── Redis
└── Queue Workers
All services run as containers.
14. Docker vs Virtual Machines
| Feature | Docker | Virtual Machines |
|---|---|---|
| Startup | Seconds | Minutes |
| Resource usage | Low | High |
| Portability | High | Medium |
| Isolation | Process-level | Full OS |
15. When Should You Use Docker?
Docker is best for:
-
Microservices
-
CI/CD pipelines
-
Development environments
-
Cloud deployments
-
DevOps workflows
16. Example Workflow Using Docker
Typical developer workflow:
1. Write application
2. Create Dockerfile
3. Build image
4. Run container
5. Push image to registry
6. Deploy container to server
Example deployment:
docker build -t myapp
docker push myapp
docker run myapp
Conclusion
Docker simplifies application development by allowing developers to package applications with their dependencies into containers.
Key takeaways:
-
Docker uses containers instead of virtual machines
-
Applications run consistently across environments
-
Docker improves deployment, scalability, and collaboration
Because of these benefits, Docker has become a core tool in modern DevOps and cloud-native development.
