DEV Community

araf
araf

Posted on

🐳 Docker & Docker Compose for Beginners: Full Guide + Real Project Example

Whether you’re a backend developer, DevOps enthusiast, or just getting into microservices, Docker is one of the most essential tools in the modern development toolbox.

This guide will walk you through:

  • What Docker & Docker Compose are
  • Why Docker became so popular
  • How to install Docker
  • How to use Docker & Docker Compose
  • Real-world project: Node.js + MongoDB

Let’s dive in πŸ‘‡


πŸ”§ What is Docker?

Docker is a platform for packaging applications into containers β€” lightweight, portable units that bundle your app with its dependencies.

Imagine this:

"It works on my machine" – Docker makes that your reality everywhere.

Each container includes:

  • Code
  • Runtime (e.g., Node.js, Java)
  • Libraries
  • Environment/configuration

So your app runs the same on any system, cloud, or CI/CD pipeline.


πŸš€ Why Docker Became So Popular

Docker took over the world for several key reasons:

βœ… Lightning Fast – Containers start in milliseconds vs full VMs
βœ… Environment Consistency – Same behavior in dev, staging, and prod
βœ… DevOps & CI/CD Friendly – Works seamlessly with GitHub Actions, Jenkins, GitLab CI
βœ… Perfect for Microservices – Each service in its own container
βœ… Portable – Works on Windows, Linux, Mac, cloud platforms
βœ… Vibrant Ecosystem – Docker Hub provides 100,000+ ready-to-use images


βš™οΈ Docker vs Traditional Deployment

Feature Traditional Deployment Docker-Based Deployment
Setup time Manual setup, often inconsistent One-line setup (docker run)
Environment conflicts Very common Fully isolated per container
Portability OS/Env dependent Runs anywhere Docker is installed
Scaling services Hard & manual Easy with docker-compose or K8s
Dev β†’ Staging β†’ Prod Error-prone Consistent containers everywhere

🧰 How to Install Docker & Docker Compose

πŸ”Ή 1. Install Docker

docker --version
Enter fullscreen mode Exit fullscreen mode

You should see something like:

Docker version 24.x.x, build abcdef
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή 2. Docker Compose (usually pre-installed)

Check with:

docker compose version
Enter fullscreen mode Exit fullscreen mode

If it shows the version, you're good to go!


πŸ“¦ Real Example: Node.js + MongoDB with Docker Compose

Let’s build a simple project that runs a Node.js API connected to MongoDB β€” all inside Docker.


🧱 Project Structure

docker-node-mongo-app/
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ server.js
β”‚   └── package.json
β”œβ”€β”€ Dockerfile
└── docker-compose.yml
Enter fullscreen mode Exit fullscreen mode

✏️ Step 1: Node.js Code (app/server.js)

const express = require('express');
const mongoose = require('mongoose');

const app = express();

mongoose.connect('mongodb://mongo:27017/mydb', {
  useNewUrlParser: true,
  useUnifiedTopology: true,
});

app.get('/', (req, res) => {
  res.send('Hello from Dockerized Node.js + MongoDB!');
});

const PORT = 3000;
app.listen(PORT, () => {
  console.log(`Server running on http://localhost:${PORT}`);
});
Enter fullscreen mode Exit fullscreen mode

✏️ Step 2: app/package.json

{
  "name": "docker-node-mongo",
  "version": "1.0.0",
  "main": "server.js",
  "scripts": {
    "start": "node server.js"
  },
  "dependencies": {
    "express": "^4.18.2",
    "mongoose": "^7.2.2"
  }
}
Enter fullscreen mode Exit fullscreen mode

🐳 Step 3: Dockerfile

FROM node:18

WORKDIR /app

COPY app/package*.json ./
RUN npm install

COPY app .

EXPOSE 3000

CMD ["npm", "start"]
Enter fullscreen mode Exit fullscreen mode

πŸ›  Step 4: docker-compose.yml

version: '3.8'

services:
  app:
    build: .
    ports:
      - "3000:3000"
    depends_on:
      - mongo

  mongo:
    image: mongo
    ports:
      - "27017:27017"
    volumes:
      - mongo-data:/data/db

volumes:
  mongo-data:
Enter fullscreen mode Exit fullscreen mode

▢️ How to Run the Project

  1. Open terminal in your project root.
  2. Run the following:
docker-compose up --build
Enter fullscreen mode Exit fullscreen mode
  1. Visit: πŸ“ http://localhost:3000 You’ll see: Hello from Dockerized Node.js + MongoDB!

🧹 How to Stop & Clean Up

To stop the app:

docker-compose down
Enter fullscreen mode Exit fullscreen mode

To remove the volume data as well:

docker-compose down -v
Enter fullscreen mode Exit fullscreen mode

🎯 Recap

By now, you've learned:

βœ… What Docker and Docker Compose are
βœ… Why Docker became a standard in modern development
βœ… How to install and check Docker setup
βœ… How to containerize an app
βœ… How to run multi-container apps with Compose


πŸ’¬ What's Next?

You can now dockerize:

  • Python Flask/Django apps
  • Spring Boot + PostgreSQL apps
  • React/Next.js with NGINX

Top comments (0)