Learn how to route Python PyMongo database traffic through a QuotaGuard Static IP using QGTunnel.
RUN pip install --no-cache-dir pymongo
RUN curl https://s3.amazonaws.com/quotaguard/qgtunnel-latest.tar.gz | tar xzQUOTAGUARDSTATIC_URL=... bin/qgtunnel python app.pyBe sure to set QUOTAGUARDSTATIC_URL to your proxy URL from the QuotaGuard Dashboard.
docker build -t qg-static-python-mongo-pymongo-example .
docker run -e QUOTAGUARDSTATIC_URL=... -e MONGO_URI=... qg-static-python-mongo-pymongo-exampleimport os
import pymongo
from pymongo.errors import ConnectionFailure
MONGO_URI = os.getenv("MONGO_URI")
def test_mongo_connection():
try:
client = pymongo.MongoClient(MONGO_URI, serverSelectionTimeoutMS=5000)
client.server_info() # Will attempt to connect to the server
print("Connection Success.")
except ConnectionFailure as e:
print(f"Connection failed: {e}")
if __name__ == "__main__":
test_mongo_connection()
# Use an official Python runtime as a parent image
FROM python:latest
# Set the working directory in the container
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# Install the required Python packages
RUN pip install --no-cache-dir pymongo
# Download and extract QGTunnel software
RUN curl https://s3.amazonaws.com/quotaguard/qgtunnel-latest.tar.gz | tar xz
# Command to run when the container starts, assuming the environment variable is passed at runtime
CMD ["bin/qgtunnel", "python", "app.py"]