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.
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 aContainer
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 commandgo version
, which returns the Go version string. ThewithExec()
method returns a revisedContainer
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:
go run ci/main.go
Dagger resolves the pipeline and 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.
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 aContainer
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 commandnode -v
, which returns the Node version string. ThewithExec()
method returns a revisedContainer
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:
node ci/index.mjs
Dagger resolves the pipeline and 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.
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 aContainer
representing 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 revisedContainer
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:
python ci/main.py
Dagger resolves the pipeline and 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!
- Go
- Node
- Python