Module 8 Lesson 2: Docker Compose for Dev
Master the local dev stack. Learn how to use Docker Compose to manage dependencies like databases and caches alongside your application code.
Module 8 Lesson 2: Docker Compose for Local Development
When developing locally, your docker-compose.yml serves as your "Dependency Map." It defines everything your app needs to function that isn't the code itself.
1. The "Thin App" Pattern
The most common pattern is to run your "Databases" in Docker but run your "App" natively on your laptop for the fastest debugging.
# docker-compose.yml
services:
db:
image: postgres:15
ports:
- "5432:5432"
environment:
POSTGRES_PASSWORD: development_pass
cache:
image: redis:alpine
ports:
- "6379:6379"
Result: You have a production-grade database running in 1 second. Your code (running in VS Code or IntelliJ) connects to localhost:5432 just like it would if you had installed Postgres manually.
2. Using docker-compose.override.yml
Docker Compose has a secret weapon: Automatic Merging.
If you have a file named docker-compose.override.yml, Compose will automatically merge it with your main file.
docker-compose.yml: Contains settings that work for everyone (e.g., the images).docker-compose.override.yml: Contains settings specific to YOUR laptop (e.g., mapping your local folder to a volume).
Tip: You usually put the override file in .gitignore so every developer can have their own personal settings.
3. Speeding Up with --profile
What if your project has 20 services, but you are only working on the "Search" feature today? You can group services into Profiles.
services:
app:
# ...
database:
# ...
search-engine:
profiles: ["search"]
heavy-batch-process:
profiles: ["background"]
- Running
docker-compose upwill only startappanddatabase. - Running
docker-compose --profile search upwill start the search engine too.
4. Managing Logs
In dev, you want to see what's happening.
docker-compose logs -f: Streams logs from ALL services.docker-compose logs -f app: Only shows your app's logs.
Exercise: The Personal Override
- Create a simple
docker-compose.ymlwith one service (nginx) using a static image. - Now create a
docker-compose.override.ymland add aportsmapping to port9000:80. - Run
docker-compose up -d. - Check
localhost:9000. Did it work? - Run
docker-compose config. This shows you the "Final Result" of how Docker merged both files. Very useful for debugging!
Summary
Docker Compose is the "glue" that holds your local environment together. By using overrides and profiles, you can tailor your infrastructure to your specific task without breaking it for the rest of your team.
Next Lesson: See changes instantly: Hot reloading and bind mounts.