Quickstart
Test the application
The pipeline in the previous example was illustrative - you wouldn't ever use it in the real world! So let's write a Dagger pipeline to do something more useful, like running an application's tests.
The code listing below demonstrates a Dagger pipeline that runs tests for the example application, by executing the npm run test
command.
The npm run test
command is appropriate for a React application, but other applications are likely to use different commands. Modify your Dagger pipeline accordingly.
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 node:16-slim container
// mount the source code directory on the host
// at /src in the container
source := client.Container().
From("node:16-slim").
WithDirectory("/src", client.Host().Directory(".", dagger.HostDirectoryOpts{
Exclude: []string{"node_modules/", "ci/", "build/"},
}))
// set the working directory in the container
// install application dependencies
runner := source.WithWorkdir("/src").
WithExec([]string{"npm", "install"})
// run application tests
out, err := runner.WithExec([]string{"npm", "test", "--", "--watchAll=false"}).
Stderr(ctx)
if err != nil {
panic(err)
}
fmt.Println(out)
}
This code listing does the following:
- It creates a Dagger client with
Connect()
as before. - It uses the client's
Container().From()
method to initialize a new container from a base image - again, thenode:16-slim
image. This base image is the Node.js version to use for testing. TheFrom()
method returns a newContainer
object with the result. - It uses the
Container.WithDirectory()
method to mount the source code directory on the host at the/src
mount point in the container, and theContainer.WithWorkdir()
method to set the working directory to that mount point.- Notice that the
Container.WithDirectory()
method accepts additional options to exclude (or include) specific files from the mount. In this case, it excludes thenode_modules
(locally-installed dependencies),ci
(pipeline code) andbuild
(build artifacts) directories.
- Notice that the
- It uses the
Container.WithExec()
method to define the commands to install dependencies and run tests in the container - in this case, the commandsnpm install
andnpm test -- --watchAll=false
. - It uses the
Container.Stderr()
method to return the error stream of the last executed command. No error output implies successful execution (all tests pass).- Failure, indicated by error output, will cause the pipeline to terminate.
Run the pipeline by executing the command below from the application directory:
dagger run go run ci/main.go
The From()
, WithDirectory()
, WithWorkdir()
and WithExec()
methods all return a Container
, making it easy to chain method calls together and create a pipeline that is intuitive to understand.
import { connect } from "@dagger.io/dagger"
connect(
async (client) => {
// use a node:16-slim container
// mount the source code directory on the host
// at /src in the container
const source = client
.container()
.from("node:16-slim")
.withDirectory(
"/src",
client
.host()
.directory(".", { exclude: ["node_modules/", "ci/", "build/"] }),
)
// set the working directory in the container
// install application dependencies
const runner = source.withWorkdir("/src").withExec(["npm", "install"])
// run application tests
const out = await runner
.withExec(["npm", "test", "--", "--watchAll=false"])
.stderr()
console.log(out)
},
{ LogOutput: process.stderr },
)
This code listing does the following:
- It creates a Dagger client with
connect()
as before. - It uses the client's
container().from()
method to initialize a new container from a base image - again, thenode:16-slim
image. This base image is the Node.js version to use for testing. Thefrom()
method returns a newContainer
object with the result. - It uses the
Container.withDirectory()
method to mount the source code directory on the host at the/src
mount point in the container, and theContainer.withWorkdir()
method to set the working directory to that mount point.- Notice that the
Container.withDirectory()
method accepts additional options to exclude (or include) specific files from the mount. In this case, it excludes thenode_modules
(locally-installed dependencies),ci
(pipeline code) andbuild
(build artifacts) directories.
- Notice that the
- It uses the
Container.withExec()
method to define the commands to install dependencies and run tests in the container - in this case, the commandsnpm install
andnpm test -- --watchAll=false
. - It uses the
Container.stderr()
method to return the error stream of the last executed command. No error output implies successful execution (all tests pass).- Failure, indicated by error output, will cause the pipeline to terminate.
Run the pipeline by executing the command below from the application directory:
dagger run node ci/index.mjs
The from()
, withDirectory()
, withWorkdir()
and withExec()
methods all return a Container
, making it easy to chain method calls together and create a pipeline that is intuitive to understand.
import sys
import anyio
import dagger
async def main():
config = dagger.Config(log_output=sys.stdout)
async with dagger.Connection(config) as client:
# use a node:16-slim container
# mount the source code directory on the host
# at /src in the container
source = (
client.container()
.from_("node:16-slim")
.with_directory(
"/src",
client.host().directory(
".", exclude=["node_modules/", "ci/", "build/"]
),
)
)
# set the working directory in the container
# install application dependencies
runner = source.with_workdir("/src").with_exec(["npm", "install"])
# run application tests
out = await runner.with_exec(["npm", "test", "--", "--watchAll=false"]).stderr()
print(out)
anyio.run(main)
This code listing does the following:
- It creates a Dagger client with
with dagger.Connection()
as before. - It uses the client's
container().from_()
method to initialize a new container from a base image - again, thenode:16-slim
image. This base image is the Node.js version to use for testing. Thefrom_()
method returns a newContainer
object with the result. - It uses the
Container.with_directory()
method to mount the source code directory on the host at the/src
mount point in the container, and theContainer.with_workdir()
method to set the working directory to that mount point.- Notice that the
Container.with_directory()
method accepts additional options to exclude (or include) specific files from the mount. In this case, it excludes thenode_modules
(locally-installed dependencies),ci
(pipeline code) andbuild
(build artifacts) directories.
- Notice that the
- It uses the
Container.with_exec()
method to define the commands to install dependencies and run tests in the container - in this case, the commandsnpm install
andnpm test -- --watchAll=false
. - It uses the
Container.stderr()
method to return the error stream of the last executed command. No error output implies successful execution (all tests pass).- Failure, indicated by error output, will cause the pipeline to terminate.
Run the pipeline by executing the command below from the application directory:
dagger run python ci/main.py
The from_()
, with_directory()
, with_workdir()
and with_exec()
methods all return a Container
, making it easy to chain method calls together and create a pipeline that is intuitive to understand.