From 1235cb8da54f98d5337092b7ca1bf9f85ef5ea97 Mon Sep 17 00:00:00 2001 From: tooboredtocode Date: Fri, 22 Mar 2024 00:31:56 +0100 Subject: [PATCH 1/5] feat: improve docker run speeds --- Dockerfile | 19 +++++++++++++------ boot.sh | 22 +++++++++++++++++----- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/Dockerfile b/Dockerfile index 3f17d0f118..a398507252 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,6 +6,8 @@ RUN apk add --no-cache postgresql-libs postgresql-client gettext zlib libjpeg li #Print all logs without buffering it. ENV PYTHONUNBUFFERED 1 +ENV DOCKER true + #This port will be used by gunicorn. EXPOSE 8080 @@ -23,18 +25,23 @@ RUN \ RUN sed -i '/# Development/,$d' requirements.txt RUN apk add --no-cache --virtual .build-deps gcc musl-dev postgresql-dev zlib-dev jpeg-dev libwebp-dev openssl-dev libffi-dev cargo openldap-dev python3-dev && \ echo -n "INPUT ( libldap.so )" > /usr/lib/libldap_r.so && \ - python -m venv venv && \ - /opt/recipes/venv/bin/python -m pip install --upgrade pip && \ - venv/bin/pip install wheel==0.42.0 && \ - venv/bin/pip install setuptools_rust==1.9.0 && \ - venv/bin/pip install -r requirements.txt --no-cache-dir &&\ + python -m pip install --upgrade pip && \ + pip install wheel==0.42.0 && \ + pip install setuptools_rust==1.9.0 && \ + pip install -r requirements.txt --no-cache-dir &&\ apk --purge del .build-deps #Copy project and execute it. COPY . ./ +# collect the static files +RUN python manage.py collectstatic_js_reverse +RUN python manage.py collectstatic --noinput +# copy the collected static files to a different location, so they can be moved into a potentially mounted volume +RUN mv /opt/recipes/staticfiles /opt/recipes/staticfiles-collect + # collect information from git repositories -RUN /opt/recipes/venv/bin/python version.py +RUN python version.py # delete git repositories to reduce image size RUN find . -type d -name ".git" | xargs rm -rf diff --git a/boot.sh b/boot.sh index ae3dbb51d8..1c3c09a764 100644 --- a/boot.sh +++ b/boot.sh @@ -1,5 +1,9 @@ #!/bin/sh -source venv/bin/activate + +# conditionally activate virtualenv, since the docker container does not need it +if [[ "${DOCKER}" != "true" ]]; then + source venv/bin/activate +fi TANDOOR_PORT="${TANDOOR_PORT:-8080}" GUNICORN_WORKERS="${GUNICORN_WORKERS:-3}" @@ -67,12 +71,20 @@ echo "Migrating database" python manage.py migrate -echo "Generating static files" +if [[ "${DOCKER}" == "true" ]]; then + echo "Collecting static files" -python manage.py collectstatic_js_reverse -python manage.py collectstatic --noinput + mkdir -p /opt/recipes/staticfiles + mv /opt/recipes/staticfiles-collect/* /opt/recipes/staticfiles + rm -rf /opt/recipes/staticfiles-collect +else + echo "Generating static files" -echo "Done" + python manage.py collectstatic_js_reverse + python manage.py collectstatic --noinput + + echo "Done" +fi chmod -R 755 /opt/recipes/mediafiles From 5f211e420e2212dd71d1d7120a11e4272aa50643 Mon Sep 17 00:00:00 2001 From: tooboredtocode Date: Fri, 22 Mar 2024 02:50:18 +0100 Subject: [PATCH 2/5] fix: moving into exisiting files --- Dockerfile | 1 + 1 file changed, 1 insertion(+) diff --git a/Dockerfile b/Dockerfile index a398507252..1de5178b4a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -38,6 +38,7 @@ COPY . ./ RUN python manage.py collectstatic_js_reverse RUN python manage.py collectstatic --noinput # copy the collected static files to a different location, so they can be moved into a potentially mounted volume +RUN rm -rf /opt/recipes/staticfiles/* RUN mv /opt/recipes/staticfiles /opt/recipes/staticfiles-collect # collect information from git repositories From ae37abf8b21d3e9f13773d6857efbaed8db501ae Mon Sep 17 00:00:00 2001 From: tooboredtocode Date: Fri, 22 Mar 2024 11:26:26 +0100 Subject: [PATCH 3/5] fix: fix modified the wrong file --- Dockerfile | 1 - boot.sh | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 1de5178b4a..a398507252 100644 --- a/Dockerfile +++ b/Dockerfile @@ -38,7 +38,6 @@ COPY . ./ RUN python manage.py collectstatic_js_reverse RUN python manage.py collectstatic --noinput # copy the collected static files to a different location, so they can be moved into a potentially mounted volume -RUN rm -rf /opt/recipes/staticfiles/* RUN mv /opt/recipes/staticfiles /opt/recipes/staticfiles-collect # collect information from git repositories diff --git a/boot.sh b/boot.sh index 1c3c09a764..286d6c983c 100644 --- a/boot.sh +++ b/boot.sh @@ -75,6 +75,7 @@ if [[ "${DOCKER}" == "true" ]]; then echo "Collecting static files" mkdir -p /opt/recipes/staticfiles + rm -rf /opt/recipes/staticfiles/* mv /opt/recipes/staticfiles-collect/* /opt/recipes/staticfiles rm -rf /opt/recipes/staticfiles-collect else From 7bfa23b953a2a9ee671f23af7d55fbc997461e10 Mon Sep 17 00:00:00 2001 From: tooboredtocode Date: Fri, 22 Mar 2024 11:32:30 +0100 Subject: [PATCH 4/5] feat: better messages --- boot.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/boot.sh b/boot.sh index 286d6c983c..7d6ec74424 100644 --- a/boot.sh +++ b/boot.sh @@ -72,14 +72,14 @@ echo "Migrating database" python manage.py migrate if [[ "${DOCKER}" == "true" ]]; then - echo "Collecting static files" + echo "Copying cached static files from docker build" mkdir -p /opt/recipes/staticfiles rm -rf /opt/recipes/staticfiles/* mv /opt/recipes/staticfiles-collect/* /opt/recipes/staticfiles rm -rf /opt/recipes/staticfiles-collect else - echo "Generating static files" + echo "Collecting static files, this may take a while..." python manage.py collectstatic_js_reverse python manage.py collectstatic --noinput From 20b812c2cce1db0101a3fb2a10abca90410e44a3 Mon Sep 17 00:00:00 2001 From: tooboredtocode Date: Fri, 22 Mar 2024 19:04:29 +0100 Subject: [PATCH 5/5] feat: re-add venv --- Dockerfile | 15 ++++++++------- boot.sh | 6 +----- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/Dockerfile b/Dockerfile index a398507252..c5856bcfa5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -25,23 +25,24 @@ RUN \ RUN sed -i '/# Development/,$d' requirements.txt RUN apk add --no-cache --virtual .build-deps gcc musl-dev postgresql-dev zlib-dev jpeg-dev libwebp-dev openssl-dev libffi-dev cargo openldap-dev python3-dev && \ echo -n "INPUT ( libldap.so )" > /usr/lib/libldap_r.so && \ - python -m pip install --upgrade pip && \ - pip install wheel==0.42.0 && \ - pip install setuptools_rust==1.9.0 && \ - pip install -r requirements.txt --no-cache-dir &&\ + python -m venv venv && \ + /opt/recipes/venv/bin/python -m pip install --upgrade pip && \ + venv/bin/pip install wheel==0.42.0 && \ + venv/bin/pip install setuptools_rust==1.9.0 && \ + venv/bin/pip install -r requirements.txt --no-cache-dir &&\ apk --purge del .build-deps #Copy project and execute it. COPY . ./ # collect the static files -RUN python manage.py collectstatic_js_reverse -RUN python manage.py collectstatic --noinput +RUN /opt/recipes/venv/bin/python manage.py collectstatic_js_reverse +RUN /opt/recipes/venv/bin/python manage.py collectstatic --noinput # copy the collected static files to a different location, so they can be moved into a potentially mounted volume RUN mv /opt/recipes/staticfiles /opt/recipes/staticfiles-collect # collect information from git repositories -RUN python version.py +RUN /opt/recipes/venv/bin/python version.py # delete git repositories to reduce image size RUN find . -type d -name ".git" | xargs rm -rf diff --git a/boot.sh b/boot.sh index 7d6ec74424..ab5d7fdddd 100644 --- a/boot.sh +++ b/boot.sh @@ -1,9 +1,5 @@ #!/bin/sh - -# conditionally activate virtualenv, since the docker container does not need it -if [[ "${DOCKER}" != "true" ]]; then - source venv/bin/activate -fi +source venv/bin/activate TANDOOR_PORT="${TANDOOR_PORT:-8080}" GUNICORN_WORKERS="${GUNICORN_WORKERS:-3}"