docs/current_docs/extending/modules/functions.mdx
import DaggerModuleInit from '@partials/_dagger_module_init.mdx';
Dagger Functions are individual units of computation that perform a specific task by combining operations on components with custom logic. Dagger Functions are written in a programming language using a type-safe Dagger SDK, and packaged and shared in Dagger modules.
Here's an example of a Dagger Function which calls a remote API method and returns the result:
<Tabs groupId="language" queryString="sdk"> <TabItem value="go" label="Go">Update the main.go file with the following code:
This Dagger Function includes the context as input and error as return in its signature. </TabItem> <TabItem value="python" label="Python">
Update the src/my_module/main.py file with the following code:
Dagger Functions are implemented as @dagger.function decorated methods, of a @dagger.object_type decorated class.
It's possible for a module to implement multiple classes (object types), but the first one needs to have a name that matches the module's name, in PascalCase. This object is sometimes referred to as the "main object".
For example, for a module initialized with dagger init --name=my-module,
the main object needs to be named MyModule.
</TabItem> <TabItem value="typescript" label="TypeScript">
Update the src/index.ts file with the following code:
Update the src/MyModule.php file with the following code:
Update the src/main/java/io/dagger/modules/mymodule/MyModule.java file with the following code:
Dagger Functions must be public. The function must be decorated with the @Function annotation
and the class containing the functions must be decorated with the @Object annotation.
</TabItem>
</Tabs>
:::caution
You can try this Dagger Function by copying it into the default template generated by dagger init, but remember that you must update the module name in the code samples above to match the name used when your module was first initialized.
:::
In simple terms, here is what this Dagger Function does:
alpine base image.apk add ... command in the container to add the curl and jq utilities.curl utility to send an HTTP request to the URL https://randomuser.me/api/ and parses the response using jq.Here is an example call for this Dagger Function:
<Tabs groupId="shell"> <TabItem value="System shell"> ```shell dagger -c 'get-user' ``` </TabItem> <TabItem value="Dagger Shell"> ```shell title="First type 'dagger' for interactive mode." get-user ``` </TabItem> <TabItem value="Dagger CLI"> ```shell dagger call get-user ``` </TabItem> </Tabs> Here's what you should see:{
"title": "Mrs",
"first": "Beatrice",
"last": "Lavigne"
}
:::important Dagger Functions execute within containers spawned by the Dagger Engine. This "sandboxing" serves a few important purposes:
When implementing Dagger Functions, you are free to write arbitrary code that will execute inside the Dagger module's container. You have access to the Dagger API to make calls to the core Dagger API or other Dagger modules you depend on, but you are also free to just use the language's standard library and/or imported third-party libraries.
The process your code executes in will currently be with the root user, but without a full set of Linux capabilities and other standard container sandboxing provided by runc.
The current working directory of your code will be an initially empty directory. You can write and read files and directories in this directory if needed. This includes using the Container.export(), Directory.export() or File.export() APIs to write those artifacts to this local directory if needed.