- Add external volume mounting for database, uploads, and media files - Update Dockerfile to conditionally initialize database only if it doesn't exist - Move database to external /data volume for persistence across rebuilds - Configure production environment with proper FLASK_CONFIG - Add volume mappings for complete data persistence: * ./data:/data - Database persistence * ./uploads:/opt/moto_site/uploads - File uploads persistence * ./static/media:/opt/moto_site/static/media - Media files persistence - Create init script that skips database creation if DB already exists - Enable safe app updates without losing users, posts, or tracks This ensures all user data persists across Docker container rebuilds and app updates.
49 lines
1.4 KiB
Docker
49 lines
1.4 KiB
Docker
|
|
FROM python:3.11-slim
|
|
|
|
# Set working directory for the app
|
|
WORKDIR /opt/moto_site
|
|
|
|
# Install system dependencies
|
|
RUN apt-get update && apt-get install -y \
|
|
gcc \
|
|
postgresql-client \
|
|
&& rm -rf /var/lib/apt/lists/*
|
|
|
|
# Copy requirements and install Python dependencies
|
|
COPY requirements.txt ./
|
|
RUN pip install --no-cache-dir -r requirements.txt
|
|
|
|
# Copy all application code to /opt/moto_site
|
|
COPY . .
|
|
|
|
# Create uploads directory
|
|
RUN mkdir -p uploads/images uploads/gpx
|
|
|
|
# Set FLASK_APP so Flask CLI commands are available
|
|
ENV FLASK_APP=run.py
|
|
|
|
# Create a script to conditionally initialize database
|
|
RUN echo '#!/bin/sh\n\
|
|
if [ ! -f /data/moto_adventure.db ]; then\n\
|
|
echo "Database not found, initializing..."\n\
|
|
flask --app run.py init-db\n\
|
|
flask --app run.py create-admin\n\
|
|
echo "Database initialized successfully"\n\
|
|
else\n\
|
|
echo "Database already exists, skipping initialization"\n\
|
|
fi' > /opt/moto_site/init_db_if_needed.sh && chmod +x /opt/moto_site/init_db_if_needed.sh
|
|
|
|
# Create non-root user and set permissions
|
|
RUN adduser --disabled-password --gecos '' appuser && \
|
|
chown -R appuser:appuser /opt/moto_site && \
|
|
mkdir -p /data && \
|
|
chown -R appuser:appuser /data
|
|
USER appuser
|
|
|
|
# Expose port
|
|
EXPOSE 5000
|
|
|
|
# Run the application with Gunicorn, looking for run:app in /opt/moto_site
|
|
ENTRYPOINT ["/bin/sh", "-c", "/opt/moto_site/init_db_if_needed.sh && exec gunicorn --bind 0.0.0.0:5000 run:app"]
|