mirror of
https://github.com/DRYTRIX/TimeTracker.git
synced 2026-05-08 05:19:48 -05:00
132 lines
4.6 KiB
Docker
132 lines
4.6 KiB
Docker
FROM python:3.11-slim
|
|
|
|
# Install system dependencies including PostgreSQL
|
|
RUN apt-get update && apt-get install -y \
|
|
curl \
|
|
postgresql-13 \
|
|
postgresql-client-13 \
|
|
supervisor \
|
|
&& 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 data and logs directories with proper permissions
|
|
RUN mkdir -p /data /app/logs /var/lib/postgresql/data && \
|
|
chmod 755 /data && chmod 755 /app/logs && chmod 755 /var/lib/postgresql/data
|
|
|
|
# Create non-root user
|
|
RUN useradd -m -u 1000 timetracker && \
|
|
chown -R timetracker:timetracker /app /data /app/logs /var/lib/postgresql/data
|
|
|
|
# Create PostgreSQL configuration
|
|
RUN mkdir -p /etc/postgresql/13/main && \
|
|
echo "listen_addresses = 'localhost'" > /etc/postgresql/13/main/postgresql.conf && \
|
|
echo "port = 5432" >> /etc/postgresql/13/main/postgresql.conf && \
|
|
echo "data_directory = '/var/lib/postgresql/data'" >> /etc/postgresql/13/main/postgresql.conf && \
|
|
echo "log_destination = 'stderr'" >> /etc/postgresql/13/main/postgresql.conf && \
|
|
echo "logging_collector = on" >> /etc/postgresql/13/main/postgresql.conf && \
|
|
echo "log_directory = 'log'" >> /etc/postgresql/13/main/postgresql.conf && \
|
|
echo "log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'" >> /etc/postgresql/13/main/postgresql.conf && \
|
|
echo "log_rotation_age = 1d" >> /etc/postgresql/13/main/postgresql.conf && \
|
|
echo "log_rotation_size = 10MB" >> /etc/postgresql/13/main/postgresql.conf
|
|
|
|
# Create PostgreSQL access configuration
|
|
RUN echo "local all all trust" > /etc/postgresql/13/main/pg_hba.conf && \
|
|
echo "host all all 127.0.0.1/32 trust" >> /etc/postgresql/13/main/postgresql.conf && \
|
|
echo "host all all ::1/128 trust" >> /etc/postgresql/13/main/pg_hba.conf
|
|
|
|
# Create postgres user and set up directories
|
|
RUN useradd -r -g postgres -s /bin/bash postgres && \
|
|
mkdir -p /var/lib/postgresql/data && \
|
|
chown postgres:postgres /var/lib/postgresql/data && \
|
|
chmod 700 /var/lib/postgresql/data
|
|
|
|
# Create database initialization script
|
|
RUN echo '#!/bin/bash\n\
|
|
set -e\n\
|
|
\n\
|
|
# Initialize PostgreSQL database\n\
|
|
if [ ! -f /var/lib/postgresql/data/PG_VERSION ]; then\n\
|
|
echo "Initializing PostgreSQL database..."\n\
|
|
su - postgres -c "/usr/bin/initdb -D /var/lib/postgresql/data"\n\
|
|
su - postgres -c "/usr/bin/pg_ctl -D /var/lib/postgresql/data -l logfile start"\n\
|
|
\n\
|
|
# Create database and user\n\
|
|
su - postgres -c "/usr/bin/createdb timetracker"\n\
|
|
su - postgres -c "/usr/bin/createuser -s timetracker"\n\
|
|
\n\
|
|
# Run initialization SQL\n\
|
|
su - postgres -c "/usr/bin/psql -d timetracker -f /app/docker/init.sql"\n\
|
|
\n\
|
|
su - postgres -c "/usr/bin/pg_ctl -D /var/lib/postgresql/data stop"\n\
|
|
echo "PostgreSQL database initialized successfully"\n\
|
|
else\n\
|
|
echo "PostgreSQL database already exists"\n\
|
|
fi\n\
|
|
' > /app/init-db.sh && chmod +x /app/init-db.sh
|
|
|
|
# Create supervisor configuration
|
|
RUN echo '[supervisord]\n\
|
|
nodaemon=true\n\
|
|
user=root\n\
|
|
\n\
|
|
[program:postgresql]\n\
|
|
command=/usr/bin/postgres -D /var/lib/postgresql/data -c config_file=/etc/postgresql/13/main/postgresql.conf\n\
|
|
user=postgres\n\
|
|
autostart=true\n\
|
|
autorestart=true\n\
|
|
priority=100\n\
|
|
\n\
|
|
[program:flask-app]\n\
|
|
command=/app/start.sh\n\
|
|
user=timetracker\n\
|
|
autostart=true\n\
|
|
autorestart=true\n\
|
|
priority=200\n\
|
|
depends_on=postgresql\n\
|
|
' > /etc/supervisor/conf.d/supervisord.conf
|
|
|
|
# Create startup script for Flask app
|
|
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\
|
|
# Wait for PostgreSQL to be ready\n\
|
|
echo "Waiting for PostgreSQL..."\n\
|
|
until su - postgres -c "/usr/bin/pg_isready -d timetracker"; do\n\
|
|
sleep 1\n\
|
|
done\n\
|
|
echo "PostgreSQL is ready"\n\
|
|
\n\
|
|
# Initialize database if needed\n\
|
|
if [ ! -f /var/lib/postgresql/data/.initialized ]; then\n\
|
|
echo "Running database initialization..."\n\
|
|
su - postgres -c "/usr/bin/psql -d timetracker -f /app/docker/init.sql"\n\
|
|
touch /var/lib/postgresql/data/.initialized\n\
|
|
echo "Database initialization completed"\n\
|
|
fi\n\
|
|
\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 ports
|
|
EXPOSE 8080 5432
|
|
|
|
# Health check
|
|
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
|
|
CMD curl -f http://localhost:8080/_health || exit 1
|
|
|
|
# Initialize database and start services
|
|
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]
|