Initial commit for Docker project
This commit is contained in:
commit
de4f3134f7
58
README.md
Normal file
58
README.md
Normal file
@ -0,0 +1,58 @@
|
||||
# Docker Web Application
|
||||
|
||||
## Prerequisites
|
||||
- Docker installed on the system.
|
||||
|
||||
## Application Description
|
||||
This is a simple Flask web application running inside a Docker container. The app accepts user input via a form and displays the submitted data.
|
||||
|
||||
## Docker Setup
|
||||
|
||||
1. **Preparation**
|
||||
Run the following command to set up the environment:
|
||||
```bash
|
||||
./prepare-app.sh
|
||||
```
|
||||
|
||||
2. **Start the Application**
|
||||
Start the application with:
|
||||
```bash
|
||||
./start-app.sh
|
||||
```
|
||||
The app will be available at: [http://localhost:5000](http://localhost:5000)
|
||||
|
||||
3. **Stop the Application**
|
||||
To stop the application:
|
||||
```bash
|
||||
./stop-app.sh
|
||||
```
|
||||
|
||||
4. **Remove the Application**
|
||||
To remove all Docker containers, networks, and volumes:
|
||||
```bash
|
||||
./remove-app.sh
|
||||
```
|
||||
|
||||
## Working Example
|
||||
1. Prepare the application:
|
||||
```bash
|
||||
./prepare-app.sh
|
||||
```
|
||||
2. Start the app:
|
||||
```bash
|
||||
./start-app.sh
|
||||
```
|
||||
3. Visit [http://localhost:5000](http://localhost:5000) in a browser.
|
||||
|
||||
4. Stop the app:
|
||||
```bash
|
||||
./stop-app.sh
|
||||
```
|
||||
|
||||
5. Remove the app:
|
||||
```bash
|
||||
./remove-app.sh
|
||||
```
|
||||
|
||||
## Conclusion
|
||||
This application demonstrates Docker containerization of a Flask app with basic form handling.
|
18
docker-compose.yml
Normal file
18
docker-compose.yml
Normal file
@ -0,0 +1,18 @@
|
||||
version: '3.8'
|
||||
services:
|
||||
flask-app:
|
||||
build: .
|
||||
ports:
|
||||
- "5000:5000"
|
||||
networks:
|
||||
- flask-network
|
||||
restart: unless-stopped
|
||||
# You can add other services here, like a database if required
|
||||
|
||||
networks:
|
||||
flask-network:
|
||||
driver: bridge
|
||||
|
||||
volumes:
|
||||
flask-data:
|
||||
driver: local
|
21
dockerfile
Normal file
21
dockerfile
Normal file
@ -0,0 +1,21 @@
|
||||
# Use an official Python runtime as a parent image
|
||||
FROM python:3.8-slim
|
||||
|
||||
# Set the working directory inside the container
|
||||
WORKDIR /app
|
||||
|
||||
# Copy the current directory contents into the container at /app
|
||||
COPY . /app
|
||||
|
||||
# Install Flask
|
||||
RUN pip install --no-cache-dir flask
|
||||
|
||||
# Set the environment variable for Flask
|
||||
ENV FLASK_APP=testapp.py
|
||||
ENV FLASK_RUN_HOST=0.0.0.0
|
||||
|
||||
# Expose port 5000 for the Flask app
|
||||
EXPOSE 5000
|
||||
|
||||
# Run the Flask application
|
||||
CMD ["flask", "run", "--host=0.0.0.0", "--port=5000"]
|
11
prepare-app.sh
Executable file
11
prepare-app.sh
Executable file
@ -0,0 +1,11 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Create a Docker network
|
||||
echo "Creating Docker network..."
|
||||
docker network create flask-network
|
||||
|
||||
# Create a named volume for persistent storage (for example, for database)
|
||||
echo "Creating Docker volume..."
|
||||
docker volume create flask-data
|
||||
|
||||
echo "Preparation complete!"
|
15
remove-app.sh
Executable file
15
remove-app.sh
Executable file
@ -0,0 +1,15 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Remove the Flask app container
|
||||
echo "Removing Flask app container..."
|
||||
docker rm flask-app
|
||||
|
||||
# Remove Docker network
|
||||
echo "Removing Docker network..."
|
||||
docker network rm flask-network
|
||||
|
||||
# Remove Docker volume
|
||||
echo "Removing Docker volume..."
|
||||
docker volume rm flask-data
|
||||
|
||||
echo "App removed successfully!"
|
9
start-app.sh
Executable file
9
start-app.sh
Executable file
@ -0,0 +1,9 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Start the Flask app container
|
||||
echo "Starting Flask app container..."
|
||||
docker run -d --restart unless-stopped --name flask-app --network flask-network -p 5000:5000 flask-app
|
||||
|
||||
# Optionally, start any additional services (like a database) here if needed
|
||||
|
||||
echo "App is running at http://localhost:5000"
|
9
stop-app.sh
Executable file
9
stop-app.sh
Executable file
@ -0,0 +1,9 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Stop the Flask app container
|
||||
echo "Stopping Flask app container..."
|
||||
docker stop flask-app
|
||||
|
||||
# Optionally, stop other services like the database here
|
||||
|
||||
echo "App stopped successfully!"
|
26
testapp.py
Normal file
26
testapp.py
Normal file
@ -0,0 +1,26 @@
|
||||
from flask import Flask, render_template, request
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
@app.route('/', methods=['GET', 'POST'])
|
||||
def home():
|
||||
if request.method == 'POST':
|
||||
college_name = request.form['college_name']
|
||||
student_name = request.form['student_name']
|
||||
course_name = request.form['course_name']
|
||||
year_of_study = request.form['year_of_study']
|
||||
|
||||
return f"College: {college_name}, Student: {student_name}, Course: {course_name}, Year: {year_of_study}"
|
||||
|
||||
return '''
|
||||
<form method="POST">
|
||||
College Name: <input type="text" name="college_name"><br>
|
||||
Student Name: <input type="text" name="student_name"><br>
|
||||
Course Name: <input type="text" name="course_name"><br>
|
||||
Year of Study: <input type="text" name="year_of_study"><br>
|
||||
<input type="submit" value="Submit">
|
||||
</form>
|
||||
'''
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(debug=True, host="0.0.0.0", port=5000) # This allows the app to run inside the container
|
Loading…
Reference in New Issue
Block a user