Skip to main content

Quickstart

Write your first Dagger pipeline

You're now ready to dive into Dagger and write your first pipeline!

This first example is fairly simple: it creates a Dagger client using your chosen SDK, initializes a container, executes a command in that container and prints the output.

In the ci directory, create a file named main.go and add the following code to it.

package main

import (
"context"
"fmt"
"os"

"dagger.io/dagger"
)

func main() {
ctx := context.Background()

// initialize Dagger client
client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stderr))
if err != nil {
panic(err)
}
defer client.Close()

// use a golang:1.19 container
// get version
// execute
golang := client.Container().From("golang:1.19").WithExec([]string{"go", "version"})

version, err := golang.Stdout(ctx)
if err != nil {
panic(err)
}

// print output
fmt.Println("Hello from Dagger and " + version)
}

This Go program imports the Dagger SDK and defines a main() function for the pipeline operations. This function performs the following operations:

  • It creates a Dagger client with dagger.Connect(). This client provides an interface for executing commands against the Dagger Engine.
  • It uses the client's Container().From() method to initialize a new container from a base image. In this example, the base image is golang:1.19. This method returns a Container representing an OCI-compatible container image.
  • It uses the Container.WithExec() method to define the command to be executed in the container - in this case, the command go version, which returns the Go version string. The WithExec() method returns a revised Container with the results of command execution.
  • It retrieves the output stream of the last executed with the Container.Stdout() method and prints the result to the console.

Run the pipeline by executing the command below from the application directory:

dagger run go run ci/main.go

Dagger performs the operations defined in the pipeline script, logging each operation to the console. Once the pipeline is resolved, it outputs a string similar to the one below.

Hello from Dagger and go version go1.19.5 linux/amd64

Well done! You've just successfully written and run your first Dagger pipeline!