(I was experimenting with an AI model trained on our docs and daggerverse to see how it would do on your Dockerfile. Did pretty good, then I was trying to make it fit a simpler general case to test it. Here's the original (with a tiny tweak) in case it's more helpful:
import dagger
from dagger import dag, function, object_type
@object_type
class Tuna:
@function
def build(self, source: dagger.Directory, codeartifact_token: dagger.Secret) -> dagger.Container:
"""Build the application container"""
builder = (
dag.container()
.from_("python:3.12")
.with_exec(["pip", "install", "poetry"])
.with_env_variable("POETRY_NO_INTERACTION", "1")
.with_env_variable("POETRY_VIRTUALENVS_IN_PROJECT", "1")
.with_env_variable("POETRY_VIRTUALENVS_CREATE", "1")
.with_env_variable("POETRY_CACHE_DIR", "/tmp/poetry_cache")
.with_secret_variable("CODEARTIFACT_TOKEN", codeartifact_token)
.with_workdir("/app")
.with_directory("/app/service", source.directory("service"))
.with_directory("/app/common", source.directory("common"))
.with_workdir("/app/service")
.with_exec(["poetry", "config", "http-basic.aws", "aws", "$CODEARTIFACT_TOKEN"])
.with_exec(["poetry", "install", "--no-dev"])
)
return (
dag.container()
.from_("python:3.12-slim")
.with_env_variable("VIRTUAL_ENV", "/app/service/.venv")
.with_env_variable("PATH", "/app/service/.venv/bin:$PATH")
.with_directory("/app/service/.venv", builder.directory("/app/service/.venv"))
.with_directory("/service", source.directory("src/service"))
)
@function
def service(self, source: dagger.Directory) -> dagger.Service:
"""Create a service from the built container"""
return (
self.build(source)
.with_exposed_port(8000)
.with_default_args([
"uvicorn",
"--log-level", "warning",
"--app-dir", "service",
"main:app",
"--host", "0.0.0.0"
]).as_service()
)