DEV Community

Ray
Ray

Posted on

Deploying BillingWatch to production with Docker and Stripe webhooks

BillingWatch is a self-hosted billing anomaly detector designed specifically for Stripe subscriptions. In this article, we'll cover how to deploy it in production using Docker and Stripe webhooks.

Prerequisites

Before starting, ensure you have:

  • Docker installed on your system
  • A Stripe account with webhooks enabled
  • The BillingWatch repository cloned locally

Step 1: Set up Docker

To set up the environment for our self-hosted BillingWatch instance, we'll create a Dockerfile in the project directory.

# Create a new file named Dockerfile
touch Dockerfile
Enter fullscreen mode Exit fullscreen mode

In this Dockerfile, add the following content:

FROM python:3.9-slim

# Install required dependencies
RUN pip install --upgrade pip && \
    pip install -r requirements.txt

# Set environment variables
ENV DATABASE_URL=<DATABASE_URL>
ENV STRIPE_WEBHOOK_SECRET=<STRIPE_WEBHOOK_SECRET>

# Copy application code into the container
COPY app.py .

# Expose port for web server
EXPOSE 5000

# Run commands to start web server
CMD ["gunicorn", "app:app", "--workers", "3", "--bind", ":5000"]
Enter fullscreen mode Exit fullscreen mode

Replace <DATABASE_URL> and <STRIPE_WEBHOOK_SECRET> with your actual database URL and Stripe webhook secret.

Step 2: Configure Docker Compose

Next, we'll create a docker-compose.yml file to set up our development environment.

# Create a new file named docker-compose.yml
touch docker-compose.yml
Enter fullscreen mode Exit fullscreen mode

Add the following content:

version: '3'

services:
  app:
    build: .
    ports:
      - "5000:5000"
    environment:
      DATABASE_URL: ${DATABASE_URL}
      STRIPE_WEBHOOK_SECRET: ${STRIPE_WEBHOOK_SECRET}
    depends_on:
      - db

  db:
    image: postgres
Enter fullscreen mode Exit fullscreen mode

Step 3: Configure Stripe Webhooks

To receive notifications from Stripe, you'll need to configure your webhooks. In the Stripe dashboard, go to Developers > Webhooks, then click Add endpoint and enter the following details:

  • Name: BillingWatch Webhook
  • URL: <YOUR_SERVER_URL>/stripe/webhook
  • Events: Invoice.created, Invoice.paid, Invoice.updated

Replace <YOUR_SERVER_URL> with your actual server URL.

Step 4: Deploy to Production

Once you've set up your Docker environment and configured Stripe webhooks, follow these steps:

  1. Build the Docker image using docker build -t <IMAGE_NAME> .
  2. Run the container in detached mode using docker run --name <CONTAINER_NAME> -p 5000:5000 <IMAGE_NAME>
  3. Configure environment variables and database connections as needed

Conclusion

With these steps, you should now have a self-hosted BillingWatch instance deployed to production using Docker and Stripe webhooks.

Top comments (0)