The tricky thing here will be dealing with the 10 second grace period for shutting down a service - that's assuming you're using services here, which might not be necessary.
You could always run the container yourself and use its hostname/endpoint directly to connect to it, and then interrupt it when you're done with it. This should feel pretty intuitive in Go with context.Context but I'm not sure about other SDKs:
ctx := context.Background()
client, _ := dagger.Connect(ctx)
db := client.Container().From("postgres").WithExec(nil)
hostname, _ := db.Hostname(ctx)
cancelCtx, cancel := context.WithCancel(ctx)
go db.ExitCode(cancelCtx)
migrate := client.Container().From("foo").WithExec([]string{"migrate", "--host", hostname})
_, _ = migrate.ExitCode(cancelCtx)
cancel()