Skip to content

Commit

Permalink
! mode from poetry to uv
Browse files Browse the repository at this point in the history
  • Loading branch information
ALERTua committed Oct 2, 2024
1 parent b5a958e commit 15d9b6f
Show file tree
Hide file tree
Showing 10 changed files with 301 additions and 1,450 deletions.
2 changes: 1 addition & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Modelfile

# Docker
docker-compose.yml
Dockerfile
Dockerfile*
.docker
.dockerignore

Expand Down
11 changes: 0 additions & 11 deletions .github/dependabot.yml

This file was deleted.

1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.12
97 changes: 38 additions & 59 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,91 +1,70 @@
FROM python:3.12-slim AS python-base
FROM python:3.12-slim AS builder

ENV \
BASE_DIR=/app
COPY --from=ghcr.io/astral-sh/uv:latest /uv /bin/uv

WORKDIR $BASE_DIR
ENV APP_DIR=/app

ENV \
# OS
PORT=8000 \
# uv
UV_COMPILE_BYTECODE=1 \
UV_LINK_MODE=copy \
UV_PYTHON_DOWNLOADS=never \
UV_CACHE_DIR="$APP_DIR/.uv_cache" \
# Python
PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
PYTHONIOENCODING=utf-8 \
LANG=en_US.UTF-8 \
LANGUAGE=en_US.UTF-8 \
# LC_ALL=en_US.UTF-8 \
# pip
PIP_DISABLE_PIP_VERSION_CHECK=on \
# poetry
POETRY_HOME="$BASE_DIR/poetry" \
POETRY_NO_INTERACTION=1 \
POETRY_VIRTUALENVS_CREATE=false \
# venv and requirements path
VIRTUAL_ENV="$BASE_DIR/venv" \
# cache path is HOME/.cache
CACHE_PATH="/root/.cache"

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

RUN python -m venv $VIRTUAL_ENV

ENV PYTHONPATH="$BASE_DIR:$PYTHONPATH"


FROM python-base AS builder-base

RUN apt-get update && \
apt-get install -y curl

RUN --mount=type=cache,target=$CACHE_PATH \
curl -sSL https://install.python-poetry.org | python -

WORKDIR $BASE_DIR

COPY poetry.lock pyproject.toml ./

RUN --mount=type=cache,target=$CACHE_PATH \
poetry install --no-root --only main --compile
VIRTUAL_ENV="$APP_DIR/.venv" \
PYTHONPATH="$APP_DIR/apps:$PYTHONPATH"

WORKDIR $APP_DIR

FROM builder-base AS development
# Cache and bind mounts for uv
RUN \
--mount=type=cache,target=$UV_CACHE_DIR \
--mount=type=bind,source=uv.lock,target=uv.lock \
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
uv sync --frozen --no-install-project --no-dev

WORKDIR $BASE_DIR
ADD . .

RUN --mount=type=cache,target=$CACHE_PATH \
poetry install --no-root --compile
# Cache mount again for uv sync
RUN \
--mount=type=cache,target=$UV_CACHE_DIR \
uv sync --frozen --no-dev

CMD ["bash"]

FROM builder AS development

FROM python-base AS production
ENV PATH="$VIRTUAL_ENV/bin:$PATH"

COPY --from=builder-base $POETRY_HOME $POETRY_HOME
COPY --from=builder-base $VIRTUAL_ENV $VIRTUAL_ENV
COPY --from=builder $APP_DIR $APP_DIR

RUN \
apt-get update \
&& apt-get install -y --no-install-recommends curl \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
EXPOSE $PORT

WORKDIR $BASE_DIR
VOLUME /data

COPY . ./
FROM development AS production

RUN chmod +x ./*.sh
ENV USERNAME=nonroot

VOLUME /data
RUN useradd -ms /bin/bash $USERNAME

ENV \
PYTHONPATH="$BASE_DIR/apps:$PYTHONPATH" \
PORT=8000 \
PYTHONIOENCODING=utf-8 \
LC_ALL=en_US.UTF-8 \
LANG=en_US.UTF-8 \
LANGUAGE=en_US.UTF-8
USER $USERNAME

EXPOSE $PORT
COPY --from=development --chown=$USERNAME:$USERNAME $APP_DIR $APP_DIR

HEALTHCHECK --interval=10s --timeout=5s --start-period=10s --retries=5 \
CMD curl localhost:${PORT}/health || exit 1
HEALTHCHECK \
--interval=10s --timeout=5s --start-period=10s --retries=5 \
CMD curl localhost:${PORT}/health || exit 1

CMD ["./entrypoint.sh"]
10 changes: 3 additions & 7 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
#!/bin/bash
echo "entrypoint"

export PYTHONIOENCODING=utf-8
export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8

cd $BASE_DIR || (echo "Cannot CD to $BASE_DIR. Exiting" & exit)

port=${PORT:-8000}

echo "$port @ $(pwd)"

whoami
echo "test" >> /etc/passwd

ls -lA
python -V
python manage.py makemigrations --noinput
Expand Down
1,355 changes: 0 additions & 1,355 deletions poetry.lock

This file was deleted.

29 changes: 13 additions & 16 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
[tool.poetry]
[project]
name = "import-tax-calc"
version = "0.1.0"
description = "Import Tax Calculator for Ukraine"
authors = ["Alexey ALERT Rubasheff <[email protected]>"]
readme = "README.md"
requires-python = ">=3.12"
dependencies = [
"django>=5.1.1",
"django-cors-headers>=4.4.0",
"djangorestframework>=3.15.2",
"gunicorn>=23.0.0",
"psycopg[binary,pool]>=3.2.3",
]

[tool.poetry.dependencies]
python = "^3.12"
django = "^5.1.1"
psycopg = {version = "^3.2.1", extras = ["binary", "pool"]}
gunicorn = "^23.0.0"
django-cors-headers = "^4.4.0"
djangorestframework = "^3.15.2"

[tool.poetry.group.dev.dependencies]
poetry = "^1.8.3"
pytest = "^8.3.2"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
[tool.uv]
dev-dependencies = [
"pytest>=8.3.3",
]
12 changes: 12 additions & 0 deletions script/build_run.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

@if not defined DOCKER_HOST set DOCKER_HOST=tcp://docker:2375
@if not defined STAGE set STAGE=production
@if not defined CONTAINER_NAME set CONTAINER_NAME=test
@if not defined DOCKERFILENAME set DOCKERFILENAME=Dockerfile

docker kill %CONTAINER_NAME%
docker rm %CONTAINER_NAME%
docker build -f %DOCKERFILENAME% --target %STAGE% -t %CONTAINER_NAME%:latest . || exit /b 1
docker run -p "8000:8000/tcp" -e ALLOWED_HOSTS=gitlab --name %CONTAINER_NAME% -t -d %CONTAINER_NAME% || exit /b 1
where nircmd >nul 2>nul && nircmd beep 500 500
docker exec -it %CONTAINER_NAME% bash
3 changes: 2 additions & 1 deletion script/deploy.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ poetry check || goto :end
if not exist poetry.lock poetry lock || goto :end

if not defined DOCKER_BUILDKIT set DOCKER_BUILDKIT=1
if not defined DOCKERFILE set DOCKERFILE=Dockerfile
if not defined DOCKER_REGISTRY set DOCKER_REGISTRY=registry.alertua.duckdns.org
echo DOCKER_REGISTRY: %DOCKER_REGISTRY%

Expand Down Expand Up @@ -69,7 +70,7 @@ if not defined DOCKER_REMOTE (
)

"%DOCKER_EXE%" --version
"%DOCKER_EXE%" build -t %BUILD_TAG% %BUILD_PATH% %* || goto :end
"%DOCKER_EXE%" build -t %BUILD_TAG% %BUILD_PATH% --target production %* || goto :end
"%DOCKER_EXE%" push %DOCKER_REGISTRY%/%IMAGE_NAME% || goto :end

@REM net stop %DOCKER_SERVICE% || goto :end
Expand Down
Loading

0 comments on commit 15d9b6f

Please sign in to comment.