7.3 KiB
Strapi with PostgreSQL and Astro Client - Docker Compose Setup
This setup provides a complete Docker Compose environment for running Strapi with PostgreSQL and an Astro frontend client.
Prerequisites
- Docker
- Docker Compose
Quick Start
-
Clone and navigate to the project directory
cd /path/to/your/project -
Install PostgreSQL dependencies
cd server npm install pg cd .. -
Generate secure environment variables
# Generate JWT secrets (you can use any secure random string generator) echo "JWT_SECRET=$(openssl rand -base64 32)" echo "ADMIN_JWT_SECRET=$(openssl rand -base64 32)" echo "APP_KEYS=$(openssl rand -base64 32),$(openssl rand -base64 32),$(openssl rand -base64 32),$(openssl rand -base64 32)" echo "API_TOKEN_SALT=$(openssl rand -base64 32)" echo "TRANSFER_TOKEN_SALT=$(openssl rand -base64 32)" -
Create a .env file (copy from docker-compose.env and update with your generated secrets)
cp docker-compose.env .env # Edit .env file with your generated secrets -
Start the services
docker-compose up -d -
Access the applications
- Strapi Admin Panel: http://localhost:1337/admin
- Strapi API: http://localhost:1337/api
- Astro Client: http://localhost:4321
Environment Variables Setup
Automatic Setup (Recommended)
Use the provided setup script to automatically configure environment variables:
./setup-env.sh
This script will:
- Copy
docker-compose.envto.env - Generate secure random secrets for JWT tokens and salts
- Set up all necessary environment variables
Manual Setup
If you prefer to set up environment variables manually:
-
Copy the environment template
cp docker-compose.env .env -
Edit the .env file
nano .env # or use your preferred editor -
Update sensitive values
- Replace all
your-*-here-change-this-in-productionvalues with secure random strings - Modify database credentials if needed
- Adjust URLs and ports as required
- Replace all
Environment Variables Reference
The following variables are available in docker-compose.env:
Database Configuration
DATABASE_CLIENT: Database type (postgres)DATABASE_HOST: Database host (postgres)DATABASE_PORT: Database port (5432)DATABASE_NAME: Database name (strapi)DATABASE_USERNAME: Database username (strapi)DATABASE_PASSWORD: Database password (strapi_password)DATABASE_SSL: SSL connection (false)
Strapi Configuration
NODE_ENV: Environment (development/production)JWT_SECRET: JWT signing secretADMIN_JWT_SECRET: Admin JWT signing secretAPP_KEYS: Application keys (comma-separated)API_TOKEN_SALT: API token saltTRANSFER_TOKEN_SALT: Transfer token saltHOST: Server host (0.0.0.0)PORT: Server port (1337)
Client Configuration
CLIENT_NODE_ENV: Client environment (development/production)STRAPI_URL: Strapi API URL (http://strapi:1337)
Security Notes
- The
.envfile is gitignored for security reasons - Never commit sensitive secrets to version control
- Use different secrets for development and production
- Regularly rotate secrets in production environments
Services
Strapi Application (Backend)
- Port: 1337
- Container: strapi-app
- Database: PostgreSQL
- Purpose: Headless CMS and API
Astro Client (Frontend)
- Port: 4321
- Container: astro-client
- Purpose: Frontend application
- Backend: Connects to Strapi API
PostgreSQL Database
- Port: 5432
- Container: strapi-postgres
- Database: strapi
- Username: strapi
- Password: strapi_password
Environment Variables
The following environment variables are configured:
Strapi Configuration
DATABASE_CLIENT: postgresDATABASE_HOST: postgres (Docker service name)DATABASE_PORT: 5432DATABASE_NAME: strapiDATABASE_USERNAME: strapiDATABASE_PASSWORD: strapi_passwordNODE_ENV: development
Client Configuration
NODE_ENV: developmentSTRAPI_URL: http://strapi:1337 (internal Docker network)
Volumes
postgres_data: Persistent PostgreSQL data./server/public/uploads: Strapi uploads directory./server/.tmp: Strapi temporary files./client/src: Astro source code (for development)./client/public: Astro public assets./client/astro.config.mjs: Astro configuration./client/tsconfig.json: TypeScript configuration
Useful Commands
# Start all services
docker-compose up -d
# View logs for specific services
docker-compose logs -f strapi
docker-compose logs -f client
docker-compose logs -f postgres
# View all logs
docker-compose logs -f
# Stop all services
docker-compose down
# Stop and remove volumes (WARNING: This will delete all data)
docker-compose down -v
# Rebuild and start
docker-compose up -d --build
# Rebuild specific service
docker-compose up -d --build client
# Access PostgreSQL directly
docker-compose exec postgres psql -U strapi -d strapi
# Access Strapi container
docker-compose exec strapi sh
# Access Astro client container
docker-compose exec client sh
Development Workflow
Hot Reload
Both Strapi and Astro support hot reloading in development mode:
- Changes to Strapi files will automatically restart the server
- Changes to Astro files will automatically reload the browser
API Communication
The Astro client can communicate with Strapi using:
- Internal Docker network:
http://strapi:1337 - External access:
http://localhost:1337
Example API call from Astro
// In your Astro component or page
const response = await fetch('http://strapi:1337/api/your-content-type');
const data = await response.json();
First Time Setup
- After starting the services, visit http://localhost:1337/admin
- Create your first admin user
- Configure your content types and permissions
- Visit http://localhost:4321 to see your Astro frontend
- Configure your Astro app to fetch data from Strapi
Production Considerations
For production deployment:
- Change all default passwords and secrets
- Use proper SSL certificates
- Set
NODE_ENV=productionfor both services - Configure proper backup strategies for PostgreSQL
- Use environment-specific configuration files
- Consider using Docker secrets for sensitive data
- Build the Astro app for production:
npm run build - Use a production-ready web server for the Astro build
Troubleshooting
Strapi won't start
- Check if PostgreSQL is running:
docker-compose logs postgres - Verify database connection settings
- Check if all environment variables are set correctly
Astro client won't start
- Check if Strapi is running:
docker-compose logs strapi - Verify the STRAPI_URL environment variable
- Check for port conflicts on 4321
Database connection issues
- Ensure PostgreSQL container is healthy:
docker-compose ps - Check database logs:
docker-compose logs postgres - Verify network connectivity between containers
Permission issues
- Ensure proper file permissions on mounted volumes
- Check if the Docker user has access to the project files
Network connectivity between services
- Verify all services are on the same network:
docker network ls - Check if services can reach each other using container names