This content originally appeared on DEV Community and was authored by Yash Sonawane
“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
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
backend/Dockerfile
FROM node:18
WORKDIR /app
COPY . .
RUN npm install
EXPOSE 3000
CMD ["node", "index.js"]
Run It All
In the project root:
docker-compose up -d
Both MongoDB and your backend are up, connected, and running.
Useful Docker Compose Commands
Start all services:
docker-compose up -d
Stop all services:
docker-compose down
View logs:
docker-compose logs -f
Rebuild (if code changes):
docker-compose up --build
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β
This content originally appeared on DEV Community and was authored by Yash Sonawane