DEV Community

Yash Sonawane
Yash Sonawane

Posted on

๐Ÿ›ณ๏ธ Docker Series: Episode 8 โ€” Docker Compose: Build Multi-Container Apps Like a Boss

๐ŸŽฌ "Tired of typing long docker run commands for every container? Wish you could launch your entire app stack with one file and one command? Meet Docker Compose โ€” the superhero of multi-container development."


๐Ÿง  What is Docker Compose?

Docker Compose lets you define and run multi-container apps using a simple YAML file.

Instead of typing multiple docker run commands, you:

  • Create a docker-compose.yml file
  • Define services (containers)
  • Run everything with: docker-compose up

๐Ÿงฑ Sample Project: Node.js + MongoDB

Letโ€™s say you have:

  • A backend Node.js app
  • A MongoDB database

Hereโ€™s how to spin up both with Compose.


๐Ÿ“ Project Structure

my-app/
โ”œโ”€โ”€ docker-compose.yml
โ”œโ”€โ”€ backend/
โ”‚   โ”œโ”€โ”€ Dockerfile
โ”‚   โ””โ”€โ”€ index.js
Enter fullscreen mode Exit fullscreen mode

๐Ÿงพ docker-compose.yml

version: '3.8'

services:
  mongo:
    image: mongo
    container_name: mongo
    ports:
      - "27017:27017"

  backend:
    build: ./backend
    container_name: backend
    ports:
      - "3000:3000"
    depends_on:
      - mongo
    environment:
      - MONGO_URL=mongodb://mongo:27017
Enter fullscreen mode Exit fullscreen mode

๐Ÿ› ๏ธ backend/Dockerfile

FROM node:18
WORKDIR /app
COPY . .
RUN npm install
EXPOSE 3000
CMD ["node", "index.js"]
Enter fullscreen mode Exit fullscreen mode

๐Ÿš€ Run It All

In the project root:

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

๐ŸŽ‰ Both MongoDB and your backend are up, connected, and running.


๐Ÿงฐ Useful Docker Compose Commands

Start all services:

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

Stop all services:

docker-compose down
Enter fullscreen mode Exit fullscreen mode

View logs:

docker-compose logs -f
Enter fullscreen mode Exit fullscreen mode

Rebuild (if code changes):

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

๐Ÿ” Why Use Docker Compose?

  • ๐Ÿ“ฆ Reproducibility: Define your entire stack in one file
  • โšก Speed: One command to build, run, stop
  • ๐Ÿค Connectivity: Services can talk via service name (e.g. mongo)

โœ… Bonus Tips

  • Use .env files to manage environment variables
  • Use volumes: in Compose to persist data
  • Use profiles: to control dev/staging/test environments

๐Ÿ”ฎ Up Next: Docker Compose + Volumes + Networks (Real Project Setup)

In Episode 9, weโ€™ll:

  • Add named volumes and custom networks to our Compose file
  • Run a full-stack project with frontend, backend, and DB

๐Ÿ’ฌ Letโ€™s Build Together

Have you used Docker Compose before?
Want help creating your own docker-compose.yml?

Drop your config or questions below โ€” Iโ€™m here to help you Docker smarter.

โค๏ธ If this helped you simplify your Docker life, hit like, comment, or share it with your dev community.

๐ŸŽฌ Next: โ€œDocker Compose: Real-World Setup with Volumes + Networks + Frontendโ€

Top comments (1)

Collapse
 
kinsflow profile image
Kingsley Akindele

Great stuff!!