What Is Docker? A Beginner's Guide to Containerization
"It works on my machine!" — The phrase developers say most. An application works in the dev environment but crashes on the server. The reason: different environments, different dependencies, different configurations. Docker solves this problem at its root.
What Is Docker?
Docker is a platform that lets you package applications into isolated, portable packages called containers. A container bundles your application with all its dependencies — so it runs the same way everywhere.
Container vs Virtual Machine
| Criteria | Container (Docker) | Virtual Machine (VM) | |----------|-------------------|---------------------| | Size | 10-100 MB | 1-10 GB | | Start time | Seconds | Minutes | | Resource usage | Low | High | | Isolation | Process level | OS level | | Portability | Very high | Medium |
Why Should You Use Docker?
1. Environment Consistency
- Development = Testing = Production
- No "works on my machine" problems
- All dependencies packaged
2. Fast Deployment
- Deploy with one command
- Easy rollback
- CI/CD pipeline compatible
3. Isolation
- Each application in its own environment
- No dependency conflicts
- Security layer
4. Scaling
- Add new containers when load increases
- Auto-scaling with Kubernetes
- Perfect for microservice architecture
Core Docker Concepts
Image
Your application's template — a set of instructions.
Container
A running instance created from an image.
Dockerfile
An instruction file used to create images.
Docker Compose
Configuration for managing multiple containers together.
Docker Hub
Central registry where Docker images are shared.
Dockerfile Example (Next.js)
# Base image
FROM node:20-alpine
# Working directory
WORKDIR /app
# Copy dependencies
COPY package*.json ./
# Install dependencies
RUN npm ci
# Copy application files
COPY . .
# Build application
RUN npm run build
# Expose port
EXPOSE 3000
# Start application
CMD ["npm", "start"]
Essential Docker Commands
# Build image
docker build -t my-app .
# Run container
docker run -p 3000:3000 my-app
# List running containers
docker ps
# List all containers
docker ps -a
# Stop container
docker stop <container-id>
# List images
docker images
# Remove image
docker rmi <image-id>
# View container logs
docker logs <container-id>
Docker Compose Example
Web app + database + Redis:
version: '3.8'
services:
web:
build: .
ports:
- "3000:3000"
environment:
- DATABASE_URL=postgresql://user:pass@db:5432/mydb
depends_on:
- db
- redis
db:
image: postgres:16
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: pass
POSTGRES_DB: mydb
volumes:
- pgdata:/var/lib/postgresql/data
redis:
image: redis:7-alpine
ports:
- "6379:6379"
volumes:
pgdata:
# Start all services
docker compose up -d
# Stop services
docker compose down
# View logs
docker compose logs -f
Docker Use Cases
| Scenario | Description | |----------|------------| | Dev environment | Entire team works in same environment | | CI/CD | Automated testing and deployment | | Microservices | Each service in its own container | | Legacy apps | Isolate old applications | | Demo/prototype | Quick environment setup |
Docker Best Practices
- Use small images —
alpinevariants - Multi-stage build — Reduce production image size
- .dockerignore — Exclude unnecessary files
- Non-root user — Don't run as root for security
- Layer caching — Copy frequently changing files last
- Health check — Monitor container health
Conclusion
Docker is the standard for modern software development and deployment. The days of "it works on my machine" are over. With Docker, your development, testing, and production environments are exactly the same.
If you'd like to build projects with Docker and modern DevOps practices, get in touch: info@cagribilgehan.com. Check out my projects: cagribilgehan.com