๐Ÿ”ง DevOps

Dagger

Dagger

A programmable CI/CD engine. Container-based and can run locally.

Detailed Explanation

Dagger is an innovative tool that lets you write CI/CD pipelines in a programming language. Created by Solomon Hykes, co-founder of Docker, this tool defines pipelines in familiar programming languages such as Go, Python, and Node.js instead of YAML or Jenkins scripts.

Key features

  • Pipeline as Code: Write CI/CD logic in a real programming language
  • Local execution: Test pipelines on a development machine without a CI server
  • Container-based: All tasks run in containers, ensuring reproducibility
  • Caching: Caches build results to speed up repeated runs
  • Portability: Works the same on any CI/CD platform

Dagger Architecture

  • Dagger Engine: Pipeline execution engine (Docker-based)
  • Dagger SDK: Provides Go, Python, Node.js, and Rust SDKs
  • GraphQL API: Communication between pipeline components
  • Dagger Cloud: Distributed caching and visualization (optional)

Code Example

CI Pipeline Using the Go SDK

// main.go - Dagger CI ํŒŒ์ดํ”„๋ผ์ธ
package main

import (
    "context"
    "fmt"
    "os"

    "dagger.io/dagger"
)

func main() {
    if err := build(context.Background()); err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
}

func build(ctx context.Context) error {
    // Dagger ํด๋ผ์ด์–ธํŠธ ์ดˆ๊ธฐํ™”
    client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stdout))
    if err != nil {
        return err
    }
    defer client.Close()

    // ์†Œ์Šค ์ฝ”๋“œ ๋งˆ์šดํŠธ
    src := client.Host().Directory(".", dagger.HostDirectoryOpts{
        Exclude: []string{"node_modules", ".git"},
    })

    // Node.js ์ปจํ…Œ์ด๋„ˆ ์„ค์ •
    node := client.Container().
        From("node:20-alpine").
        WithDirectory("/app", src).
        WithWorkdir("/app")

    // ์˜์กด์„ฑ ์„ค์น˜
    node = node.WithExec([]string{"npm", "install"})

    // ํ…Œ์ŠคํŠธ ์‹คํ–‰
    node = node.WithExec([]string{"npm", "test"})

    // ๋นŒ๋“œ ์‹คํ–‰
    node = node.WithExec([]string{"npm", "run", "build"})

    // ๋นŒ๋“œ ๊ฒฐ๊ณผ๋ฌผ ๋‚ด๋ณด๋‚ด๊ธฐ
    _, err = node.Directory("/app/dist").Export(ctx, "./dist")
    if err != nil {
        return err
    }

    fmt.Println("Build completed successfully!")
    return nil
}

// Docker ์ด๋ฏธ์ง€ ๋นŒ๋“œ ๋ฐ ํ‘ธ์‹œ
func buildAndPush(ctx context.Context, registry, tag string) error {
    client, err := dagger.Connect(ctx)
    if err != nil {
        return err
    }
    defer client.Close()

    // ๋ ˆ์ง€์ŠคํŠธ๋ฆฌ ์ธ์ฆ
    secret := client.SetSecret("registry-password", os.Getenv("REGISTRY_PASSWORD"))

    // ์ด๋ฏธ์ง€ ๋นŒ๋“œ
    src := client.Host().Directory(".")
    image := client.Container().
        Build(src).
        WithRegistryAuth(registry, "username", secret)

    // ์ด๋ฏธ์ง€ ํ‘ธ์‹œ
    addr, err := image.Publish(ctx, fmt.Sprintf("%s/myapp:%s", registry, tag))
    if err != nil {
        return err
    }

    fmt.Printf("Published: %s\n", addr)
    return nil
}

Python SDK Example

# ci.py - Dagger Python SDK
import sys
import anyio
import dagger

async def main():
    config = dagger.Config(log_output=sys.stdout)

    async with dagger.Connection(config) as client:
        # Python ์ปจํ…Œ์ด๋„ˆ ์„ค์ •
        python = (
            client.container()
            .from_("python:3.11-slim")
            .with_directory("/app", client.host().directory("."))
            .with_workdir("/app")
        )

        # ์˜์กด์„ฑ ์„ค์น˜
        python = python.with_exec(["pip", "install", "-r", "requirements.txt"])

        # ๋ฆฐํŠธ ๊ฒ€์‚ฌ
        python = python.with_exec(["flake8", "."])

        # ํ…Œ์ŠคํŠธ ์‹คํ–‰
        python = python.with_exec(["pytest", "-v"])

        # ๊ฒฐ๊ณผ ์ถœ๋ ฅ
        output = await python.stdout()
        print(output)

if __name__ == "__main__":
    anyio.run(main)

dagger.json Module Configuration

{
  "name": "my-pipeline",
  "sdk": "go",
  "dependencies": [],
  "source": "dagger"
}

Practical Conversation Example

DevOps engineer
"As our Jenkins pipelines get more complex, managing the YAML is becoming difficult. Testing locally is hard too..."
Senior developer
"How about trying Dagger? You can write pipelines in Go or Python, so you get IDE support and can test right on your local machine. Our team's backend is in Go, so if we write it with the Go SDK, everyone can understand and modify it."
Junior Developer
"But can we use it alongside our existing GitHub Actions?"
Senior developer
"Yes, Dagger can run in any CI environment. You can call it from GitHub Actions with a single `dagger run go run ./ci` command. The core logic lives in Dagger, and only the trigger stays in GitHub Actions. Even if we move to GitLab CI or CircleCI later, the pipeline code stays the same."

Cautions

Docker Dependency

Dagger requires Docker (or Podman, containerd). It cannot run in an environment without Docker, and the CI runner must have Docker permissions.

Learning curve

Teams accustomed to YAML-based CI need time to adapt at first. An understanding of how to use the SDK and of container concepts is required.

Debugging Complexity

Because it runs across multiple container layers, debugging can be complex. Skill with the `--debug` flag and log analysis is needed.

Initial Build Time

The first run takes time because of base image pulling. Once caching is applied, subsequent builds are faster.

Learn More