docker.recipes

Astro + PostgreSQL

intermediate

Content-focused framework with PostgreSQL for dynamic content.

Overview

Astro is a modern web framework designed for building content-focused websites with optimal performance. Created by the team at The Astro Technology Company, it pioneered the 'islands architecture' concept where interactive components are isolated islands of interactivity in a sea of static HTML. Astro ships zero JavaScript by default, only loading JS for components that actually need it, resulting in blazingly fast sites that score perfect Lighthouse performance audits. Unlike traditional SPAs, Astro renders pages to static HTML at build time while still supporting dynamic components from React, Vue, Svelte, or any other UI framework. This stack combines Astro's content-first approach with PostgreSQL's robust relational database capabilities to create dynamic, data-driven websites that maintain exceptional performance. PostgreSQL serves as the backend data store for content management, user-generated data, analytics, and complex relational queries, while Astro handles the presentation layer with its optimized static site generation and selective hydration. The combination enables building everything from documentation sites with dynamic search to e-commerce platforms with product catalogs, all while maintaining Astro's core performance benefits. This combination is ideal for content creators, agencies, and businesses who need the reliability and query power of PostgreSQL but don't want to sacrifice the performance and SEO benefits that Astro provides. It's particularly valuable for projects that have outgrown simple static sites but don't need the complexity of a full SPA, offering a sweet spot between static site simplicity and database-driven functionality with enterprise-grade data integrity.

Key Features

  • Island architecture with zero JavaScript by default, only hydrating interactive components that need client-side behavior
  • Multi-framework component support allowing React, Vue, Svelte, and other UI frameworks within the same Astro project
  • Content Collections with TypeScript-safe frontmatter validation for structured content management
  • Server-side rendering with Astro adapters while maintaining static-first philosophy for optimal performance
  • PostgreSQL's ACID compliance ensuring data integrity for user-generated content and transactional operations
  • Advanced PostgreSQL JSON/JSONB support for flexible content schemas alongside traditional relational data
  • Built-in Astro dev server with hot module replacement and fast refresh during development
  • PostgreSQL full-text search capabilities with ranking and highlighting for content discovery

Common Use Cases

  • 1Documentation sites with dynamic search, user authentication, and contribution tracking stored in PostgreSQL
  • 2Content marketing websites with blog posts, case studies, and lead capture forms backed by relational data
  • 3E-commerce product catalogs using Astro's static generation for product pages with PostgreSQL inventory management
  • 4SaaS marketing sites with dynamic pricing tables, feature comparisons, and customer testimonials from database
  • 5Portfolio websites with project galleries, contact forms, and analytics tracking using PostgreSQL storage
  • 6News and magazine sites with article management, author profiles, and comment systems requiring database relationships
  • 7Corporate websites with dynamic team directories, press releases, and investor relations data from PostgreSQL

Prerequisites

  • Docker and Docker Compose installed with at least 1.5GB available RAM for PostgreSQL operation
  • Node.js knowledge for Astro development and understanding of static site generation concepts
  • Basic SQL skills for PostgreSQL database design, queries, and data modeling
  • Port 4321 available for Astro development server and port 5432 free for PostgreSQL connections
  • Understanding of environment variables for database connection configuration and secrets management
  • Familiarity with at least one UI framework (React, Vue, Svelte) if building interactive components

For development & testing. Review security settings, change default credentials, and test thoroughly before production use. See Terms

docker-compose.yml

docker-compose.yml
1services:
2 astro:
3 build: .
4 container_name: astro
5 environment:
6 DATABASE_URL: postgresql://${DB_USER}:${DB_PASSWORD}@postgres:5432/${DB_NAME}
7 ports:
8 - "4321:4321"
9 depends_on:
10 - postgres
11 networks:
12 - astro-network
13
14 postgres:
15 image: postgres:16-alpine
16 environment:
17 POSTGRES_DB: ${DB_NAME}
18 POSTGRES_USER: ${DB_USER}
19 POSTGRES_PASSWORD: ${DB_PASSWORD}
20 volumes:
21 - postgres_data:/var/lib/postgresql/data
22 networks:
23 - astro-network
24
25volumes:
26 postgres_data:
27
28networks:
29 astro-network:
30 driver: bridge

.env Template

.env
1DB_NAME=astro
2DB_USER=astro
3DB_PASSWORD=changeme

Usage Notes

  1. 1Docs: https://docs.astro.build/
  2. 2Access at http://localhost:4321
  3. 3Island architecture: ship zero JS by default
  4. 4Use React, Vue, Svelte, or any UI framework together
  5. 5SSG by default, SSR with adapter
  6. 6Content Collections for type-safe Markdown/MDX

Individual Services(2 services)

Copy individual services to mix and match with your existing compose files.

astro
astro:
  build: .
  container_name: astro
  environment:
    DATABASE_URL: postgresql://${DB_USER}:${DB_PASSWORD}@postgres:5432/${DB_NAME}
  ports:
    - "4321:4321"
  depends_on:
    - postgres
  networks:
    - astro-network
postgres
postgres:
  image: postgres:16-alpine
  environment:
    POSTGRES_DB: ${DB_NAME}
    POSTGRES_USER: ${DB_USER}
    POSTGRES_PASSWORD: ${DB_PASSWORD}
  volumes:
    - postgres_data:/var/lib/postgresql/data
  networks:
    - astro-network

Quick Start

terminal
1# 1. Create the compose file
2cat > docker-compose.yml << 'EOF'
3services:
4 astro:
5 build: .
6 container_name: astro
7 environment:
8 DATABASE_URL: postgresql://${DB_USER}:${DB_PASSWORD}@postgres:5432/${DB_NAME}
9 ports:
10 - "4321:4321"
11 depends_on:
12 - postgres
13 networks:
14 - astro-network
15
16 postgres:
17 image: postgres:16-alpine
18 environment:
19 POSTGRES_DB: ${DB_NAME}
20 POSTGRES_USER: ${DB_USER}
21 POSTGRES_PASSWORD: ${DB_PASSWORD}
22 volumes:
23 - postgres_data:/var/lib/postgresql/data
24 networks:
25 - astro-network
26
27volumes:
28 postgres_data:
29
30networks:
31 astro-network:
32 driver: bridge
33EOF
34
35# 2. Create the .env file
36cat > .env << 'EOF'
37DB_NAME=astro
38DB_USER=astro
39DB_PASSWORD=changeme
40EOF
41
42# 3. Start the services
43docker compose up -d
44
45# 4. View logs
46docker compose logs -f

One-Liner

Run this command to download and set up the recipe in one step:

terminal
1curl -fsSL https://docker.recipes/api/recipes/astro-postgres/run | bash

Troubleshooting

  • Astro build fails with 'Cannot connect to database': Ensure PostgreSQL container is fully started before Astro attempts connection, add health checks or wait conditions
  • PostgreSQL connection refused on port 5432: Check that DATABASE_URL uses 'postgres' hostname (container name) not 'localhost' for inter-container communication
  • Astro pages showing stale data: Astro caches aggressively in production builds, implement proper cache invalidation or use SSR adapter for dynamic content
  • Memory issues with PostgreSQL container: Increase Docker memory limits and tune PostgreSQL shared_buffers and work_mem settings for your available resources
  • Astro islands not hydrating properly: Verify client:load or client:visible directives are properly set on interactive components and check browser console for hydration errors
  • Database connection pool exhausted: Configure PostgreSQL max_connections and implement proper connection pooling in Astro database client configuration

Community Notes

Loading...
Loading notes...

Download Recipe Kit

Get all files in a ready-to-deploy package

Includes docker-compose.yml, .env template, README, and license

Ad Space