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 isgolang:1.19. This method returns aContainerrepresenting 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 commandgo version, which returns the Go version string. TheWithExec()method returns a revisedContainerwith 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
In the ci directory, create a new file named index.mjs and add the following code to it.
import { connect } from "@dagger.io/dagger"
// initialize Dagger client
connect(
async (client) => {
// use a node:16-slim container
// get version
const node = client
.container()
.from("node:16-slim")
.withExec(["node", "-v"])
// execute
const version = await node.stdout()
// print output
console.log("Hello from Dagger and Node " + version)
},
{ LogOutput: process.stderr },
)
This Node.js script imports the Dagger SDK and defines an asynchronous function. This function performs the following operations:
- It creates a Dagger client with
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 isnode:16-slim. This method returns aContainerrepresenting 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 commandnode -v, which returns the Node version string. ThewithExec()method returns a revisedContainerwith 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 node ci/index.mjs
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 Node v16.18.1
In the ci directory, create a new file named main.py and add the following code to it.
import sys
import anyio
import dagger
async def main():
config = dagger.Config(log_output=sys.stdout)
# initialize Dagger client
async with dagger.Connection(config) as client:
# use a python:3.11-slim container
# get version
python = (
client.container().from_("python:3.11-slim").with_exec(["python", "-V"])
)
# execute
version = await python.stdout()
print(f"Hello from Dagger and {version}")
anyio.run(main)
This Python script imports the Dagger SDK and defines an asynchronous function. This function performs the following operations:
- It creates a Dagger client with
with dagger.Connection(). 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 ispython:3.11-slim. This method returns aContainerrepresenting an OCI-compatible container image. - It uses the
Container.with_exec()method to define the command to be executed in the container - in this case, the commandpython -V, which returns the Python version string. Thewith_exec()method returns a revisedContainerwith 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 python ci/main.py
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 Python v3.11.1
Well done! You've just successfully written and run your first Dagger pipeline!