Skip to content

Commit

Permalink
Merge pull request #520 from iMMAP/chore/reduce_docker_django_image_size
Browse files Browse the repository at this point in the history
reduced the django docker image size
  • Loading branch information
shtayeb authored Dec 25, 2024
2 parents 505f587 + 8658334 commit 5d8d949
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 32 deletions.
51 changes: 25 additions & 26 deletions docker/django/Dockerfile.prod
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,43 @@ ENV PYTHONPATH=/usr/src/rh/src

# Install dependencies
RUN apt-get update && apt-get install -y \
build-essential cron \
libpq-dev curl man wget \
npm \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
curl cron \
# TODO: This adds 1.3GB to layers
npm nodejs && \
apt-get clean && \
npm cache clean -f && \
apt-get autoremove -y && \
rm -rf /var/lib/apt/lists/*

# Install Python dependencies
RUN pip install poetry

# npm and node latest with n
RUN npm cache clean -f
RUN npm install -g n
RUN n stable

# Set the working directory
WORKDIR /usr/src/rh

# to preserve the docker cache
COPY ["README.md", "Makefile", "poetry.lock","pyproject.toml","/usr/src/rh"]
RUN poetry install --without dev --no-root --no-directory

# copy project code -> src to /usr/src/rh/src
COPY src /usr/src/rh/src

# Install Node.js dependencies and build static files
# Activate shell for django-vite-plugin build
RUN poetry run make npm-install

COPY docker/django/django-entrypoint.sh /usr/src/rh/django-entrypoint.sh
RUN chmod a+x /usr/src/rh/django-entrypoint.sh

COPY docker/django/rh-tasks-cron /etc/cron.d/rh-tasks-cron
RUN chmod 0644 /etc/cron.d/rh-tasks-cron
RUN crontab /etc/cron.d/rh-tasks-cron
RUN touch /var/log/cron.log
RUN service cron start

# to preserve the docker cache
COPY ["README.md", "Makefile", "poetry.lock","pyproject.toml","/usr/src/rh/"]
# COPY ["src/static/package.json","src/static/vite.config.js","src/static/package-lock.json","/usr/src/rh/src/static/"]

# Set the working directory
WORKDIR /usr/src/rh
RUN poetry install --without dev --no-root --no-interaction --no-directory --no-ansi
# copy project code -> src to /usr/src/rh/src
COPY src /usr/src/rh/src

# DEBUG should be False by default in production.py file, vite.config.js file should have production settings
RUN poetry run make npm-install && \
poetry run make npm-build && \
npm cache clean --force && \
rm -rf /root/.npm && \
rm -rf src/static/node_modules

ENTRYPOINT ["/usr/src/rh/django-entrypoint.sh"]

# Gunicorn will be used to run the server
CMD ["gunicorn", "--bind", "0.0.0.0:8000","--workers=4","src.core.wsgi:application"]
CMD ["gunicorn", "--bind", "0.0.0.0:8000","--error-logfile","-","--workers=4","src.core.wsgi:application"]
7 changes: 2 additions & 5 deletions docker/django/django-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@ set -e
echo "Preparing Django Application"

# static files
make collectstatic settings=core.settings.production
echo "DEBUG: $DEBUG"
make npm-build
# rm -rf src/static/node_modules
poetry run python src/manage.py collectstatic --settings=core.settings.production --no-input --ignore=node_modules --ignore=*.scss --ignore=*.json --ignore=vite.config.js

# Create super user
# Password is set in the env file. variable DJANGO_SUPERUSER_PASSWORD
poetry run python src/manage.py createsuperuser --username=admin [email protected] --no-input --setting=core.settings.production || true

# database
make migrate
poetry run python src/manage.py migrate

exec "$@"
2 changes: 2 additions & 0 deletions src/core/settings/production.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

APP_ENV = env("APP_ENV", default="production")

DEBUG = env("DEBUG", default=False)

# ALLOWED_HOSTS=reporthub.immap.org,www.reporthub.immap.org
# env.list() splits comma-separated string into a list
ALLOWED_HOSTS = env.list(
Expand Down
2 changes: 1 addition & 1 deletion src/static/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default defineConfig({
// relative to the folder that vite.config is located
// "../users/static/users/users.js",
],
// pyArgs: ["--settings=core.settings.production"],
pyArgs: ["--settings=core.settings.production"],
root: "..",
}),
],
Expand Down

0 comments on commit 5d8d949

Please sign in to comment.