Skip to content

Commit

Permalink
Cookbook: integrating with a docker engine (dagger#5421)
Browse files Browse the repository at this point in the history
Cookbook: integrating with a Docker engine

Signed-off-by: Solomon Hykes <solomon@dagger.io>
Signed-off-by: Helder Correia <helder@dagger.io>
  • Loading branch information
shykes committed Jul 11, 2023
1 parent f057da6 commit c44009e
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 0 deletions.
32 changes: 32 additions & 0 deletions docs/current/742989-cookbook.md
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,38 @@ The following code listing demonstrates how to add multiple environment variable

## Integrations

### Docker Engine

The following code shows different ways to integrate with the Docker Engine.

#### Connecting to Docker Engine on the host

This shows how to connect to a Docker Engine on the host machine, by mounting the Docker unix socket into a container, and running the `docker` CLI.

<Tabs groupId="language">
<TabItem value="Go">

```go file=./cookbook/snippets/docker-engine-host/main.go
```

</TabItem>

<TabItem value="Node.js">

```javascript file=./cookbook/snippets/docker-engine-host/index.mjs
```

</TabItem>

<TabItem value="Python">

```python file=./cookbook/snippets/docker-engine-host/main.py
```

</TabItem>

</Tabs>

### AWS Cloud Development Kit

The following code listing builds, publishes and deploys a container using the Amazon Web Services (AWS) Cloud Development Kit (CDK).
Expand Down
21 changes: 21 additions & 0 deletions docs/current/cookbook/snippets/docker-engine-host/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { connect } from "@dagger.io/dagger"

// create Dagger client
connect(
async (client) => {
// setup container with docker socket
const ctr = client
.container()
.from("docker")
.withUnixSocket(
"/var/run/docker.sock",
client.host().unix_socket("/var/run/docker.sock")
)
.withExec(["docker", "run", "--rm", "alpine", "uname", "-a"])
.stdout()

// print docker run
console.log(await ctr.stdout())
},
{ LogOutput: process.stderr }
)
34 changes: 34 additions & 0 deletions docs/current/cookbook/snippets/docker-engine-host/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"context"
"fmt"

"os"

"dagger.io/dagger"
)

func main() {
// create Dagger client
ctx := context.Background()
client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stderr))
if err != nil {
panic(err)
}
defer client.Close()

// setup container with docker socket
ctr := client.
Container().
From("docker").
WithUnixSocket("/var/run/docker.sock", client.Host().UnixSocket("/var/run/docker.sock")).
WithExec([]string{"docker", "run", "--rm", "alpine", "uname", "-a"})

// print docker run
out, err := ctr.Stdout(ctx)
if err != nil {
panic(err)
}
fmt.Println(out)
}
26 changes: 26 additions & 0 deletions docs/current/cookbook/snippets/docker-engine-host/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import sys

import anyio

import dagger


async def main():
# create Dagger client
async with dagger.Connection(dagger.Config(log_output=sys.stderr)) as client:
# setup container with docker socket
ctr = (
client.container()
.from_("docker")
.with_unix_socket(
"/var/run/docker.sock",
client.host().unix_socket("/var/run/docker.sock"),
)
.with_exec(["docker", "run", "--rm", "alpine", "uname", "-a"])
)

# print docker run
print(await ctr.stdout())


anyio.run(main)

0 comments on commit c44009e

Please sign in to comment.