๐ฌ "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โ
Top comments (1)
Great stuff!!