Friday, November 18, 2022
HomeWeb DevelopmentUtilizing Dagger with Docker for higher CI/CD

Utilizing Dagger with Docker for higher CI/CD


Constructing and managing purposes in improvement or native environments could be a day by day battle for devs. Like something, wouldn’t or not it’s nice if it had been extra simple? Fortunately for us, there’s a device known as Dagger!

On this article, we will likely be demonstrating how you should use Dagger with Docker to enhance your CI/CD pipelines.

Leap forward:

Why CI/CD is essential

CI/CD pipelines have develop into a vital a part of our software program improvement lifecycles. Whereas they make it lots simpler to launch software program reliably and persistently, additionally they add a level of complexity and have a little bit of a studying curve for builders to keep in mind.

Most CI/CD instruments are hosted on the cloud, that means you’ll have to push your adjustments to a distant repository and sometimes wait a very long time for the CI course of to execute.

Chances are you’ll be pondering why we will’t simply run it domestically. Nicely, most CI/CD instruments don’t work domestically, and those that do are very tough to arrange. What we’d like is the flexibility to run our CI/CD pipelines domestically on our machine, whereas additionally in a cloud-based CI/CD device of our selection.

And that’s the place Dagger is available in.

What’s Dagger?

Dagger is an open-source devkit for CI/CD. It really works utilizing Cue, a robust configuration language made by Google that helps to validate and outline text-based and dynamic configurations. We may also be utilizing BuildKit, through which Dagger’s configuration is executed.

Moreover, the client-server structure has the identical idea as that of Docker. The Dagger daemon engine, in the meantime, runs on any container runtime.

Dagger goals to unravel many points by standardizing the best way we write CI/CD pipelines. On this tutorial, we’ll discover learn how to set up and arrange Dagger and construct a container picture.

Earlier than we proceed, it’s essential to be aware of the next applied sciences:

  • Dagger: A device that enables for the event, testing, and execution of pipelines domestically
  • CUE: A language that helps to validate and outline text-based and dynamic configuration
  • Docker: An open-source platform for creating, delivery, and operating purposes
  • CI/CD: Steady integration (CI) and steady supply (CD) is a contemporary software program improvement follow through which incremental code adjustments are made persistently and reliably

(Be aware: Dagger’s configuration is executed in BuildKit — this is among the most used options of Docker Engine)

Set up

Earlier than you start, you’ll must have Docker Engine put in, as that is required for Dagger to work. When you don’t have Docker put in, you are able to do so through the use of a Docker set up script.

Docker

Run the next to get began:

curl -fsSL https://get.docker.com -o get-docker.sh
sh get-docker.sh

As soon as that is accomplished, you’ll be able to run docker model to make certain Docker was put in accurately.

Dagger

The subsequent step is to put in Dagger, and this may be achieved utilizing the next set up script:

cd /usr/native
curl -L https://dl.dagger.io/dagger/set up.sh | sh

If you’re not a brilliant consumer, you might must append sudo in entrance of the final command.

As soon as full, run the command dagger model to verify Dagger’s set up.

(Be aware: If you’re operating a unique working system (not Linux or Mac) or face any points throughout set up, you’ll be able to take a look at the official set up information right here)

What Dagger does

In any CI system, you want to outline steps and actions in a sure format (YAML, most probably) and run it in your CI system.

Dagger makes it potential to:

  1. Automate actions together with your favored programming language
  2. Take a look at and debug immediately in your native machine
  3. Combine with current pipelines on any Docker-compatible runtime
  4. Reuse actions from a big and rising catalog
  5. Tie all of it collectively utilizing the CUE language

Dagger actions and lifecycle

As famous within the official docs, actions are the fundamental constructing blocks of Dagger  —  they summary advanced automation into easy elements that may be reused.

Actions may be executed utilizing the next command:

dagger do <action_name>

A cool function of Dagger is that it doesn’t want to tell apart between pipelines and steps; all the pieces is just an motion.

There are two varieties of Dagger actions:

  • Core actions, that are applied by the Dagger engine itself
  • Composite actions, that are made out of different actions

Composite actions have 4 lifecycle levels:

  • Definition: A CUE syntax that describes the motion’s inputs, outputs, sub-actions, and their connection
  • Integration: A relationship between an motion’s definition and a Dagger plan
  • Discovery: Figuring out the composite motion(s)
  • Execution: Working the composite motion(s)

Lifecycle Of A Dagger Composite Action

Organising our Dagger plan

All of it begins with a plan! The key phrase dagger.#Plan is used to determine a plan in our Dagger configuration information.

A plan is an execution context for actions. It specifies:

  • What actions to current to the tip consumer
  • Dependencies between these duties
  • Interactions between the duties and the consumer system

Now, let’s transfer on to utilizing Dagger to construct container photos.

Constructing Docker photos with Dagger

Dagger can execute common Docker information to construct container photos.

To start, you want to create a major.py file with the next contents (this may be a Python software):

# filename: major.py

from flask import Flask
app = Flask(__name__)
@app.route("https://weblog.logrocket.com/")
def hello_world():
    return "<p>Hiya, Dagger!</p>"

The subsequent step is to create a Dagger configuration file; dagger.cue.


Extra nice articles from LogRocket:


// filename: dagger.cue
package deal major

import (
    "dagger.io/dagger"
    "universe.dagger.io/docker"
)

#PythonBuild: docker.#Dockerfile & {
    dockerfile: contents: """
        FROM python:3.7-stretch
        RUN apt-get replace -y
        RUN apt-get set up -y python-pip python-dev build-essential
        COPY . /app
        WORKDIR /app
        RUN pip set up flask
        ENTRYPOINT ["python"]
        CMD ["main.py"]
        """
}

dagger.#Plan & {
    consumer: filesystem: "./": learn: contents: dagger.#FS
    actions: construct: #PythonBuild & {
        supply: consumer.filesystem."./".learn.contents
    }
}

You may see from the Dagger configuration that the motion builds a Docker picture from the Python software we created earlier.

As well as, the construct steps are outlined utilizing an inline method that enables us to jot down the Dockerfile contents inside Dagger’s configuration.

It’s required to initialize and replace the venture, as this installs these required Dagger packages. You are able to do this utilizing the next instructions:

dagger venture init
dagger venture replace

Now, we will proceed to executing the construct plan utilizing Docker. To set off the construct motion, run the next command:

dagger do construct 

You can even verify that Dagger has efficiently constructed the container picture utilizing the Dockerfile contents by checking the checklist of Docker photos to confirm that the picture exists, as proven right here:

Dockerfile Contents

Now, you understand how to construct container photos with Dagger!

Advantages of utilizing Dagger with Docker for CI/CD

Dagger is making an effort to interchange the difficult Bash scripts that many current CI/CD techniques require rather than extra modular, faster, and moveable elements.

Chances are you’ll create reusable modules which might be tailored to your wants and depending on the instruments you’re utilizing to supply them, and different folks can use your modules as a place to begin to create new modules which might be suited to their distinctive use instances.

As somebody that works for a consulting agency, now we have a variety of use instances the place we should continuously swap between initiatives that use totally different CI/CD stacks. Subsequently, the promise to construct an abstraction layer on high of a present CI runner appears intriguing to me — impartial of CI runners, we may arrange our personal processes and apply them to our initiatives.

Conclusion

To complete up our article in the present day, there are a number of use instances for why you might need to use Dagger with Docker on your CI/CD pipelines.

When you continuously write intricate construct or deployment scripts, share massive sections of scripts between groups, spend quite a lot of time making an attempt to debug issues within the CI, or favor a neighborhood construct to be a simple script, then Dagger might be the device for you.

Likewise, it may additionally be best for you in case your CI pipeline is taking too lengthy to do duties serially when it may be parallelized. Lastly, the venture we’ve labored on in the present day exhibits how easy it’s to create container photos utilizing Dagger!

Listed below are some further useful sources you should use on your initiatives:

Let me find out about your experiences utilizing Dagger with Docker within the feedback part under!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments