Welcome to the guide on Dockerizing a Flask application! This README provides a comprehensive guide on setting up, building, and running a Dockerized Flask app, along with some useful Docker commands.
Here is the directory structure of the Flask project:
Flask_App/
βββ AI_Service/
β βββ gemini_response_text.py
β βββ gemini_response_image.py
βββ Backend/
β βββ app.py
βββ Frontend/
β βββ static/
β β βββ Images/
β β β βββ icon.jpg
β β βββ style.css
β β βββ script.js
β βββ templates/
β βββ index.html
βββ uploads/
βββ Dockerfile
βββ .env
βββ requirements.txt
AI_Service/: Contains scripts for processing text and image responses.Backend/: Includes the main Flask application file.Frontend/: Holds static files (CSS, JavaScript, images) and HTML templates.uploads/: Directory for storing uploaded files.Dockerfile: Configuration file for building the Docker image..env: Environment variables.requirements.txt: Python dependencies.
Before Dockerizing, ensure your Flask application is set up correctly:
-
Create a virtual environment (optional but recommended):
python -m venv venv
-
Install dependencies:
pip install -r requirements.txt
-
Run the Flask app locally to verify everything works:
python Backend/app.py
Build the Docker image for your Flask app using:
docker build -t flask-app .Run the Docker container and map the local uploads directory to the containerβs uploads directory:
docker run -p 3000:3000 -v "$(pwd)/uploads:/app/uploads" flask-app-p 3000:3000: Maps port 3000 on your local machine to port 3000 in the container.-v "$(pwd)/uploads:/app/uploads": Shares theuploadsdirectory between your host and the container.
Once the container is running, access the Flask application at http://localhost:3000. π
Here are some essential Docker commands to manage your containers and images:
-
List Docker images:
docker images
-
List running containers:
docker ps
-
List all containers (including stopped ones):
docker ps -a
-
Remove a Docker image:
docker rmi -f image-name
-
View logs from a container:
docker logs container-id
-
Open an interactive terminal session inside a container:
docker exec -it container-id /bin/bash