Docker is one of the most powerful tools in modern software development. Whether you're a beginner trying to containerize your first app or an advanced engineer managing CI/CD pipelines, this guide will walk you through everything you need to know — in plain English.
🔰 Beginner Level
- What is Docker?
Dockerlets you package and run applications in isolated environments called containers.
✅ Why Use It?
Ensures your app runs the same everywhere — no more "it works on my machine."
- What is a Docker Image?
A Docker image is a read-only blueprint of your app that includes the code, dependencies, and OS environment.
bash
docker build -t my-app .
3. What is a Docker Container?
A container is a running instance of an image. It’s isolated, fast, and lightweight.
docker run my-app
4. Dockerfile
A Dockerfile
is a set of instructions for building Docker images.
FROM node:18
COPY . /app
WORKDIR /app
RUN npm install
CMD ["npm", "start"]
5. Docker Hub
Docker Hub is like GitHub, but for images. You can push/pull images from it.
docker pull nginx
docker push myuser/my-app
6. Basic Commands
docker build -t app . # Build image
docker run -p 3000:3000 app # Run container
docker ps # List containers
docker stop <id> # Stop container
docker exec -it <id> bash # Open shell inside container
7. Port Mapping
Maps container ports to host ports. Example:
docker run -p 8080:3000 app
Now access the app at localhost:8080
.
8. Volumes
Used to persist data outside of the container.
docker run -v /data:/app/data my-app
⚙️ Intermediate Level
9. Docker Compose
Use docker-compose.yml
to define multi-container apps.
version: "3"
services:
app:
build: .
ports:
- "3000:3000"
db:
image: postgres
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata:
docker-compose up
10. Docker Networks
Enables communication between containers.
docker network create my-net
docker run --network my-net ...
11. Bind Mount vs Volume
-
Bind Mount: Links to local folders (
-v ./data:/data
) -
Volume: Managed by Docker (
docker volume create
)
12. Docker Context
Lets you switch between environments (e.g., local and remote).
docker context use remote
13. Dockerfile Layers
Each instruction (RUN
, COPY
, etc.) creates a layer.
✅ Helps with caching and faster rebuilds.
14. .dockerignore
Ignore files from being sent to the Docker daemon.
node_modules
.env
.git
🚀 Advanced Level
15. Docker-in-Docker (DinD)
Run Docker inside Docker. Useful in CI/CD tools like GitLab CI.
image: docker:latest
services:
- docker:dind
⚠️ Security Risk: Grants full control over the Docker host.
16. Docker Socket
Access the host Docker engine inside containers.
-v /var/run/docker.sock:/var/run/docker.sock
17. Multi-stage Builds
# Builder
FROM node:18 as builder
WORKDIR /app
COPY . .
RUN npm run build
# Final
FROM nginx
COPY --from=builder /app/dist /usr/share/nginx/html
✅ Reduces image size and exposure of build tools.
18. Healthcheck
HEALTHCHECK CMD curl --fail http://localhost:3000 || exit 1
Docker will monitor container health and restart if needed.
19. Entrypoint vs CMD
ENTRYPOINT ["node"]
CMD ["app.js"]
- ENTRYPOINT: Always runs
- CMD: Default arguments
20. Docker Orchestration Tools
- Docker Swarm: Built-in clustering
- Kubernetes: Industry standard for scaling, self-healing, rolling deployments
🧠 Real-World Scenarios
Use Case | Docker Concepts Used |
---|---|
Simple React App | Dockerfile, Image, Container, Port Mapping |
MERN Stack App | Compose, Networks, Volumes |
GitLab CI for Deployments | Docker-in-Docker, Multi-stage, Context |
Production-grade Builds | Multi-stage, Healthcheck, Entrypoint |
📌 Final Tips
- 🧹 Clean up unused stuff:
docker system prune -a
- 📦 List volumes:
docker volume ls
- 🔍 Debug:
docker logs