Skip to content

Conversation

@krishvsoni
Copy link

Add Docker Compose Volume Persistence

Summary

This PR adds persistent volumes to docker-compose.yml to prevent data loss when running docker-compose down. Previously, both the PostgreSQL database and media files were lost on container shutdown, requiring users to run load_initial_data on every restart.

Changes

PostgreSQL Data Persistence

Added a named volume postgres_data to persist the database across container restarts:

  • Maps to /var/lib/postgresql/data/ inside the container
  • Eliminates the need to reload database fixtures after docker-compose down

Media Files Persistence

Added a named volume bakerydemo_media to persist uploaded images and media files:

  • Maps to /code/bakerydemo/media inside the container
  • Fixes the issue where images would not load after restarting containers

Fixed Docker Entrypoint Script

Converted docker-entrypoint.sh line endings from CRLF to LF:

  • Windows CRLF line endings were causing container startup failures
  • Linux containers require Unix-style LF line endings for shell scripts
  • Fixed error: "exec /code/docker-entrypoint.sh: no such file or directory"

Removed Conflicting Volume Mount

Removed the ./bakerydemo:/code/bakerydemo volume mount from the app service:

  • This mount was overwriting container files and breaking the entrypoint
  • Only the media directory needs to be persisted, not the entire application code
  • Application code is properly baked into the Docker image during build

Technical Details

Modified docker-compose.yml:

  1. Added volume mount to the db service for PostgreSQL data persistence
  2. Added media volume mount to the app service
  3. Removed the development code mount that was causing conflicts
  4. Declared both named volumes at the root level

Modified docker-entrypoint.sh:

  1. Converted line endings from CRLF to LF for Linux compatibility

Testing Steps

  1. Build and start containers: docker-compose up -d
  2. Run migrations: docker-compose exec app python manage.py migrate
  3. Load initial data: docker-compose exec app python manage.py load_initial_data
  4. Verify site works at http://localhost:8000
  5. Stop containers: docker-compose down
  6. Restart containers: docker-compose up -d
  7. Verify data persists without re-running load_initial_data

Impact

Users can now run docker-compose down without losing:

  • Database records and schema
  • Uploaded media files and images

The load_initial_data management command only needs to be run once during initial setup, not after every container restart.

Fixes

Closes #207

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add docker-compose volumne

1 participant