π οΈ 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>
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
π 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
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
Stop all services with:
docker compose down
π§ 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)