Skip to main content

Quickstart

Reuse existing Dockerfiles

As you've seen over the last few pages, Dagger is a powerful tool. It lets you create CI pipelines using general purpose programming languages, giving you full access to native control structures like conditionals and loops. By allowing you to import and use existing language extensions or packages in your pipeline code, Dagger makes it easier to quickly add new functionality or integrate with third-party services. By the same token, Dagger pipelines also benefit from static typing and easier refactoring.

However, rewriting your entire CI/CD system to use Dagger, all at once and without breaking compatibility, is not an easy task.

The good news here is that Dagger can natively run Dockerfiles with full compatibility. This means that it's easy to wrap your existing Dockerfile in a Dagger pipeline, and gradually refactor it over time, without breaking your team's workflow.

The example application repository includes a simple Dockerfile. Use it with a Dagger pipeline as shown below:

package main

import (
"context"
"fmt"
"math"
"math/rand"
"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()

contextDir := client.Host().Directory(".")

ref, err := contextDir.
DockerBuild().
Publish(ctx, fmt.Sprintf("ttl.sh/hello-dagger-%.0f", math.Floor(rand.Float64()*10000000))) //#nosec
if err != nil {
panic(err)
}

fmt.Printf("Published image to :%s\n", ref)
}

This code listing does the following:

  • It creates a Dagger client with Connect().
  • It uses the client's Host().Directory() method to obtain a reference to the source code directory on the host.
  • It uses the Directory object's DockerBuild() method to build a new container using a Dockerfile. This method defaults to using the Dockerfile located at ./Dockerfile in the directory passed to it as argument and returns the built Container object.
  • It uses the Container object's Publish() method to publish the container to ttl.sh. As before, to prevent name collisions, the container image name is suffixed with a random number.

Run the pipeline by executing the command below from the application directory:

dagger run go run ci/main.go

After Dagger resolves the pipeline, the newly-built container image will be available in the ttl.sh registry. Download and test it as described in the section on publishing the application.