0% found this document useful (0 votes)
12 views3 pages

Ass 3

This document provides a step-by-step guide to create and deploy a simple Flask application using Docker. It covers prerequisites, application setup, Dockerfile creation, image building, and running the container, along with optional steps for pushing the image to a registry and deploying it. The assignment emphasizes the importance of containerization in modern DevOps and cloud-native development.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

Ass 3

This document provides a step-by-step guide to create and deploy a simple Flask application using Docker. It covers prerequisites, application setup, Dockerfile creation, image building, and running the container, along with optional steps for pushing the image to a registry and deploying it. The assignment emphasizes the importance of containerization in modern DevOps and cloud-native development.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

### **Step 1: Prerequisites**

1. **Install Docker**: Ensure Docker is installed on your system.


- Download and install Docker from
[https://www.docker.com/](https://www.docker.com/).
2. **Python Installed**: Ensure Python is installed on your system.
3. **Flask Installed**: Install Flask using pip:
```bash
pip install Flask
```

---

### **Step 2: Create a Simple Flask Application**


1. Create a project directory:
```bash
mkdir flask-app
cd flask-app
```
2. Create a Python file (`app.py`) for your Flask application:
```python
from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello_world():
return 'Hello, Dockerized Flask App!'

if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
```

---

### **Step 3: Create a `requirements.txt` File**


1. Generate a `requirements.txt` file to list dependencies:
```bash
pip freeze > requirements.txt
```
Ensure it includes Flask:
```
Flask==2.3.2
```

---

### **Step 4: Create a Dockerfile**


1. Create a `Dockerfile` in the project directory:
```Dockerfile
# Use an official Python runtime as the base image
FROM python:3.9-slim

# Set the working directory in the container


WORKDIR /app

# Copy the requirements file into the container


COPY requirements.txt .

# Install the dependencies


RUN pip install --no-cache-dir -r requirements.txt

# Copy the rest of the application code


COPY . .

# Expose the port the app runs on


EXPOSE 5000

# Command to run the application


CMD ["python", "app.py"]
```

---

### **Step 5: Build the Docker Image**


1. Build the Docker image:
```bash
docker build -t flask-app .
```
- `flask-app` is the name of the Docker image.
- `.` refers to the current directory (where the Dockerfile is located).

---

### **Step 6: Run the Docker Container**


1. Run the Docker container:
```bash
docker run -d -p 5000:5000 flask-app
```
- `-d`: Run the container in detached mode (in the background).
- `-p 5000:5000`: Map port 5000 on your local machine to port 5000 in the
container.

---

### **Step 7: Test the Application**


1. Open your browser and go to `http://localhost:5000`.
2. You should see the message: **"Hello, Dockerized Flask App!"**.

---

### **Step 8: Push the Docker Image to a Registry (Optional)**


1. Tag the Docker image:
```bash
docker tag flask-app your-dockerhub-username/flask-app:latest
```
2. Log in to Docker Hub:
```bash
docker login
```
3. Push the image to Docker Hub:
```bash
docker push your-dockerhub-username/flask-app:latest
```

---

### **Step 9: Deploy the Container (Optional)**


1. You can deploy the container to a cloud platform like AWS, Google Cloud, or
Azure.
2. Use Kubernetes or Docker Swarm for orchestration if deploying multiple
containers.

---

### **Step 10: Clean Up**


1. Stop the running container:
```bash
docker stop <container-id>
```
2. Remove the container:
```bash
docker rm <container-id>
```
3. Remove the Docker image:
```bash
docker rmi flask-app
```

---

### **Summary**
- You created a simple Flask application.
- You containerized it using Docker by writing a `Dockerfile`.
- You built and ran the Docker container.
- You tested the application and optionally pushed the image to a Docker registry.

This assignment demonstrates how to containerize a Python application with Flask


and deploy it using Docker, a fundamental skill in modern DevOps and cloud-native
development.

You might also like