Deploying a Flask App with Docker and PostgreSQL on Google Cloud
In today’s fast-paced tech landscape, deploying applications efficiently is crucial for developers. Flask, a lightweight web framework for Python, paired with Docker and PostgreSQL, offers a powerful stack for building and deploying scalable web applications. In this article, we’ll walk through the process of deploying a Flask app using Docker and PostgreSQL on Google Cloud Platform (GCP). Whether you're new to cloud deployment or looking to refine your skills, this guide covers everything you need to know.
Understanding the Components
Flask
Flask is a micro web framework for Python that is easy to use and flexible for developers. It’s perfect for small to medium-sized applications and is known for its simplicity.
Docker
Docker is a platform that allows you to automate the deployment of applications inside lightweight containers. This means that your app can run consistently across different environments.
PostgreSQL
PostgreSQL is a powerful, open-source object-relational database system. It is known for its robustness, performance, and support for advanced data types.
Google Cloud Platform (GCP)
Google Cloud offers a suite of cloud computing services that make it easy to deploy and manage applications. Services such as Google Compute Engine and Google Kubernetes Engine enhance deployment efficiency.
Prerequisites
Before diving into the deployment process, ensure you have the following:
- Basic knowledge of Python and Flask.
- Docker installed on your local machine.
- A Google Cloud account.
- PostgreSQL installed locally for development.
Step-by-Step Guide to Deploying a Flask App
1. Setting Up Your Flask Application
First, let’s create a simple Flask application. Create a new directory for your project and navigate into it:
mkdir flask-docker-postgres
cd flask-docker-postgres
Now, create a file named app.py
:
from flask import Flask
import os
app = Flask(__name__)
@app.route('/')
def hello():
return "Hello, World!"
if __name__ == '__main__':
app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 5000)))
2. Creating a Requirements File
Create a requirements.txt
file to specify the dependencies:
Flask==2.0.1
psycopg2-binary==2.9.1
3. Dockerizing Your Flask App
Next, we need to create a Dockerfile
to define how our application will run inside a Docker container. Create a file named Dockerfile
:
# Use the official Python image from the Docker Hub
FROM python:3.9-slim
# Set the working directory
WORKDIR /app
# Copy the requirements file
COPY requirements.txt .
# Install the dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Copy the application code
COPY app.py .
# Expose the port
EXPOSE 5000
# Command to run the app
CMD ["python", "app.py"]
4. Creating a Docker Compose File
To simplify the management of multiple containers (Flask app and PostgreSQL), we can use Docker Compose. Create a file named docker-compose.yml
:
version: '3.8'
services:
db:
image: postgres:13
environment:
POSTGRES_DB: mydatabase
POSTGRES_USER: myuser
POSTGRES_PASSWORD: mypassword
ports:
- "5432:5432"
web:
build: .
ports:
- "5000:5000"
depends_on:
- db
5. Building and Running Your Containers
With your Dockerfile and Docker Compose file ready, you can build and run your containers. Run the following command in your terminal:
docker-compose up --build
This will build the Docker image for your Flask application and start both the Flask app and PostgreSQL database.
6. Setting Up Google Cloud
Now that your application runs locally, it’s time to deploy it on Google Cloud.
- Set up Google Cloud SDK on your local machine.
- Create a new project in the Google Cloud Console.
- Enable the required APIs for Google Compute Engine.
7. Deploying to Google Cloud
- Authenticate your Google Cloud SDK:
bash
gcloud auth login
- Create a Google Cloud Storage bucket (optional, for static files):
bash
gsutil mb gs://your-bucket-name
- Push your Docker image to Google Container Registry:
bash
docker tag your-image gcr.io/your-project-id/your-image
docker push gcr.io/your-project-id/your-image
- Deploy the application using Google Cloud Run:
bash
gcloud run deploy --image gcr.io/your-project-id/your-image --platform managed
- Follow the prompts to choose a region and allow unauthenticated invocations.
8. Accessing Your Application
Once the deployment is complete, Google Cloud will provide a URL where you can access your Flask application. Open this URL in your web browser, and you should see “Hello, World!” displayed.
Troubleshooting Common Issues
- Database Connection Errors: Ensure that your Flask app is correctly configured to connect to the PostgreSQL instance.
- Docker Build Failures: Check your Dockerfile for typos and ensure all dependencies in
requirements.txt
are correct. - GCP Permissions: Make sure your Google Cloud account has the necessary permissions to deploy applications.
Conclusion
Deploying a Flask app with Docker and PostgreSQL on Google Cloud is a powerful way to leverage modern web technologies. By following this guide, you’ve learned how to set up your application, create a Docker environment, and deploy it in the cloud. With these skills, you can build robust applications that are ready for production. Happy coding!