Skip to content
This repository has been archived by the owner on Jul 18, 2024. It is now read-only.

feat(docker): add docker file for production ready image #89

Merged
merged 1 commit into from
Nov 20, 2023
Merged
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
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.tmp
*.sqlite3
17 changes: 17 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,20 @@ jobs:
- uses: moneymeets/action-setup-python-poetry@master

- uses: moneymeets/moneymeets-composite-actions/lint-python@master

build-image:
runs-on: ubuntu-22.04
timeout-minutes: 30
steps:
- uses: actions/checkout@v4

- uses: moneymeets/moneymeets-composite-actions/detect-python-version@master
id: 'detect-versions'

- name: Build image
run: |
docker build \
--build-arg PYTHON_VERSION_CONSTRAINT="${{ steps.detect-versions.outputs.python-version-constraint }}" \
--build-arg POETRY_VERSION="${{ steps.detect-versions.outputs.poetry-version }}" \
-t sepacetamol-ci .
# TODO: share image tag when pushing part is implemented
58 changes: 58 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
ARG PYTHON_VERSION_CONSTRAINT
FROM python:${PYTHON_VERSION_CONSTRAINT}-slim-bookworm as python-base

LABEL maintainer="moneymeets GmbH <[email protected]>"

ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
APP_PATH="/opt/app" \
POETRY_HOME="/opt/poetry" \
POETRY_NO_INTERACTION=1 \
POETRY_VIRTUALENVS_IN_PROJECT=true \
VENV_PATH="/opt/app/.venv"

ENV PATH="$POETRY_HOME/bin:$VENV_PATH/bin:$PATH"

ARG POETRY_VERSION
FROM python-base as builder-deps

RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt install -y curl git \
&& apt-get install -y --no-install-recommends build-essential \
&& curl -sSL https://install.python-poetry.org | python - --yes --version=${POETRY_VERSION} \
&& apt remove -y --purge curl \
&& rm -rf /var/lib/apt/lists/*

COPY .git/ ./.git/

RUN git archive -v -o app.tar.gz --format=tar.gz HEAD

WORKDIR $APP_PATH

ADD poetry.lock pyproject.toml ./

RUN --mount=type=cache,target=/root/.cache \
poetry install --without dev


FROM python-base as prod

RUN apt update \
&& DEBIAN_FRONTEND=noninteractive apt install -y curl \
&& rm -rf /var/lib/apt/lists/*


# Copy Python dependencies from the previous build stage
COPY --from=builder-deps $APP_PATH $APP_PATH

RUN useradd -m appuser -d $APP_PATH && chown appuser:appuser -R $APP_PATH

USER appuser

WORKDIR $APP_PATH

COPY --from=builder-deps app.tar.gz $APP_PATH

RUN tar -xvf app.tar.gz && rm -rf app.tar.gz

ENTRYPOINT [ "bash", "-c", "gunicorn -c ./docker-gunicorn.conf.py" ]
11 changes: 11 additions & 0 deletions docker-gunicorn.conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
bind = "0.0.0.0:8000"
worker = 2
threads = 4
keepalive = 5
wsgi_app = "config.wsgi"
loglevel = "debug"
access_log_format = '%(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s" "%(M)s"'
errorlog = "-" # log to stderr
accesslog = "-" # log to stdout
# Enable inheritance for stdio file descriptors in daemon mode, allows to stream more logs to stdout
enable_stdio_inheritance = True