modern-django-starter/Dockerfile
Django Template e1fd61561e Fix Docker static files permissions for production deployment
## Issue Fixed:
- Static files collection failing with permission error in Docker container
- Error: `PermissionError: [Errno 13] Permission denied: '/app/staticfiles/js'`
- Occurs when running `python manage.py collectstatic` in production

## Solution Applied:
1. **Enhanced Dockerfile**:
   - Create `/app/staticfiles` and `/app/media` directories explicitly
   - Set proper ownership with `chown -R django:django /app`
   - Ensures directories exist with correct permissions before user switch

2. **Improved entrypoint.sh**:
   - Added `mkdir -p` to ensure directories exist at runtime
   - Creates directories before attempting static files collection
   - Provides fallback if directories weren't created in Docker build

3. **Updated Documentation**:
   - Added troubleshooting section for static files permission error
   - Explains the fix and prevention methods

## Benefits:
-  Resolves Docker container permission issues
-  Works with non-root user (security best practice)
-  Handles both build-time and runtime directory creation
-  Maintains proper file ownership for Django operations

This should resolve the collectstatic permission error in Dokploy deployment.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-11 16:22:16 +05:30

50 lines
1.2 KiB
Docker

FROM python:3.11-slim as base
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
postgresql-client \
build-essential \
libpq-dev \
gettext \
curl \
&& curl -fsSL https://deb.nodesource.com/setup_18.x | bash - \
&& apt-get install -y nodejs \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY requirements/base.txt /app/requirements/
RUN pip install --no-cache-dir -r requirements/base.txt
FROM base as development
COPY requirements/development.txt /app/requirements/
RUN pip install --no-cache-dir -r requirements/development.txt
COPY . /app/
EXPOSE 8000
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
FROM base as production
COPY requirements/production.txt /app/requirements/
RUN pip install --no-cache-dir -r requirements/production.txt
RUN groupadd -r django && useradd -r -g django django
COPY . /app/
# Create directories that need write permissions
RUN mkdir -p /app/staticfiles /app/media && \
chown -R django:django /app
USER django
EXPOSE 8000
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "--workers", "3", "django_project.wsgi:application"]