Skip to content

Instantly share code, notes, and snippets.

@jpadams
Created December 30, 2022 18:08
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 jpadams/4c59f43cab23895951e1c7fe71c03562 to your computer and use it in GitHub Desktop.
Save jpadams/4c59f43cab23895951e1c7fe71c03562 to your computer and use it in GitHub Desktop.
simple image build and publish (push)
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.Stdout))
if err != nil {
return err
}
defer client.Close()
// get reference to the local project
src := client.Host().Directory("./src")
// get base image
image := client.Container().
//From("eu.gcr.io/private/image:foo").
From("jeremyatdockerhub/ubuntu_test:latest").
// mount temp snapshot of local source directory
// useful for multistage builds etc where you don't need
// to keep the source in the container afterwards, maybe
// just an executable
//WithMountedDirectory("/src", src).
// copy local source directory into container so it persists
WithDirectory("/src", src).WithWorkdir("/src").
// set the working directory
WithWorkdir("/src").
// set the container entrypoint
WithEntrypoint([]string{"sh", "-c", "echo foo"})
// publish the new image
//addr, err := image.Publish(ctx, "eu.gcr.io/private/image:foo")
addr, err := image.Publish(ctx, "jeremyatdockerhub/result:1")
if err != nil {
panic(err)
}
fmt.Println(addr)
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment