Certainly! Here's a README file to help you run the Python application using Docker:

Running the Python Application with Docker

This guide will walk you through the process of building a Docker image and running a Python application using FastAPI, which interacts with a Microsoft SQL Server database using PyODBC. The application is containerized for easy deployment and scaling.

Prerequisites

Before getting started, make sure you have the following prerequisites installed on your system:

  • Docker
  • Docker Compose (usually included with Docker Desktop on Windows and Docker for Mac)

Step 1: Clone the Repository

Clone the application's source code from your version control system or download it as a zip archive and extract it to your local machine.

git clone <repository_url>
cd <repository_directory>

Step 2: Update Connection String

Edit the app.py file to update the connection_string variable with the appropriate connection details for your SQL Server database.

Step 3: Build the Docker Image

To build the Docker image, open a terminal, navigate to the project directory, and run the following command:

docker build -t my-python-app .

Replace my-python-app with a suitable name for your Docker image.

Step 4: Run the Docker Container

After successfully building the Docker image, you can run the application in a Docker container with the following command:

docker run -p 8000:8000 my-python-app

Replace my-python-app with the name you provided in step 3.

The -p 8000:8000 option maps port 8000 on your host machine to the container's port 8000. You can change the host port if needed.

Step 5: Access the Application

Your Python application is now running in a Docker container. You can access it by opening a web browser or sending HTTP requests to http://localhost:8000.

API Endpoints

  • /tasks: List all tasks (GET)
  • /tasks/{task_id}: Retrieve a single task by ID (GET)
  • /tasks: Create a new task (POST)
  • /tasks/{task_id}: Update an existing task by ID (PUT)
  • /tasks/{task_id}: Delete a task by ID (DELETE)

Cleaning Up

To stop and remove the Docker container, press Ctrl + C in the terminal where the container is running. Then, remove the container with:

docker rm -f <container_id>

Replace <container_id> with the actual container ID, which you can obtain from docker ps.

Conclusion

You've successfully built and run a Python application using Docker. Feel free to make changes to the application, rebuild the Docker image, and deploy it to your preferred environment.