2021-06-02 16:53:39 +02:00
---
sidebar_position: 1
slug: /programming
---
2021-05-01 09:24:53 +02:00
# Dagger Programming Guide
2021-02-08 20:47:07 +01:00
2021-03-25 09:56:26 +01:00
## Overview
2021-03-18 06:44:50 +01:00
2021-06-02 16:53:39 +02:00
1. A developer writes a _plan_ specifying how to deliver their application. Plans are written in the [Cue ](https://cuelang.org ) data language.
2. Dagger executes plans in isolated _environments_ . Each environment has its own configuration and state.
2021-03-25 09:56:26 +01:00
2021-05-01 09:15:03 +02:00
## Programming in Cue
[Cue ](https://cuelang.org ) is a next-generation data language by Marcel van Lohuizen and the spiritual successor
2021-05-22 00:02:18 +02:00
of GCL, the language used to configure all of Google's infrastructure.
2021-05-01 09:15:03 +02:00
Cue extends JSON with powerful features:
2021-06-02 16:53:39 +02:00
- Composition: layering, templating, references
- Correctness: types, schemas
- Developer experience: comments, packages, first-class tooling, builtin functions
- And mucn more.
2021-05-01 09:15:03 +02:00
2021-05-01 09:46:49 +02:00
To get started with Cue, we recommend the following resources:
2021-06-02 16:53:39 +02:00
- [Cuetorials ](https://cuetorials.com )
- [Cue playground ](https://cuelang.org/play/ )
2021-05-01 09:46:49 +02:00
2021-05-01 09:15:03 +02:00
## Writing your first plan
To create a Dagger plan:
2021-05-26 00:20:44 +02:00
1\. Initialize a Dagger workspace anywhere in your git repository.
2021-05-01 09:15:03 +02:00
2021-05-26 00:20:44 +02:00
`dagger init` .
2021-05-01 09:15:03 +02:00
2021-05-26 00:20:44 +02:00
2\. Create a new environment.
2021-05-01 09:15:03 +02:00
2021-05-26 00:20:44 +02:00
For example: `dagger new staging` .
2021-05-01 09:15:03 +02:00
2021-06-02 16:53:39 +02:00
3\. Create a new file with the _.cue_ extension in `.dagger/env/staging/plan` , and open it with any text editor or IDE.
2021-05-26 00:20:44 +02:00
For example: `.dagger/env/staging/plan/staging.cue` .
4\. Describe each relay in your plan as a field in the cue configuration.
2021-05-01 09:15:03 +02:00
For example:
2021-05-22 00:02:18 +02:00
```cue
2021-05-01 09:46:49 +02:00
package main
2021-05-01 09:15:03 +02:00
import (
2021-05-22 00:02:18 +02:00
"dagger.io/docker"
"dagger.io/git"
2021-05-01 09:15:03 +02:00
)
// Relay for fetching a git repository
repo: git.#Repository & {
2021-05-22 00:02:18 +02:00
remote: "https://github.com/dagger/dagger"
ref: "main"
2021-05-01 09:15:03 +02:00
}
// Relay for building a docker image
ctr: docker.#Build & {
2021-05-22 00:02:18 +02:00
source: repo
2021-05-01 09:15:03 +02:00
}
```
2021-05-01 09:37:54 +02:00
For more inspiration, see these examples:
2021-05-22 00:02:18 +02:00
2021-06-02 16:53:39 +02:00
- [Deploy a static page to S3 ](https://github.com/dagger/dagger/blob/main/examples/README.md#deploy-a-static-page-to-s3 )
- [Deploy a simple React application ](https://github.com/dagger/dagger/blob/main/examples/README.md#deploy-a-simple-react-application )
- [Deploy a complete JAMstack app ](https://github.com/dagger/dagger/blob/main/examples/README.md#deploy-a-complete-jamstack-app )
- [Provision a Kubernetes cluster on AWS ](https://github.com/dagger/dagger/blob/main/examples/README.md#provision-a-kubernetes-cluster-on-aws )
- [Add HTTP monitoring to your application ](https://github.com/dagger/dagger/blob/main/examples/README.md#add-http-monitoring-to-your-application )
- [Deploy an application to your Kubernetes cluster ](https://github.com/dagger/dagger/blob/main/examples/README.md#deploy-an-application-to-your-kubernetes-cluster )
2021-05-01 09:15:03 +02:00
2021-06-02 21:51:41 +02:00
5\. Extend your plan with relay definitions from [Dagger
Universe](https://github.com/dagger/dagger/tree/main/stdlib), an encyclopedia of
cue packages curated by the Dagger community.
2021-05-01 09:15:03 +02:00
2021-05-26 00:20:44 +02:00
6\. If you can't find the relay you need in the Universe, you can simply create your own.
2021-05-01 09:15:03 +02:00
For example:
2021-05-22 00:02:18 +02:00
```cue
2021-05-01 09:59:49 +02:00
import (
2021-05-22 00:02:18 +02:00
"strings"
2021-05-01 09:59:49 +02:00
)
2021-05-01 09:15:03 +02:00
// Create a relay definition which generates a greeting message
#Greeting: {
2021-05-22 00:02:18 +02:00
salutation: string | *"hello"
name: string | *"world"
message: "\(strings.ToTitle(salutation)), \(name)!"
2021-05-01 09:15:03 +02:00
}
```
You may then create any number of relays from the same definition:
2021-05-22 00:02:18 +02:00
```cue
2021-05-01 09:15:03 +02:00
french: #Greeting & {
2021-05-22 00:02:18 +02:00
salutation: "bonjour"
name: "monde"
2021-05-01 09:15:03 +02:00
}
american: #Greeting & {
2021-05-22 00:02:18 +02:00
salutation: "hi"
name: "y'all"
2021-05-01 09:15:03 +02:00
}
```
## Concepts
### Plans
2021-03-25 09:56:26 +01:00
2021-06-02 16:53:39 +02:00
A _plan_ specifies, in code, how to deliver a particular application in a particular way.
2021-03-25 09:56:26 +01:00
2021-04-24 03:00:00 +02:00
It lays out the application's supply chain as a graph of interconnected nodes:
2021-02-08 20:47:07 +01:00
2021-06-02 16:53:39 +02:00
- Development tools: source control, CI, build systems, testing systems
- Hosting infrastructure: compute, storage, networking, databases, CDN..
- Software dependencies: operating systems, languages, libraries, frameworks, etc.
2021-02-08 20:47:07 +01:00
2021-04-24 03:00:00 +02:00
The graph models the flow of code and data through the supply chain:
2021-05-22 00:02:18 +02:00
2021-06-02 16:53:39 +02:00
- source code flows from a git repository to a build system;
- system dependencies are combined in a docker image, then uploaded to a registry;
- configuration files are generated then sent to a compute cluster or load balancer;
- etc.
2021-04-24 03:00:00 +02:00
Dagger plans are written in [Cue ](https://cuelang.org ), a powerful declarative language by the creator of GQL, the language used to deploy all applications at Google.
2021-05-01 09:15:03 +02:00
### Environments
2021-04-24 03:00:00 +02:00
2021-06-02 16:53:39 +02:00
An _environment_ is a live implementation of a _plan_ , with its own user inputs and state.
2021-04-24 03:00:00 +02:00
The same plan can be executed in multiple environments, for example to differentiate production from staging.
An environment can be updated with `dagger up` . When updating an environment, Dagger determines which inputs have
changed since the last update, and runs them through the corresponding pipelines to produce new outputs.
For example, if an application has a new version of its frontend source code available, but no changes to
the frontend, it will build, test and deploy the new frontend, without changing the backend.
2021-02-08 20:47:07 +01:00
2021-05-01 09:15:03 +02:00
### Relays
2021-02-08 20:47:07 +01:00
2021-06-02 16:53:39 +02:00
_Relays_ are the basic components of a _plan_ . Each relay is a node in the graph defined by the plan,
2021-04-24 03:00:00 +02:00
performing the task assigned to that node. For example one relay fetches source code; another runs a build;
another uploads a container image; etc.
2021-02-08 20:47:07 +01:00
2021-06-02 21:51:41 +02:00
Relays are standalone software components: they are defined in [Cue ](https://cuelang.org/ ), but can
execute code in any language using the [Dagger pipeline
API](https://github.com/dagger/dagger/blob/main/stdlib/dagger/op/op.cue).
2021-03-27 01:13:43 +01:00
2021-04-24 03:00:00 +02:00
A relay is made of 3 parts:
2021-05-22 00:02:18 +02:00
2021-06-02 16:53:39 +02:00
- Inputs: data received from the user, or upstream relays
2021-06-02 21:51:41 +02:00
- A processing pipeline: code executed against each new input, using the
[pipeline
API](https://github.com/dagger/dagger/blob/main/stdlib/dagger/op/op.cue)
2021-06-02 16:53:39 +02:00
- Outputs: data produced by the processing pipeline
2021-03-27 01:13:43 +01:00
Relays run in parallel, with their inputs and outputs interconnected into a special kind of graph,
2021-06-02 16:53:39 +02:00
called a _DAG_ . When a relay receives a new input, it runs it through the processing pipeline,
2021-03-27 01:13:43 +01:00
and produces new outputs, which are propagated to downstream relays as inputs, and so on.
2021-05-01 09:15:03 +02:00
### Using third-party relays
2021-02-08 20:47:07 +01:00
2021-04-24 03:00:00 +02:00
Cue includes a complete package system. This makes it easy to create a complex plan in very few
2021-03-27 01:13:43 +01:00
lines of codes, simply by importing relays from third-party packages.
2021-03-18 06:44:50 +01:00
2021-04-24 03:00:00 +02:00
For example, to create a plan involving Github, Heroku and Amazon RDS, one might import the three
2021-03-26 23:57:52 +01:00
corresponding packages:
2021-03-18 06:44:50 +01:00
2021-05-22 00:02:18 +02:00
```cue
2021-03-26 23:57:52 +01:00
import (
2021-05-22 00:02:18 +02:00
"dagger.io/github"
"dagger.io/heroku"
"dagger.io/amazon/rds"
2021-03-26 23:57:52 +01:00
)
2021-03-18 06:44:50 +01:00
2021-03-26 23:57:52 +01:00
repo: github.#Repository & {
2021-05-22 00:02:18 +02:00
// Github configuration values
2021-03-26 23:57:52 +01:00
}
2021-03-18 06:44:50 +01:00
2021-03-26 23:57:52 +01:00
backend: heroku.#App & {
2021-05-22 00:02:18 +02:00
// Heroku configuration values
2021-03-26 23:57:52 +01:00
}
db: rds.#Database & {
2021-05-22 00:02:18 +02:00
// RDS configuration values
2021-03-26 23:57:52 +01:00
}
2021-03-27 01:13:43 +01:00
```
2021-05-01 09:15:03 +02:00
### Creating a new relay
2021-03-26 23:57:52 +01:00
2021-04-24 03:00:00 +02:00
Sometimes there is no third-party relay available for a particular task in your workflow; or it may exist but need to be customized.
2021-03-26 23:57:52 +01:00
2021-04-24 03:00:00 +02:00
A relay is typically contained in a [cue definition ](https://cuetorials.com/overview/foundations/#definitions ), with the definition name describing its function.
2021-03-27 01:13:43 +01:00
For example a relay for a git repository might be defined as `#Repository` .
2021-03-26 23:57:52 +01:00
2021-03-27 01:13:43 +01:00
The processing pipeline is a crucial feature of Dagger. It uses the [LLB ](https://github.com/moby/buildkit )
executable format pioneered by the Buildkit project. It allows Dagger components to run
sophisticated pipelines to ingest produce artifacts such as source code, binaries, database exports, etc.
Best of all, LLB pipelines can securely build and run any docker container, effectively making Dagger
2021-03-26 23:57:52 +01:00
scriptable in any language.
2021-03-27 01:13:43 +01:00
2021-05-01 09:15:03 +02:00
### Docker compatibility
2021-03-27 01:13:43 +01:00
Thanks to its native support of LLB, Dagger offers native compatibility with Docker.
This makes it very easy to extend an existing Docker-based workflow, including:
2021-06-02 16:53:39 +02:00
- Reusing Dockerfiles and docker-compose files without modification
- Wrapping other deployment tools in a Dagger relay by running them inside a container
- Robust multi-arch and multi-OS support, including Arm and Windows.
- Integration with existing Docker engines and registries
- Integration with Docker for Mac and Docker for Windows on developer machines