Docker Deep Dive
π³ Docker Deep Dive: From Zero to Production-Ready Containers π
A Complete, Practical & Beginner-to-Advanced Guide
βIf it works on my machine, Docker makes sure it works everywhere.β π
π₯ What is Docker?
Docker is a containerization platform that allows you to package an application along with its dependencies, libraries, and configurations into a single unit called a container π¦.
π€ Why Docker?
- β Same environment everywhere (Dev, QA, Prod)
- β‘ Faster deployment
- π Less resource usage than Virtual Machines
- π§© Perfect for Microservices
- βοΈ Cloud & DevOps friendly
π§± Docker vs Virtual Machine
| Feature | Docker | Virtual Machine |
|---|---|---|
| OS | Shares Host OS | Full Guest OS |
| Boot Time | Seconds β‘ | Minutes π’ |
| Size | MBs | GBs |
| Performance | High π | Moderate |
π§ Core Docker Terminologies (Must-Know) π
1οΈβ£ Image
- A blueprint of your application
- Read-only
- Built using a
Dockerfile
Example:
docker pull nginx
2οΈβ£ Container
- A running instance of an image
- Lightweight & isolated
docker run nginx
3οΈβ£ Dockerfile
A file containing instructions to build an image
Example:
FROM ruby:3.2
WORKDIR /app
COPY . .
RUN bundle install
CMD ["rails", "server", "-b", "0.0.0.0"]
4οΈβ£ Docker Hub
- Public image registry π
- Like GitHub for Docker images
docker pull redis
5οΈβ£ Volumes
- Persistent storage for containers πΎ
- Data survives container restart
docker volume create my_volume
6οΈβ£ Networks
- Enable container-to-container communication π
docker network create my_network
7οΈβ£ Docker Compose
- Manage multi-container applications
- Defined using
docker-compose.yml
βοΈ Docker Installation & Setup Guide π οΈ
πΉ Install Docker
π https://docs.docker.com/get-docker/
Verify:
docker --version
ποΈ Docker Commands (Daily Use Cheat Sheet) π
π Image Commands
docker images
docker pull ubuntu
docker rmi image_id
π Container Commands
docker ps
docker ps -a
docker start container_id
docker stop container_id
docker rm container_id
π§Ή Cleanup Commands
docker system prune
docker container prune
π§ͺ Build & Run Your First App (Example) π§©
Step 1οΈβ£ Create Dockerfile
FROM node:18
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
CMD ["npm", "start"]
Step 2οΈβ£ Build Image
docker build -t my-node-app .
Step 3οΈβ£ Run Container
docker run -p 3000:3000 my-node-app
π App running at http://localhost:3000
𧬠Docker Compose Example (Rails + PostgreSQL) π
version: '3.9'
services:
web:
build: .
ports:
- "3000:3000"
depends_on:
- db
db:
image: postgres
environment:
POSTGRES_PASSWORD: password
Run:
docker-compose up
π Common Docker Mistakes to Avoid π«
β 1. Using latest Tag
βοΈ Always use specific versions
image: postgres:14
β 2. Huge Images
βοΈ Use lightweight images like:
FROM node:18-alpine
β 3. Running as Root
βοΈ Create a non-root user inside Dockerfile π
β 4. Not Using .dockerignore
βοΈ Avoid copying unnecessary files
node_modules
.git
log
tmp
β 5. Storing Secrets in Dockerfile
βοΈ Use environment variables or secrets manager π
π§ Best Practices for Production π
- β Multi-stage builds
- β Minimal base images
- β Health checks
- β Use volumes for DB data
- β Scan images for vulnerabilities
π§© Docker + DevOps = β€οΈ
Docker integrates seamlessly with:
- π CI/CD (GitHub Actions, Jenkins)
- βοΈ Cloud (AWS ECS, EKS)
- π§± Kubernetes
- π§ Microservices Architecture
π Final Thoughts
Docker is not just a tool, itβs a mindset shift π§ . Once you master Docker, scaling, deploying, and maintaining applications becomes effortless.
βBuild once. Run anywhere.β ππ³
π₯ Bonus Tip
π If youβre a Ruby on Rails / React / DevOps developer like me, Docker is a career multiplier πΌπ
© Lakhveer Singh Rajput - Blogs. All Rights Reserved.