Skip to content

fix: prestart.sh loaddata re-runs on every container restart#1502

Open
ayanaoide wants to merge 1 commit intomediacms-io:mainfrom
ayanaoide:main
Open

fix: prestart.sh loaddata re-runs on every container restart#1502
ayanaoide wants to merge 1 commit intomediacms-io:mainfrom
ayanaoide:main

Conversation

@ayanaoide
Copy link
Copy Markdown

Problem

When deploying with Docker, fixtures/categories.json and other loaddata fixtures are re-imported on every container restart, even on existing installations. This causes deleted sample data (e.g. default categories) to reappear after each restart.

Root Cause

prestart.sh detects existing installations with an exact string comparison:

EXISTING_INSTALLATION=`echo "from users.models import User; print(User.objects.exists())" |python manage.py shell`
if [ "$EXISTING_INSTALLATION" = "True" ]; then

When manage.py shell runs in non-interactive (pipe) mode, it outputs extra lines to stdout before the actual result:

44 objects imported automatically (use -v 2 for details).
True

So $EXISTING_INSTALLATION contains two lines and is never equal to "True" — the condition always fails, and the script treats every restart as a fresh installation.

Reproduced with:

docker compose exec web \
  bash -c 'echo "from users.models import User; print(User.objects.exists())" | python manage.py shell'

Output:

44 objects imported automatically (use -v 2 for details).
True

Fix

Redirect stderr to /dev/null and pipe through tail -1 to extract only the last line, leaving the original = "True" comparison intact:

EXISTING_INSTALLATION=`echo "from users.models import User; print(User.objects.exists())" |python manage.py shell 2>/dev/null | tail -1`
if [ "$EXISTING_INSTALLATION" = "True" ]; then

Minimal change — only 2>/dev/null | tail -1 is added. No logic is altered.

Testing

  1. Deploy with docker-compose
  2. Delete the default categories from the admin panel
  3. Restart containers — sample categories no longer reappear ✓
  4. Fresh install still works correctly — loaddata runs as expected ✓

fix: prestart.sh loaddata re-runs on every container restart

manage.py shell outputs extra lines to stdout in non-interactive mode (e.g. "44 objects imported automatically"), causing the exact string comparison with "True" to always fail.

Pipe through 2>/dev/null | tail -1 to discard noise and extract only the actual True/False value.
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.

1 participant