In this lecture, you'll learn how to use Docker Compose to easily start and stop multiple microservices using just a few simple commands.
π§© Why Use Docker Compose?
Docker Compose helps you run multiple containers (like microservices) at once using a single command, rather than running them one-by-one with separate docker run
commands.
π The docker-compose.yml
File
Docker Compose needs a special file named docker-compose.yml
where you define all your services (containers). Without this file, the docker compose up
command won't work.
π Important: Run docker compose up
from the folder where the docker-compose.yml
file is located.
βΆοΈ Starting All Microservices
To start all the containers (microservices), run:
docker compose up -d
-
-d
means "detached mode" (runs in the background so your terminal stays free). - Example: If you're in the correct folder and run this command, it will start all services defined in the
docker-compose.yml
.
Then you can check that everything is running with:
docker ps
It will list all the running containers.
β Confirm Services Are Working
You can now test each microservice (like Accounts, Loans, Cards) using tools like Postman to send API requests. If you get a successful response, your services are working fine!
βοΈ Stopping All Microservices
To stop and remove all running containers, use:
docker compose down
This command stops and deletes all the containers created by Docker Compose.
π If you just want to stop them but not delete, use:
docker compose stop
However, itβs usually better to use down
so everything is cleaned up.
π Recap of Key Commands
Command | What It Does |
---|---|
docker compose up -d |
Starts all containers in background |
docker ps |
Shows running containers |
docker compose down |
Stops and removes containers |
docker compose stop |
Only stops containers (doesnβt remove) |
β οΈ YAML File Tips
- Indentation in
docker-compose.yml
is very important. - Be careful with spaces β YAML files break easily if formatting is wrong.
- You can find the sample file in the GitHub repo.
π‘ Summary
With Docker Compose, you can:
- Start all microservices with one command β
- Stop and delete them easily β
- Save time compared to running containers one by one β
This makes it super efficient to manage your microservices during development.
Top comments (0)