Back to Dagger

Expose Host Service To Function

docs/current_docs/partials/cookbook/services/_expose-host-service-to-function.mdx

0.20.71.6 KB
Original Source

Expose host services to Dagger Functions

The following Dagger Function accepts a Service running on the host, binds it using an alias, and creates a client to access it via the service binding. This example uses a MariaDB database service running on host port 3306, aliased as db in the Dagger Function.

:::note This implies that a service is already listening on a port on the host, out-of-band of Dagger. :::

<Tabs groupId="language" queryString="sdk"> <TabItem value="go" label="Go">
go
</TabItem> <TabItem value="python" label="Python">
python
</TabItem> <TabItem value="typescript" label="TypeScript">
typescript
</TabItem> <TabItem value="php" label="PHP">
php
</TabItem> </Tabs>

Example

Send a query to the database service listening on host port 3306 and return the result as a string:

<Tabs groupId="shell"> <TabItem value="System shell"> ```shell dagger -c 'user-list tcp://localhost:3306' ``` </TabItem> <TabItem value="Dagger Shell"> ```shell title="First type 'dagger' for interactive mode." user-list tcp://localhost:3306 ``` </TabItem> <TabItem value="Dagger CLI"> ```shell dagger call user-list --svc=tcp://localhost:3306 ``` </TabItem> </Tabs>