Build Your Own Backend-as-a-Service with Self-hosted Supabase via Docker

Database tutorial - IT technology blog
Database tutorial - IT technology blog

Quick Start: Run Supabase in 5 Minutes

Want a quick test without reading too much? Just open your terminal and run these 4 commands. Ensure you have Docker and Docker Compose installed on your machine.

# 1. Clone the official repository
git clone --depth 1 https://github.com/supabase/supabase

# 2. Navigate to the docker directory
cd supabase/docker

# 3. Copy the example configuration file
cp .env.example .env

# 4. Pull images and start the containers
docker compose pull
docker compose up -d

Once finished, access http://localhost:8000. Log in with the username supabase and the default password this_password_is_insecure_and_should_be_updated. Make sure to change your password immediately to avoid data loss when deploying to a production server.

Anatomy of Supabase: What’s Under the Hood?

Don’t let the Dashboard UI fool you. Supabase isn’t just a single database “blob.” It’s a highly sophisticated ecosystem of interconnected microservices:

  • PostgreSQL: The heart of the system. Unlike Firebase’s NoSQL, Postgres offers powerful querying capabilities and absolute data integrity.
  • GoTrue (Auth): A Go-based service for user management and JWT issuance. It handles everything from email registration to various OAuth providers seamlessly.
  • PostgREST: This tool automatically turns your Postgres database into a RESTful API, saving you hundreds of hours of manual CRUD coding.
  • Realtime: Built on Elixir/Phoenix, it allows you to listen to database changes via WebSockets with extremely low latency.
  • Kong (API Gateway): The entry point that routes every request to the appropriate service.

Having used MySQL and MongoDB, I find Supabase’s biggest selling point to be the combination of traditional SQL power and a modern API approach.

Advanced Configuration: Essential Parameters to Update

Running the containers is just the beginning. For stable operation, you need to open the .env file and fine-tune the following parameters:

1. JWT and Password Security

Never use the default secrets. If these strings are leaked, attackers can forge tokens to gain admin access. Use a random string at least 32 characters long.

POSTGRES_PASSWORD=your_strong_password
JWT_SECRET=your_super_long_random_string
ANON_KEY=your_new_anon_key
SERVICE_ROLE_KEY=your_new_service_role_key

2. SMTP Connection for Email Delivery

Supabase cannot send confirmation emails without SMTP. I usually use Resend or SendGrid as they offer generous free tiers:

[email protected]
SMTP_HOST=smtp.resend.com
SMTP_PORT=587
SMTP_USER=resend
SMTP_PASS=re_your_api_key

Practical Tips for Self-hosting

After several real-world deployments, I’ve learned three painful lessons to prevent midnight server crashes.

RAM Usage Warning

Supabase is a RAM hog. At idle, the system consumes about 1.2GB – 1.5GB of RAM. On a 2GB VPS, the server can easily hang due to Out of Memory (OOM) issues.
Solution: Enable at least 4GB of Swap. If you don’t use Edge Functions or Vector, disable them in the docker-compose file to reduce the load.

Data Backup Strategy

Don’t rely solely on Docker Volumes. I usually set up a cron job to run pg_dump at 2 AM daily. The backup file is then pushed directly to S3 or Google Cloud Storage for safety.

Security with a Reverse Proxy

Never expose port 8000 directly to the internet. Put Supabase behind Nginx or Caddy. This makes SSL (HTTPS) management easier and filters out basic attack requests.

# Caddy configuration for reference
supabase.yourdomain.com {
    reverse_proxy localhost:8000
}

Why Choose Supabase Over Firebase?

Many developers wonder which one to choose. Here are three reasons why I lean towards Supabase:

  1. No Vendor Lock-in: With Firebase, you’re a tenant. With self-hosted Supabase, you’re the landlord. You have full control over migrating your data without being subject to Google’s pricing whims.
  2. The Power of SQL: Firestore makes complex queries like table JOINs or aggregations difficult. Postgres handles these effortlessly.
  3. Cost Control: For large projects, Firebase’s read/write fees can skyrocket uncontrollably. Self-hosting Supabase only costs a fixed monthly VPS fee (around $10-$20).

Self-hosted Supabase is a top-tier choice if you need a fast, powerful backend while maintaining full data control. Good luck with your setup. If you run into any deployment errors, leave a comment below for support!

Share: