DEV Community

SOVANNARO
SOVANNARO

Posted on

3 Microservices, 1 YAML File, 1 Command: The Power of Docker Compose

πŸ› οΈ Problem: Too Many docker run Commands

Right now, you have Docker images for three microservices:

  • accounts
  • loans
  • cards

To run each one, you’d normally do:

docker run -p <port>:<port> <image-name>
Enter fullscreen mode Exit fullscreen mode

But imagine doing that for 100 microservices or even just multiple instances.
πŸ’‘ Manually running each with docker run becomes slow and painful.


βœ… Solution: Use Docker Compose

What is Docker Compose?

Docker Compose is a tool that lets you:

  • Define all microservices in one YAML file.
  • Start or stop all services with just one command.

πŸ‘‰ Instead of typing docker run multiple times, you write everything once in a file called docker-compose.yml.


βš™οΈ What Can Docker Compose Do?

  • Start all microservices with docker compose up
  • Stop everything with docker compose down
  • Set memory limits
  • Link services together with a shared network
  • View logs, restart services, and more

Docker Compose is installed automatically with Docker Desktop


πŸ“„ Let's Create a docker-compose.yml

Put the file anywhere you want (e.g., in your accounts project folder).

Step-by-step Structure:

services:
  accounts:
    image: "your-username/accounts:s4"
    container_name: accounts-ms
    ports:
      - "8080:8080"
    deploy:
      resources:
        limits:
          memory: 700m
    networks:
      - easybank

  loans:
    image: "your-username/loans:s4"
    container_name: loans-ms
    ports:
      - "8090:8090"
    deploy:
      resources:
        limits:
          memory: 700m
    networks:
      - easybank

  cards:
    image: "your-username/cards:s4"
    container_name: cards-ms
    ports:
      - "9000:9000"
    deploy:
      resources:
        limits:
          memory: 700m
    networks:
      - easybank

networks:
  easybank:
    driver: bridge
Enter fullscreen mode Exit fullscreen mode

πŸ” Explanation:

Key Meaning
services: Section where you define each microservice
image: The Docker image to use (add your Docker Hub username)
container_name: Give your container a readable name
ports: Map internal ports to your machine
deploy > resources > limits: Restrict memory usage
networks: Allow microservices to talk to each other
easybank (at bottom): Creates a shared network all services use

🀝 Why Use networks:?

Without a shared network, microservices can’t talk to each other.
Adding them to the same network (like easybank) enables communication.


βœ… Final Steps

To check if Docker Compose is installed:

docker compose version
Enter fullscreen mode Exit fullscreen mode

If not, visit the Docker Compose install page and follow the steps for your OS.


🎯 What’s Next?

Now that your docker-compose.yml is ready, you can:

Start all services with:

docker compose up
Enter fullscreen mode Exit fullscreen mode

Stop all services with:

docker compose down
Enter fullscreen mode Exit fullscreen mode

🧠 Summary

  • docker-compose.yml lets you manage all services in one file.
  • You define images, ports, memory limits, and networks.
  • Use docker compose up to launch everything with one command.
  • It saves time and avoids manual repetition.

Top comments (0)