From e903c472fe8a8825081fc5f50b22dcd4c796c43c Mon Sep 17 00:00:00 2001 From: Abhiek187 Date: Sat, 6 May 2023 16:59:54 -0400 Subject: [PATCH] Reduce the prod image size by 83% --- .dockerignore | 2 +- Dockerfile | 20 ++++++++++---------- Dockerfile.dev | 4 ---- docker-compose.yml | 1 + 4 files changed, 12 insertions(+), 15 deletions(-) diff --git a/.dockerignore b/.dockerignore index d00375d..58dc989 100644 --- a/.dockerignore +++ b/.dockerignore @@ -92,7 +92,7 @@ venv/ */*/*.swp */*/*/*.swp -# Django stuff: +# Django stuff *.log local_settings.py db.sqlite3 diff --git a/Dockerfile b/Dockerfile index f5cb083..f5855d6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,26 +1,26 @@ +# Start with a python 3 base image ARG PYTHON_VERSION=3-alpine - FROM python:${PYTHON_VERSION} -RUN \ - apk add --no-cache postgresql-libs && \ - apk add --no-cache --virtual .build-deps gcc musl-dev postgresql-dev - -RUN mkdir -p /app WORKDIR /app - COPY requirements.txt . -RUN pip install -r requirements.txt -RUN apk --purge del .build-deps + +# Install dependencies +RUN \ + apk add --no-cache postgresql-libs && \ + apk add --no-cache --virtual .build-deps gcc musl-dev postgresql-dev && \ + pip install -r requirements.txt --no-cache-dir && \ + apk --purge del .build-deps COPY . . ENV SECRET_KEY=$SECRET_KEY FMP_API_KEY=$FMP_API_KEY +# Initialize the Postgres database, load the flashcards data, and collect all static files RUN \ python stockhelper/manage.py migrate && \ python stockhelper/manage.py loaddata cards.json && \ python stockhelper/manage.py collectstatic --noinput +# Start an HTTP server EXPOSE 8080 - CMD ["gunicorn", "--bind", ":8080", "--workers", "2", "--chdir", "stockhelper", "stockhelper.wsgi"] diff --git a/Dockerfile.dev b/Dockerfile.dev index 00792f9..1f936c7 100644 --- a/Dockerfile.dev +++ b/Dockerfile.dev @@ -18,7 +18,3 @@ RUN \ COPY . /code WORKDIR /code/stockhelper -# Initialize the SQLite databases -RUN python manage.py migrate -# Load the flashcards data -RUN python manage.py loaddata cards.json diff --git a/docker-compose.yml b/docker-compose.yml index 3a10c26..3791901 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,6 +6,7 @@ services: build: context: . dockerfile: Dockerfile.dev + # Initialize the SQLite database, load the flashcards data, and start the server command: > sh -c "python manage.py migrate && python manage.py loaddata cards.json &&