Docker eliminates "it works on my machine" problems by containerizing apps along with their environments. Itβs the backbone of modern DevOps, enabling reproducible builds, fast deployments, and scalable infrastructure.
Whether you're building microservices, APIs, frontends, or full-stack SaaS appsβDocker is essential.
π§ Prerequisites:
β
Basic CLI knowledge
β
A project or app to test Docker with (Node.js used here, but language doesn't matter)
π οΈ 1. Install Docker
π₯οΈ macOS / Windows
β‘οΈ Install Docker Desktop: https://www.docker.com/products/docker-desktop
sudo apt update sudo apt install docker.io -y sudo systemctl start docker sudo systemctl enable docker
Verify
docker --version
π³ 2. Understand Dockerβs Core Concepts
Concept | Description |
---|---|
Image | A blueprint of your app (like a class) |
Container | A running instance of an image |
Dockerfile | Instructions to build your image |
Volume | Persistent storage for containers |
Network | Allows containers to communicate |
Registry | Stores images (like Docker Hub or GitHub Container Registry) |
π 3. Running Your First Container
docker run hello-world
Explanation:
- Pulls the
hello-world
image - Runs it inside a new container
- Shows a success message
π§± 4. Create and Use a Dockerfile
Step-by-step: Let's Dockerize a Node.js app.
Project:
mkdir myapp && cd myapp npm init -y npm i express echo "require('express')().listen(3000)" > index.js
DockerFile :
# Base image FROM node:18 # Set working directory WORKDIR /app # Copy files COPY package*.json ./ RUN npm install COPY . . # Expose and run EXPOSE 3000 CMD ["node", "index.js"]
Build and Run:
docker build -t myapp . docker run -p 3000:3000 myapp
π¦ 5. Use Volumes (Dev Mode)
So changes reflect without rebuilds:
docker run -p 3000:3000 -v $(pwd):/app myapp
π 6. Docker Compose β Multi-Container Apps
Example: Node.js app + MongoDB
<pre> version: '3.9' services: app: build: . ports: - "3000:3000" volumes: - .:/app environment: - NODE_ENV=development depends_on: - mongo mongo: image: mongo volumes: - mongodata:/data/db ports: - "27017:27017" volumes: mongodata: </pre>
*** Run: ***
docker-compose up --build
π‘7. Multi-Stage Docker Builds
Reduce image size and isolate build from production:
# Stage 1: Build FROM node:18 AS builder WORKDIR /app COPY . . RUN npm install && npm run build # Stage 2: Runtime FROM node:18-slim WORKDIR /app COPY --from=builder /app/dist . CMD ["node", "server.js"]
π§° 8. Helpful Docker Commands
docker ps -a # List all containers docker images # List images docker logs <container> # Logs docker exec -it <container> bash # Enter container docker stop <id> # Stop container docker rm <id> # Remove container docker rmi <image> # Remove image
π 9. Docker Security Best Practices
β
Use minimal base images (alpine, node:slim)
β
Use .dockerignore
like .gitignore
β
Run as non-root if possible
β
Use image scanning tools:
docker scout quickview # or trivy image myapp
π 10. Real DevOps CI/CD with Docker
GitHub Actions Example:
<pre> name: Docker CI on: push: branches: [ main ] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Build Docker image run: docker build -t myorg/myapp:${{ github.sha }} . - name: Push to Docker Hub run: | echo "${{ secrets.DOCKER_PASS }}" | docker login -u ${{ secrets.DOCKER_USER }} --password-stdin docker push myorg/myapp:${{ github.sha }} </pre>
π§ͺ 11. Advanced Real-World Scenarios
π Run PostgreSQL + Adminer for Dev:
services: db: image: postgres environment: POSTGRES_PASSWORD: pass adminer: image: adminer ports: - "8080:8080"
π§© Host React or Next.js with Nginx:
FROM node:18 AS builder WORKDIR /app COPY . . RUN npm install && npm run build FROM nginx:alpine COPY --from=builder /app/build /usr/share/nginx/html
π§Ό 12. Clean Up Resources
docker system prune -a docker volume prune docker image prune
π§ Conclusion: Docker is DevOps
Once youβve mastered Docker:
- Build fast, reproducible workflows
- Deploy secure, production-ready images
- Use in CI/CD pipelines
- Plug into Kubernetes or serverless platforms
π¬ Want More?
Let me know in the comments if you want a follow-up:
- π§ Kubernetes for Docker users
- π οΈ Docker + Ansible + Terraform workflow
- π§ Docker debugging & profiling tools
π§βπ» Connect With Me
πΊ YouTube: Codewithimran
π GitHub: Codewithimran
π‘ LinkedIn: Codewithimran
Top comments (0)