Skip to content

Commit

Permalink
feat(build): use multi stage docker build for smaller images
Browse files Browse the repository at this point in the history
  • Loading branch information
anehx committed Jan 22, 2025
1 parent e2c6826 commit fa0a856
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 21 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
python-version: "3.12"
cache: "poetry"
- name: Install dependencies
run: poetry install --all-extras
run: poetry install --all-extras --with dev
- name: Run gitlint
run: poetry run gitlint --contrib contrib-title-conventional-commits
- name: Run ruff check
Expand Down Expand Up @@ -120,7 +120,7 @@ jobs:
run: |
sudo apt-get update
sudo apt-get install -y --no-install-recommends util-linux unoconv libreoffice-writer
poetry install --all-extras
poetry install --all-extras --with dev
- name: Set environment
run: |
echo "ENV=dev" >> .env
Expand Down
52 changes: 35 additions & 17 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,40 @@
FROM python:3.12 AS build

ARG ENV=docker

ENV PYTHONUNBUFFERED=1
ENV POETRY_VIRTUALENVS_CREATE=false
ENV POETRY_HOME=/opt/poetry
ENV APP_HOME=/app

WORKDIR $APP_HOME

RUN pip install -U poetry

# Install project dependencies
COPY pyproject.toml poetry.lock $APP_HOME/
RUN \
--mount=type=cache,target=.cache/pypoetry \
poetry install --no-root --all-extras $(test "$ENV" = "dev" && echo "--with dev")

# Install project itself
COPY . $APP_HOME
RUN \
--mount=type=cache,target=.cache/pypoetry \
poetry install --only-root

FROM python:3.12-slim

ARG UID=901

# Needs to be set for users with manually set UID
ENV HOME=/home/document-merge-service

ENV PYTHONUNBUFFERED=1
ENV DJANGO_SETTINGS_MODULE=document_merge_service.settings
ENV APP_HOME=/app
ENV UWSGI_INI=/app/uwsgi.ini
ENV MEDIA_ROOT=/var/lib/document-merge-service/media
ENV DATABASE_DIR=/var/lib/document-merge-service/data

ARG UID=901
WORKDIR $APP_HOME

Check warning on line 37 in Dockerfile

View workflow job for this annotation

GitHub Actions / container-registry

Relative workdir without an absolute workdir declared within the build can have unexpected results if the base image changes

WorkdirRelativePath: Relative workdir "" can have unexpected results if the base image changes More info: https://docs.docker.com/go/dockerfile/rule/workdir-relative-path/

Check warning on line 37 in Dockerfile

View workflow job for this annotation

GitHub Actions / container-registry

Variables should be defined before their use

UndefinedVar: Usage of undefined variable '$APP_HOME' More info: https://docs.docker.com/go/dockerfile/rule/undefined-var/

RUN mkdir -p $APP_HOME $DATABASE_DIR/tmp $MEDIA_ROOT /var/www/static \
&& useradd -u $UID -r document-merge-service --create-home \
Expand All @@ -21,30 +45,24 @@ RUN mkdir -p $APP_HOME $DATABASE_DIR/tmp $MEDIA_ROOT /var/www/static \
# root.
&& chown -R document-merge-service:root $DATABASE_DIR $MEDIA_ROOT $HOME /var/www/static

WORKDIR $APP_HOME

RUN \
--mount=type=cache,target=/var/cache/apt \
--mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
apt-get update && apt-get install -y --no-install-recommends \
build-essential \
default-libmysqlclient-dev \
libreoffice-writer \
pkg-config \
unoconv \
util-linux \
wait-for-it \
&& rm -rf /var/lib/apt/lists/*

RUN pip install -U poetry

USER document-merge-service

ARG ENV=docker
COPY pyproject.toml poetry.lock $APP_HOME/
RUN if [ "$ENV" = "dev" ]; then poetry install --no-root --all-extras; else poetry install --no-root --all-extras --without dev; fi

COPY . $APP_HOME
COPY --from=build /usr/local/lib/python3.12/site-packages/ /usr/local/lib/python3.12/site-packages/
COPY --from=build /usr/local/bin/ /usr/local/bin/

EXPOSE 8000

CMD /bin/sh -c "poetry run python ./manage.py migrate && poetry run uwsgi"
COPY . $APP_HOME

Check warning on line 66 in Dockerfile

View workflow job for this annotation

GitHub Actions / container-registry

Variables should be defined before their use

UndefinedVar: Usage of undefined variable '$APP_HOME' More info: https://docs.docker.com/go/dockerfile/rule/undefined-var/

CMD ["/bin/sh", "-c", "./manage.py migrate && uwsgi"]
2 changes: 1 addition & 1 deletion docker-compose.override.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ services:
[
"/bin/sh",
"-c",
"poetry run python ./manage.py migrate && poetry run python ./manage.py runserver 0.0.0.0:8000"
"./manage.py migrate && ./manage.py runserver 0.0.0.0:8000"
]
environment:
- ENV=dev
Expand Down
4 changes: 3 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ uWSGI = "^2.0.21"
xltpl = "~0.21"
poetry = "^2.0.0"

[tool.poetry.group.dev]
optional = true

[tool.poetry.group.dev.dependencies]
django-stubs = "5.1.1"
factory-boy = "3.3.1"
Expand Down

0 comments on commit fa0a856

Please sign in to comment.