Dockerfile.prod 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. ###########
  2. # BUILDER #
  3. ###########
  4. # pull official base image
  5. FROM python:3.9.6-alpine as builder
  6. # set work directory
  7. WORKDIR /usr/src/app
  8. # set environment variables
  9. ENV PYTHONDONTWRITEBYTECODE 1
  10. ENV PYTHONUNBUFFERED 1
  11. # install psycopg2 dependencies
  12. RUN apk update \
  13. && apk add postgresql-dev gcc python3-dev musl-dev
  14. # lint
  15. RUN pip install --upgrade pip
  16. RUN pip install flake8==3.9.2
  17. COPY . .
  18. RUN flake8 --ignore=E501,F401 .
  19. # install dependencies
  20. COPY ./requirements.txt .
  21. RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/app/wheels -r requirements.txt
  22. #########
  23. # FINAL #
  24. #########
  25. # pull official base image
  26. FROM python:3.9.6-alpine
  27. # create directory for the app user
  28. RUN mkdir -p /home/app
  29. # create the app user
  30. RUN addgroup -S app && adduser -S app -G app
  31. # create the appropriate directories
  32. ENV HOME=/home/app
  33. ENV APP_HOME=/home/app/web
  34. RUN mkdir $APP_HOME
  35. RUN mkdir $APP_HOME/staticfiles
  36. RUN mkdir $APP_HOME/mediafiles
  37. WORKDIR $APP_HOME
  38. # install dependencies
  39. RUN apk update && apk add libpq
  40. COPY --from=builder /usr/src/app/wheels /wheels
  41. COPY --from=builder /usr/src/app/requirements.txt .
  42. RUN pip install --no-cache /wheels/*
  43. # copy entrypoint.prod.sh
  44. COPY ./entrypoint.prod.sh .
  45. RUN sed -i 's/\r$//g' $APP_HOME/entrypoint.prod.sh
  46. RUN chmod +x $APP_HOME/entrypoint.prod.sh
  47. # copy project
  48. COPY . $APP_HOME
  49. # chown all the files to the app user
  50. RUN chown -R app:app $APP_HOME
  51. # change to the app user
  52. USER app
  53. # run entrypoint.prod.sh
  54. ENTRYPOINT ["/home/app/web/entrypoint.prod.sh"]