top of page

Login Form with Flask, MongoDB, and Docker

Updated: Sep 9, 2025

Step 1: Install Docker

  1. Go to Docker Desktop.

  2. Download Docker Desktop for Windows (since you’re on Windows).

  3. Install it → follow setup wizard.

  4. After install, open PowerShell/Command Prompt and run:

    docker --version

    If you see version info → Docker is installed successfully.

Step 2: Setup Project Folder

Create a new folder for your app:

mkdir flask-login-app
cd flask-login-app

Inside this folder, create files like this:

flask-login-app/
 ├── app.py
 ├── requirements.txt
 ├── templates/
 │    └── login.html
 ├── Dockerfile

Step 3: Create HTML Login Form

Create templates/login.html:

<!DOCTYPE html>
<html>
<head>
    <title>Login Form</title>
</head>
<body>
    <h2>Login Page</h2>
    <form action="/login" method="POST">
        <label>Username:</label>
        <input type="text" name="username" required><br><br>

        <label>Password:</label>
        <input type="password" name="password" required><br><br>

        <button type="submit">Login</button>
    </form>
</body>
</html>

Step 4: Python Flask App

Create app.py:

from flask import Flask, render_template, request, redirect
from pymongo import MongoClient

app = Flask(__name__)

# Connect to MongoDB (running locally in Compass)
client = MongoClient("mongodb://host.docker.internal:27017/")  # Docker → local MongoDB
db = client["loginDB"]
users = db["users"]

@app.route("/")
def home():
    return render_template("login.html")

@app.route("/login", methods=["POST"])
def login():
    username = request.form["username"]
    password = request.form["password"]

    # Check if user exists in DB
    user = users.find_one({"username": username, "password": password})

    if user:
        return "Welcome {username}, you are logged in!"
    else:
        return "Invalid username or password"

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000, debug=True)

Step 5: Requirements File

Create requirements.txt:

flask
pymongo

Step 6: Dockerfile

Create Dockerfile:

# Use official Python image
FROM python:3.9

# Set working directory
WORKDIR /app

# Copy requirements and install
COPY requirements.txt .
RUN pip install -r requirements.txt

# Copy project
COPY . .

# Run Flask app
CMD ["python", "app.py"]

Step 7: Build Docker Image

In project folder, run:

docker build -t flask-login-app .

Step 8: Run Flask Container

docker run -d -p 5000:5000 flask-login-app

Now open browser → http://localhost:5000 → you’ll see the login form.

$50

Product Title

Product Details goes here with the simple product description and more information can be seen by clicking the see more button. Product Details goes here with the simple product description and more information can be seen by clicking the see more button

$50

Product Title

Product Details goes here with the simple product description and more information can be seen by clicking the see more button. Product Details goes here with the simple product description and more information can be seen by clicking the see more button.

$50

Product Title

Product Details goes here with the simple product description and more information can be seen by clicking the see more button. Product Details goes here with the simple product description and more information can be seen by clicking the see more button.

Recommended Products For This Post
 
 
 

Comments


© 2023 by newittrendzzz.com 

  • Facebook
  • Twitter
  • Instagram
bottom of page