Certainly! Here's a README file to help you run the Python application using 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.
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)
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>
Edit the app.py
file to update the connection_string
variable with the appropriate connection details for your SQL Server database.
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.
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.
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
.
/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)
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
.
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.