mirror of
https://github.com/DRYTRIX/TimeTracker.git
synced 2026-01-21 20:09:57 -06:00
83 lines
2.6 KiB
Docker
83 lines
2.6 KiB
Docker
FROM python:3.11-slim
|
|
|
|
# Install system dependencies
|
|
RUN apt-get update && apt-get install -y \
|
|
postgresql \
|
|
postgresql-contrib \
|
|
&& rm -rf /var/lib/apt/lists/*
|
|
|
|
# Set work directory
|
|
WORKDIR /app
|
|
|
|
# Install Python dependencies
|
|
COPY requirements.txt .
|
|
RUN pip install --no-cache-dir -r requirements.txt
|
|
|
|
# Copy application code
|
|
COPY . .
|
|
|
|
# Create necessary directories
|
|
RUN mkdir -p /data /app/logs /var/lib/postgresql/data /var/run/postgresql
|
|
|
|
# Create timetracker user and ensure postgres user exists
|
|
RUN useradd -m -u 1000 timetracker && \
|
|
chown -R timetracker:timetracker /app /data /app/logs && \
|
|
chown -R postgres:postgres /var/lib/postgresql/data /var/run/postgresql
|
|
|
|
# Create a simple startup script
|
|
RUN echo '#!/bin/bash\n\
|
|
set -e\n\
|
|
cd /app\n\
|
|
export FLASK_APP=app\n\
|
|
export DATABASE_URL=postgresql+psycopg2://timetracker@localhost:5432/timetracker\n\
|
|
\n\
|
|
echo "=== Starting TimeTracker ==="\n\
|
|
\n\
|
|
# Initialize PostgreSQL if needed\n\
|
|
if [ ! -f /var/lib/postgresql/data/PG_VERSION ]; then\n\
|
|
echo "Initializing PostgreSQL..."\n\
|
|
su - postgres -c "/usr/lib/postgresql/*/bin/initdb -D /var/lib/postgresql/data"\n\
|
|
echo "PostgreSQL initialized"\n\
|
|
fi\n\
|
|
\n\
|
|
# Start PostgreSQL in background\n\
|
|
echo "Starting PostgreSQL..."\n\
|
|
su - postgres -c "/usr/lib/postgresql/*/bin/postgres -D /var/lib/postgresql/data" &\n\
|
|
POSTGRES_PID=$!\n\
|
|
\n\
|
|
# Wait for PostgreSQL to be ready\n\
|
|
echo "Waiting for PostgreSQL..."\n\
|
|
sleep 10\n\
|
|
\n\
|
|
# Wait for PostgreSQL to actually be accepting connections\n\
|
|
echo "Checking PostgreSQL connection..."\n\
|
|
until su - postgres -c "/usr/bin/pg_isready -q"; do\n\
|
|
echo "PostgreSQL is not ready yet, waiting..."\n\
|
|
sleep 2\n\
|
|
done\n\
|
|
echo "PostgreSQL is ready!"\n\
|
|
\n\
|
|
# Create database and user\n\
|
|
echo "Setting up database..."\n\
|
|
su - postgres -c "/usr/bin/createdb timetracker" 2>/dev/null || echo "Database exists"\n\
|
|
su - postgres -c "/usr/bin/createuser -s timetracker" 2>/dev/null || echo "User exists"\n\
|
|
\n\
|
|
# Initialize schema if needed or if FORCE_REINIT is set\n\
|
|
if [ ! -f /var/lib/postgresql/data/.initialized ] || [ "$FORCE_REINIT" = "true" ]; then\n\
|
|
echo "Initializing database schema..."\n\
|
|
su - postgres -c "/usr/bin/psql -d timetracker -f /app/docker/init.sql"\n\
|
|
touch /var/lib/postgresql/data/.initialized\n\
|
|
echo "Schema initialized"\n\
|
|
fi\n\
|
|
\n\
|
|
# Start Flask app\n\
|
|
echo "Starting Flask application..."\n\
|
|
exec gunicorn --bind 0.0.0.0:8080 --worker-class eventlet --workers 1 --timeout 120 "app:create_app()"\n\
|
|
' > /app/start.sh && chmod +x /app/start.sh
|
|
|
|
# Expose port
|
|
EXPOSE 8080
|
|
|
|
# Start the application
|
|
CMD ["/app/start.sh"]
|