DEV Community

SOVANNARO
SOVANNARO

Posted on

πŸš€ Learn to Start and Stop All Microservices with Docker Compose

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
Enter fullscreen mode Exit fullscreen mode
  • -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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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)