Skip to content

Nuface Blog

隨意隨手記 Casual Notes

Menu
  • Home
  • About
  • Services
  • Blog
  • Contact
  • Privacy Policy
  • Login
Menu

Docker Architecture Insight: Should Each Project Have Its Own Database Container?

Posted on 2025-11-05 by Rico

When working with Docker-based applications, a common architectural question often comes up:

“If multiple projects need a database, should each project run its own database container,
or should all projects connect to a single shared database container?”

This question looks simple but touches on isolation, security, maintainability, and resource efficiency.
Here’s a summary of my practical observations and recommendations.


1. Architecture Comparison

AspectOne Database Container per ProjectShared Database Container for All Projects
IsolationExcellent — issues in one project (e.g., high load or full disk) won’t affect others.Low — one misbehaving app can impact all others.
SecurityEach project has its own credentials and clear access boundaries.Permissions are managed inside one DB engine; easy to misconfigure.
Version ControlEach project can upgrade or roll back independently.All must share the same version; upgrades require coordination.
Backup / RestoreEasy to back up or restore a single project’s data.Partial restore is difficult; may affect other projects.
Resource UsageHeavier — each instance runs its own engine and cache.More efficient — shared memory and buffer pools.
DeploymentSelf-contained: docker-compose up brings up app + DB easily.Requires an existing DB service and credentials.
MaintenanceMore containers to monitor, back up, and upgrade.Simpler to manage, but also a single point of failure.

2. Which Option to Choose?

🔹 Production Environments

  • For critical systems, use separate database instances (at least separate containers, ideally separate hosts).
    • Example: ERP, email, or core APIs.
    • Benefits: independent upgrades, backups, and troubleshooting; failure isolation.
  • For lightweight or internal tools, sharing a single DB engine is fine if you ensure:
    • Each project has its own DB/schema and user credentials.
    • Principle of least privilege is enforced.
    • Backups can be done per database.

🔹 Development and Testing

  • Each project should have its own DB container for simplicity and reproducibility.
    • A single docker-compose up spins up a full environment with no data conflicts.
  • If resources are limited, a shared DB is acceptable — but automate cleanup to avoid data pollution.

3. Practical Recommendations

1️⃣ Persistent Storage

Always store DB data in volumes or host directories, not inside ephemeral containers:

-v /data/postgres/appA:/var/lib/postgresql/data

2️⃣ Resource Limits

Use Docker’s resource controls to avoid a runaway DB consuming all host resources:

--cpus=2 --memory=4g

3️⃣ Backup & Recovery

Schedule regular automatic backups and practice restoration.
Use tools like pg_dump, mysqldump, or PITR (Point-in-Time Recovery).

4️⃣ Monitoring

Implement Prometheus + Grafana (or exporters) to track:
CPU usage, connections, slow queries, I/O latency, backup status, etc.

5️⃣ Network Isolation

Each project can have its own Docker network:

  • Only app and DB communicate within it.
  • Do not expose 3306/5432 ports externally.
  • Optionally restrict access via iptables or firewall rules.

6️⃣ Secrets Management

Never hardcode credentials in Dockerfiles or Compose files.
Use Docker secrets, environment variables, or tools like Vault.


4. Example Configurations

📦 One Database per Project (Recommended)

services:
  app:
    image: myapp:latest
    environment:
      DB_HOST: db
      DB_USER: appuser
      DB_PASS: secret
    depends_on: [db]

  db:
    image: postgres:16
    volumes:
      - app_pgdata:/var/lib/postgresql/data

volumes:
  app_pgdata:

🏗 Shared Database for Multiple Projects (Use with Caution)

services:
  db:
    image: postgres:16
    volumes:
      - shared_pg:/var/lib/postgresql/data

  appA:
    environment:
      DB_NAME: appa
      DB_USER: appa
      DB_PASS: passA

  appB:
    environment:
      DB_NAME: appb
      DB_USER: appb
      DB_PASS: passB

volumes:
  shared_pg:

5. My Experience

In my own environment (running mail servers, DNS, EIP, and internal AI services on Docker/Proxmox),
I’ve settled on a hybrid strategy:

  • Production: Each critical project has its own dedicated DB container (or host).
    • Independent volumes, networks, credentials, and backups.
  • Development: Each project’s Compose file includes its own DB for easy startup and teardown.
  • Lightweight utilities: May share a central DB instance.
  • All DB containers have CPU/memory limits, automated backups, and monitoring.

This approach keeps maintenance predictable, upgrades safe, and failure isolation clear.


6. Conclusion

To put it simply:

Share during development, isolate in production.

If your system stores valuable or long-lived data,
give it its own database container.
It’s safer, easier to maintain, and makes scaling or restoring much simpler.


Further Reading

  • Docker Docs – Volumes and Persistent Storage
  • PostgreSQL – Physical and Logical Backups
  • MySQL – Using Docker for MySQL Server

Recent Posts

  • Postfix + Let’s Encrypt + BIND9 + DANE Fully Automated TLSA Update Guide
  • Postfix + Let’s Encrypt + BIND9 + DANE TLSA 指紋自動更新完整教學
  • Deploying DANE in Postfix
  • 如何在 Postfix 中部署 DANE
  • DANE: DNSSEC-Based TLS Protection

Recent Comments

  1. Building a Complete Enterprise-Grade Mail System (Overview) - Nuface Blog on High Availability Architecture, Failover, GeoDNS, Monitoring, and Email Abuse Automation (SOAR)
  2. Building a Complete Enterprise-Grade Mail System (Overview) - Nuface Blog on MariaDB + PostfixAdmin: The Core of Virtual Domain & Mailbox Management
  3. Building a Complete Enterprise-Grade Mail System (Overview) - Nuface Blog on Daily Operations, Monitoring, and Performance Tuning for an Enterprise Mail System
  4. Building a Complete Enterprise-Grade Mail System (Overview) - Nuface Blog on Final Chapter: Complete Troubleshooting Guide & Frequently Asked Questions (FAQ)
  5. Building a Complete Enterprise-Grade Mail System (Overview) - Nuface Blog on Network Architecture, DNS Configuration, TLS Design, and Postfix/Dovecot SNI Explained

Archives

  • December 2025
  • November 2025
  • October 2025

Categories

  • AI
  • Apache
  • Cybersecurity
  • Database
  • DNS
  • Docker
  • Fail2Ban
  • FileSystem
  • Firewall
  • Linux
  • LLM
  • Mail
  • N8N
  • OpenLdap
  • OPNsense
  • PHP
  • QoS
  • Samba
  • Switch
  • Virtualization
  • VPN
  • WordPress
© 2025 Nuface Blog | Powered by Superbs Personal Blog theme