This project demonstrates how to build a simple Python web application using Flask, Docker, and Docker Compose. The project creates a containerized Python app that can be run locally or deployed to a cloud environment.
This repository contains a basic Flask web application. The app simply returns "Hello, Docker!" when accessed via a web browser. Docker is used to containerize the application, making it easy to run in any environment.
- Python: Programming language used for building the Flask application.
- Flask: A lightweight web framework for Python.
- Docker: Containerization tool used to build and run the application in isolated environments.
- Docker Compose (Optional): To orchestrate the application and its dependencies.
.
├── app.py # Main Flask application
├── Dockerfile # Instructions to build the Docker image
├── requirements.txt # Python dependencies
└── README.md # Project documentation (this file)
app.py
: Contains the Flask application code.Dockerfile
: Used by Docker to build a containerized version of the application.requirements.txt
: Lists the dependencies required by the app (Flask and Werkzeug).
Before you begin, ensure you have met the following requirements:
-
Docker installed on your machine.
-
Python installed (optional for local testing without Docker).
git clone https://github.com/yourusername/myfirstpythonapp.git
cd myfirstpythonapp
Create a Dockerfile
in your project root with the following content:
# Use an official Python runtime as a parent image
FROM python:3.8-slim
# Set the working directory in the container
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# Install the required packages
RUN pip install --no-cache-dir -r requirements.txt
# Expose the port the app runs on
EXPOSE 80
# Run the app
CMD ["python", "app.py"]
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return "Hello, Docker!"
if __name__ == '__main__':
app.run(host='0.0.0.0', port=80)
List the required Python dependencies:
Flask==2.0.3
Werkzeug==2.0.3
Run the following command to build the Docker image:
docker build -t myfirstpythonapp .
Once the image is built, you can run it using:
docker run -p 4000:80 myfirstpythonapp
Now, navigate to http://localhost:4000
in your browser to see the app running!
docker build -t myfirstpythonapp .
docker run -p 4000:80 myfirstpythonapp
To stop the container, first find the container ID using:
docker ps
Then stop it using:
docker stop <container_id>
You can use Docker Compose to simplify running multiple services, such as a database alongside your app. Create a docker-compose.yml
file:
version: '3'
services:
app:
build: .
ports:
- "4000:80"
To run the app using Docker Compose:
docker-compose up
Dockerfile cannot be empty
: Ensure that theDockerfile
is present in the project root directory and has valid content.ImportError: cannot import name 'url_quote' from 'werkzeug.urls'
: This is due to incompatibility between Flask and Werkzeug. Ensure you are using compatible versions (Flask==2.0.3
andWerkzeug==2.0.3
).
- Use
docker logs <container_id>
to view logs from your container. - Use
docker exec -it <container_id> /bin/sh
to open a shell inside the running container for debugging.