Yeah, you can add a cache volume mount for pip cache and poetry cache here. It'll help not having to re-download the packages from pypi if poetry install executes again (which is easy if you change anything in your source code). It'll still create the venv and install all the packages, but they'll be collected from cache instead of re-downloaded. You can also use a cache mount for the venv itself like you said, but you need to set the initial dir first and set it as base for the mount (source argument: https://dagger-io.readthedocs.io/en/sdk-python-v0.8.4/client.html#dagger.Container.with_mounted_cache).
For now, to keep it simple, just adding the cache mounts is an improvement:
runner = (
client.container()
.from_("python:3.10-slim-buster")
+ .With_mounted_cache("/root/.cache/pip", client.cache_volume("pip_cache")).
.with_exec(["pip", "install", "poetry"])
+ .With_mounted_cache("/root/.cache/poetry", client.cache_volume("poetry_cache")).
.with_secret_variable(
"POETRY_PYPI_TOKEN_PYPI",
client.set_secret("pypi_token", os.getenv("PYPI_TOKEN")),
)
.with_directory(
"/src",
client.host().directory(".", exclude=[".venv", ".vscode"]),
)
.with_workdir("/src")
.with_exec(["poetry", "install"])
)