50 lines
1.9 KiB
Docker
50 lines
1.9 KiB
Docker
FROM python:3.12
|
||
|
||
# Prevents Python from writing pyc files to disk
|
||
ENV PYTHONDONTWRITEBYTECODE=1
|
||
#Prevents Python from buffering stdout and stderr
|
||
ENV PYTHONUNBUFFERED=1
|
||
|
||
# User
|
||
RUN useradd -m -r appuser && \
|
||
mkdir /opt/app && \
|
||
chown -R appuser /opt/app
|
||
|
||
WORKDIR /opt/app
|
||
|
||
# Copy the Django project and install dependencies
|
||
COPY requirements.txt /opt/app/
|
||
# run this command to install all dependencies
|
||
RUN pip install --no-cache-dir -r requirements.txt
|
||
|
||
COPY --chown=appuser:appuser . /opt/app/
|
||
|
||
RUN chmod -R 755 /opt/app
|
||
RUN chown -R appuser:appuser /opt/app
|
||
USER appuser
|
||
|
||
# Initialization script
|
||
RUN echo '#!/bin/bash' > /opt/app/initialize.sh && \
|
||
echo 'if [ "${INITIALIZE_DB}" = false ]; then' >> /opt/app/initialize.sh && \
|
||
echo 'echo "Initialization not required"' >> /opt/app/initialize.sh && \
|
||
echo 'else' >> /opt/app/initialize.sh && \
|
||
echo 'echo "Initializating database"' >> /opt/app/initialize.sh && \
|
||
echo 'python db.py --initialize_tables --initialize_data' >> /opt/app/initialize.sh && \
|
||
echo 'python manage.py makemigrations fetcher; python manage.py migrate --fake-initial' >> /opt/app/initialize.sh && \
|
||
echo 'python manage.py createsuperuser --noinput' >> /opt/app/initialize.sh && \
|
||
echo 'python manage.py collectstatic --no-input' >> /opt/app/initialize.sh && \
|
||
echo 'python manage.py import --filename scheduled_tasks.json' >> /opt/app/initialize.sh && \
|
||
echo 'fi' >> /opt/app/initialize.sh && \
|
||
chmod +x /opt/app/initialize.sh
|
||
|
||
# Serving script
|
||
RUN echo '#!/bin/bash' > /opt/app/run.sh && \
|
||
# Prod mode:
|
||
echo 'gunicorn core.wsgi:application --bind 0.0.0.0:8000 & python manage.py rqworker high default low' >> /opt/app/run.sh && \
|
||
# Dev mode:
|
||
#echo 'gunicorn core.wsgi:application --reload --bind 0.0.0.0:8000 & python manage.py rqworker high default low' >> /opt/app/run.sh && \
|
||
chmod +x /opt/app/run.sh
|
||
|
||
# Run Django’s server & workers
|
||
CMD ["sh", "-c", "/opt/app/initialize.sh && /opt/app/run.sh"]
|