2023-05

2023-05-10 Using Dockerfiles with Dagger

Most organizations have Dockerfiles somewhere. Some have a lot of them.

They might be managed by another team, and maybe your team only interacts with the built images, but chances are that if you’re thinking about CI, you’ve thought about writing, maintaining, simplifying, or getting rid of your Dockerfiles.

If you’re using Dagger to turn your pipelines into code via a Dagger SDK in Go, Python, or Node.js (or via the GraphQL API directly), then you have a couple of options on how to handle your Dockerfiles.

  • Keep your existing Dockerfiles and drive Docker builds from Dagger

  • Turn your Dockerfiles into Dagger code

Let’s examine each of these.

Turn your Dockerfiles into Dagger code

Whether your Dockerfiles are super simple (and thus trivial to turn into Dagger SDK code), or super gnarly, converting your existing Dockerfiles into Dagger code means you can take full advantage of both Dagger and your language of choice to create testable, extensible builds and pipelines. It might be that Dockerfile syntax is a mystery to many of your engineers and thus folks are reluctant to touch them, or that you’ve outgrown the Dockerfile format and need more control that a full programming language can offer including types, functions, loops, and conditionals.

If you’re familiar with Dockerfiles or the concepts behind them, you’ll find the Dagger API implemented by the SDKs has familiar constructs for working with Containers, Directories, Files, Git, Environment Variables, Secrets, etc.

Luckily we have an excellent blog post and technical guide that illustrates the approach in Go, Python and Node.js. It shows you how to

  • Understand and break down a Dockerfile into steps

  • Create a Dagger client

  • Write a Dagger pipeline to replicate the Dockerfile build process, taking advantage of functions and the Dagger API implemented by your SDK of choice to:

  • Configure a container with all required dependencies and environment variables for an application

    • Download and build the application source code in the container

    • Set the container entrypoint

    • Publish the built container image to Docker Hub

  • Test the Dagger pipeline locally

Conclusion

Whether you need to keep your existing Dockerfiles around for a while or are ready to turn them into Dagger SDK code, you’ve now got the tools to incorporate your existing Dockerfiles into your Dagger pipelines.

As always, we’re available in Discord if you need help!