Program 3
Program 3
Design, Deploy and manage a micro services architecture on your local machine using
Docker and Docker-compose.
Step 1: Prerequisites
microservices-lab/
│
├── backend/
│ ├── Dockerfile
│ ├── app.py
│ └── requirements.txt
│
├── frontend/
│ ├── Dockerfile
│ ├── app.py
│ └── requirements.txt
│
└── docker-compose.yml
Flask==2.1.1
Werkzeug==2.0.3
Create the app.py file for the backend (Flask app). This service will simply return a message
like "Hello, World!" when accessed.
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World! From Backend!'
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=5000)
Create the Dockerfile for the Frontend (Flask-based). This service will make an HTTP
request to the backend and display the result.
Create the requirements.txt file to specify the Python packages for Flask.
Flask==2.1.1
requests==2.26.0
Create the app.py file for the Frontend (Flask app). This service will call the Hello Service
API and display the result.
app = Flask(__name__)
@app.route('/')
def hello_world():
response = requests.get('http://hello-service:5000') # Communicate with hello-service
return f"Frontend says: {response.text}"
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=5001)
Now, let’s define the Docker Compose file to manage both services (Backend and
Frontend).
docker-compose.yml
version: '3.8'
services:
backend:
build:
context: ./backend # Path to the hello-service directory
ports:
- "5000:5000" # Expose the hello-service on port 5000
networks:
- app-network
frontend:
build:
context: ./frontend # Path to the frontend directory
ports:
- "5001:5001" # Expose the frontend on port 5001
networks:
- app-network
depends_on:
- backend # Ensure hello-service starts first
networks:
app-network:
driver: bridge
Now that everything is set up, let's build and run the containers using Docker Compose.
docker-compose up --build
1. Open your browser and go to http://localhost:5001. You should see the message:
This means the Frontend Service successfully called the Hello Service.
2. If you visit http://localhost:5000 (directly hitting the Backend), you will see the
message:
Step 8: Clean Up
Once you are done, you can stop the services by running:
docker-compose down