Skip to content

Commit

Permalink
Merge pull request #7 from mlibrary/take-out-venv-in-production
Browse files Browse the repository at this point in the history
changing to using pip installed dependencies in production
  • Loading branch information
niquerio authored Oct 4, 2024
2 parents 59e35a5 + a8f5fd7 commit 1ec9547
Show file tree
Hide file tree
Showing 8 changed files with 1,143 additions and 27 deletions.
1,113 changes: 1,113 additions & 0 deletions .config/pypoetry/poetry.lock

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions .config/pypoetry/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[tool.poetry]
name = "poetry-instance"
version = "1.8.3"
description = ""
authors = []
license = ""
package-mode = false

[tool.poetry.dependencies]
python = "3.11.8"
poetry = "1.8.3"

[tool.poetry.group.additional.dependencies]
poetry-plugin-export = "^1.8.0"

3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ __pycache__
.ssh
htmlcov/
.coverage
.gnupg
.gnupg
requirements.txt
29 changes: 8 additions & 21 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
FROM python:3.11-slim-bookworm as base

# Allowing the argumenets to be read into the dockerfile. Ex: .env > compose.yml > Dockerfile
ARG POETRY_VERSION=1.5.1
ARG POETRY_VERSION=1.8.3
ARG UID=1000
ARG GID=1000

ENV PYTHONPATH="/app"

# Create our users here in the last layer or else it will be lost in the previous discarded layers
# Create a system group named "app_user" with the -r flag
Expand All @@ -38,13 +39,6 @@ CMD ["tail", "-f", "/dev/null"]
# Both build and development need poetry, so it is its own step.
FROM base as poetry


RUN apt-get update -yqq && apt-get install -yqq --no-install-recommends \
python3-dev \
default-libmysqlclient-dev \
build-essential \
pkg-config

RUN pip install poetry==${POETRY_VERSION}

# Use this page as a reference for python and poetry environment variables: https://docs.python.org/3/using/cmdline.html#envvar-PYTHONUNBUFFERED
Expand All @@ -66,12 +60,8 @@ FROM poetry as build
# Just copy the files needed to install the dependencies
COPY pyproject.toml poetry.lock README.md ./

# Poetry cache is used to avoid installing the dependencies every time the code changes, we will keep this folder in development environment and remove it in production
# --no-root, poetry will install only the dependencies avoiding to install the project itself, we will install the project in the final layer
# --without dev to avoid installing dev dependencies, we do not need test and linters in production environment
# --with dev to install dev dependencies, we need test and linters in development environment
# --mount, mount a folder for plugins with poetry cache, this will speed up the process of building the image
RUN poetry install --no-root --without dev && rm -rf ${POETRY_CACHE_DIR};
#Use poetry to create a requirements.txt file. Dont include development dependencies
RUN poetry export --without dev -f requirements.txt --output requirements.txt

# We want poetry on in development
FROM poetry as development
Expand All @@ -84,15 +74,12 @@ USER app
# We don't want poetry on in production, so we copy the needed files form the build stage
FROM base as production
# Switch to the non-root user "user"
RUN mkdir -p /venv && chown ${UID}:${GID} /venv
# RUN mkdir -p /venv && chown ${UID}:${GID} /venv

# By adding /venv/bin to the PATH the dependencies in the virtual environment
# are used
ENV VIRTUAL_ENV=/venv \
PATH="/venv/bin:$PATH" \
PYTHONPATH="/app"

COPY --chown=${UID}:${GID} . /app
COPY --chown=${UID}:${GID} --from=build "/app/.venv" ${VIRTUAL_ENV}
COPY --chown=${UID}:${GID} --from=build "/app/requirements.txt" /app/requirements.txt

RUN pip install -r /app/requirements.txt

USER app
2 changes: 1 addition & 1 deletion compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ services:
UID: ${UID:-1000}
GID: ${GID:-1000}
DEV: ${DEV:-false}
POETRY_VERSION: ${POETRY_VERSION:-1.5.1}
POETRY_VERSION: ${POETRY_VERSION:-1.8.3}
ports:
- 8000:8000
env_file:
Expand Down
4 changes: 2 additions & 2 deletions poetry.lock

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

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]
name = "aim"
version = "0.1.0"
description = "Boilerplate code for developing a python app in docker"
description = "A CLI for the AIM team"
authors = ["Monique Rio <[email protected]>", "David Fulmer <[email protected]>"]
readme = "README.md"
packages = [{include = "aim"}]
Expand All @@ -21,6 +21,7 @@ pytest = "^8.0.2"
pytest-cov = "^5.0.0"
ruff = "^0.6.6"


[tool.pytest.ini_options]
addopts = "--cov=aim --cov-report=html --cov-report=term:skip-covered"

Expand Down
1 change: 0 additions & 1 deletion requirements.txt

This file was deleted.

0 comments on commit 1ec9547

Please sign in to comment.