I’m working on a python application using the rye package manager. I wanted to use the dynamic = ["version"]
setting and use the latest git tag as the version of the project (since I have set up a release drafter workflow), however I ran into this error (only when trying to build a docker image, running rye sync
locally works good!)
File "/tmp/pip-build-env-mfctc31h/overlay/lib/python3.12/site-packages/hatchling/metadata/core.py", line 1466, in cached
raise type(e)(message) from None
LookupError: Error getting the version from source `vcs`: setuptools-scm was unable to detect version for /app.
After some reading, I understood this was due to .git
not present in the docker image and eventually got it working using the below configurations.
Dockerfile:
FROM python:3.12.4-slim-bullseye
RUN apt-get update && apt-get install -y
git --assume-yes
ca-certificates
&& rm -rf /var/lib/apt/lists/*
COPY /certs/temp-ca.crt /usr/local/share/ca-certificates/temp-ca.crt
RUN update-ca-certificates
ENV SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt
WORKDIR /app
COPY pyproject.toml requirements.lock ./
RUN --mount=source=.git,target=.git,type=bind pip install --no-cache-dir -r requirements.lock
COPY project/ project/
CMD ["uvicorn", "project.main:app", "--host", "0.0.0.0", "--port", "80"]
pyproject.toml
[build-system]
requires = ["hatchling", "hatch-vcs"]
build-backend = "hatchling.build"
[project]
name = "project"
dynamic = ["version"]
description = ""
authors = []
dependencies = [
"fastapi~=0.111.1",
"pydantic~=2.7.1",
"pydantic-settings~=2.2.1",
"structlog>=24.2.0",
"requests>=2.32.3",
"geojson-pydantic>=1.1.0",
]
requires-python = ">=3.12"
[tool.rye]
managed = true
dev-dependencies = [
"pytest>=8.2.2",
"pre-commit>=3.7.1"
]
[tool.hatch.build.targets.wheel]
packages = ["project"]
[tool.hatch.version]
source = "vcs"
Now my issue is that after I added this line in my docker image, the overall image size went up from 190 MB to 320 MB. I’d love to optimize it!
RUN --mount=source=.git,target=.git,type=bind pip install --no-cache-dir -r requirements.lock
I’d like to reduce the size of my docker image or find a better fix for using dynamic versioning.
tonystark1234 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.