Skip to content

Instantly share code, notes, and snippets.

@vikram-dagger
Created August 1, 2025 20:22
Show Gist options
  • Save vikram-dagger/e87d7f2407909d5c79ffc70915bfb57c to your computer and use it in GitHub Desktop.
Save vikram-dagger/e87d7f2407909d5c79ffc70915bfb57c to your computer and use it in GitHub Desktop.
import { dag, Container, Directory, object, argument, func } from "@dagger.io/dagger"
@object()
export class Book {
source: Directory
// Automatically uses "." as the default source directory
constructor(
@argument({ defaultPath: "." }) source: Directory,
) {
this.source = source
}
@func()
env(): Container {
return dag
.container()
.from("python:3.11")
.withDirectory("/app", this.source)
.withWorkdir("/app")
.withMountedCache("/root/.cache/pip", dag.cacheVolume("python-pip"))
.withExec(["pip", "install", "-r", "requirements.txt"]);
}
@func()
async test(): Promise<string> {
const postgres = dag
.container()
.from("postgres:alpine")
.withEnvVariable("POSTGRES_DB", "app_test")
.withEnvVariable("POSTGRES_PASSWORD", "secret")
.withExposedPort(5432)
.asService();
const output = await this.env()
.withServiceBinding("db", postgres)
.withEnvVariable("DATABASE_URL", "postgresql://postgres:secret@db/app_test")
.withExec(["pytest"])
.stdout();
return output;
}
@func()
async publish(): Promise<string> {
await this.test();
const tag = `ttl.sh/my-fastapi-app-${Math.floor(Math.random() * 10000000)}`;
const imageRef = await this.env()
.withExposedPort(8000)
.withEntrypoint(["fastapi", "run", "main.py"])
.publish(tag);
return imageRef;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment