Module 5 Lesson 5: Real-World Multi-Container Example
The capstone of the basics. Build a complete Voting App architecture with a Python worker, a Redis queue, a Postgres DB, and a Node.js results page.
Module 5 Lesson 5: Real-World Multi-Container Example
To wrap up our basics, let’s look at a "Real World" architecture. This is a classic example used in the Docker community to show how different languages and databases interact.
1. The Architecture
We want to build a Voting App:
- Voting-UI (Python): A web page where users click "Dogs" or "Cats".
- Redis (Queue): The vote is sent to a fast in-memory queue.
- Worker (.NET or Java): Reads the vote from Redis and saves it to a database.
- DB (Postgres): Permanent storage for the votes.
- Result-UI (Node.js): A dashboard that shows the current tally.
2. The Final docker-compose.yml
version: "3"
services:
vote:
image: dockersamples/examplevotingapp_vote:before
ports:
- "5000:80"
networks:
- front-tier
- back-tier
redis:
image: redis:alpine
networks:
- back-tier
worker:
image: dockersamples/examplevotingapp_worker
networks:
- back-tier
db:
image: postgres:9.4
volumes:
- "db-data:/var/lib/postgresql/data"
networks:
- back-tier
result:
image: dockersamples/examplevotingapp_result:before
ports:
- "5001:80"
networks:
- front-tier
- back-tier
networks:
front-tier:
back-tier:
volumes:
db-data:
3. Key Observations
- Multiple Networks: Notice how
redis,worker, anddbare only on theback-tier. They cannot be reached by a user on the internet. - Public Ports: Only the
vote(port 5000) andresult(port 5001) are published to the host. - Independence: You can swap the
.NETworker for aGoworker, and as long as it talks to the same Redis/Postgres hostnames, the system doesn't care.
4. Troubleshooting the Stack
If the result page shows 0 votes even though you clicked:
- Check
docker-compose logs worker. Is it connecting to Redis? - Is the
dbservice healthy? - Are all containers on the same
back-tiernetwork?
Exercise: The Architect's Challenge
- Design: If you wanted to add a "Load Balancer" (like Nginx) in front of the
voteapp to handle 10,000 users, where would it go in the YAML file? - Persistence: If the
resultapp needs to save CSV exports of the votes, where should it save them? (Hint: add a new volume). - Security: Why is it a good thing that the
voteservice is NOT on the same network as thedbservice directly?
Conclusion of Module 5
You have mastered Docker Compose. You can now build, link, and manage complex systems that previously would have taken a whole team of SysAdmins to configure.
Next Module: We dive into the "Production Grade" topics: Module 6: Advanced Dockerfile Techniques (Multi-stage builds and more).