Skip to content

Instantly share code, notes, and snippets.

@jtarchie
Created August 1, 2023 02:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtarchie/21ecaf57659c6b62b0412618bb62802c to your computer and use it in GitHub Desktop.
Save jtarchie/21ecaf57659c6b62b0412618bb62802c to your computer and use it in GitHub Desktop.
dagger container order resolution
package main
import (
"context"
"fmt"
"os"
"dagger.io/dagger"
)
func main() {
if err := build(context.Background()); err != nil {
fmt.Println(err)
}
}
func build(ctx context.Context) error {
fmt.Println("Building with Dagger")
// initialize Dagger client
client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stderr))
if err != nil {
return err
}
defer client.Close()
// get `first` image
first := client.Container().From("ubuntu:latest")
// golang = golang.WithDirectory("/src", src).WithWorkdir("/src")
// define the application build command
path := "build/"
first = first.
WithExec([]string{"bash", "-c", "mkdir -p build/"}).
WithExec([]string{"bash", "-c", "echo asdf > build/test.txt"})
// get reference to build buildPath directory in container
buildPath := first.Directory(path)
// get second image
second := client.Container().From("ubuntu:latest")
second = second.WithDirectory("/another-build", buildPath).WithWorkdir("/another-build")
second = second.WithExec([]string{"bash", "-c", "cat test.txt"})
output, err := second.Stdout(ctx)
fmt.Printf("output from second container: %s\n", output)
return err
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment