DEV Community

Imran
Imran

Posted on

πŸš€ Master Docker: A DevOps-Grade Tutorial for Developers and Engineers 🐳


πŸ“Œ Why Docker?

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 
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

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"] 
Enter fullscreen mode Exit fullscreen mode

Build and Run:

docker build -t myapp . docker run -p 3000:3000 myapp 
Enter fullscreen mode Exit fullscreen mode

πŸ“¦ 5. Use Volumes (Dev Mode)

So changes reflect without rebuilds:

docker run -p 3000:3000 -v $(pwd):/app myapp 
Enter fullscreen mode Exit fullscreen mode

πŸ” 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> 
Enter fullscreen mode Exit fullscreen mode

*** Run: ***

docker-compose up --build 
Enter fullscreen mode Exit fullscreen mode

πŸ’‘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"] 
Enter fullscreen mode Exit fullscreen mode

🧰 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 
Enter fullscreen mode Exit fullscreen mode

πŸ” 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 
Enter fullscreen mode Exit fullscreen mode

πŸ”„ 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> 
Enter fullscreen mode Exit fullscreen mode

πŸ§ͺ 11. Advanced Real-World Scenarios

πŸ“Œ Run PostgreSQL + Adminer for Dev:

services: db: image: postgres environment: POSTGRES_PASSWORD: pass adminer: image: adminer ports: - "8080:8080" 
Enter fullscreen mode Exit fullscreen mode

🧩 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 
Enter fullscreen mode Exit fullscreen mode

🧼 12. Clean Up Resources

docker system prune -a docker volume prune docker image prune 
Enter fullscreen mode Exit fullscreen mode

🧠 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)