Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(build): use multi stage docker build for smaller images #934

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

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/

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/

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 @@
# 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"]
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ start: ## Start the development server
@docker compose up -d --build

test: ## Test the project
@docker compose exec document-merge-service poetry run sh -c "ruff format --diff --fix . && ruff check --diff . && mypy document_merge_service && pytest --no-cov-on-fail --cov --create-db"
@docker compose exec document-merge-service sh -c "ruff format --diff --fix . && ruff check --diff . && mypy document_merge_service && pytest --no-cov-on-fail --cov --create-db"

shell: ## Shell into document merge service
@docker compose exec document-merge-service poetry shell

format: ## Format python code with ruff check
@docker compose exec document-merge-service poetry run ruff format --diff .
@docker compose exec document-merge-service ruff format --diff .

dmypy: ## Run mypy locally (starts a deamon for performance)
dmypy run document_merge_service
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
Loading