Add docker cli package

Signed-off-by: Helder Correia <174525+helderco@users.noreply.github.com>
This commit is contained in:
Helder Correia 2022-03-25 15:17:11 -01:00
parent 4f440bc67a
commit 05d79e8ba6
No known key found for this signature in database
GPG Key ID: C6490D872EF1DCA7
14 changed files with 228 additions and 90 deletions

View File

@ -0,0 +1,20 @@
---
slug: /1216/docker-cli-load
displayed_sidebar: europa
---
# Loading a dagger image into a docker daemon
Using `cli.#Load`, you can save a dagger image (`docker.#Image`) into a local or remote engine.
It can be useful to debug or test a build locally before pushing.
## Local daemon
```cue file=./plans/docker-cli-load/local.cue
```
## Remote daemon, via SSH
```cue file=./plans/docker-cli-load/ssh.cue
```

View File

@ -1,20 +0,0 @@
---
slug: /1216/engine-load
displayed_sidebar: europa
---
# Loading a dagger image into a docker daemon
Using `docker.#Load`, you can save a dagger image (`docker.#Image`) into a local or remote engine.
It can be useful to debug or test a build locally before pushing.
## Local daemon
```cue file=./plans/local.cue
```
## Remote daemon, via SSH
```cue file=./plans/ssh.cue
```

View File

@ -0,0 +1,23 @@
---
slug: /1217/docker-cli-run
displayed_sidebar: europa
---
# Running commands with the docker binary (CLI)
There's a `universe.dagger.io/docker/cli` package that allows you to run docker commands against a local or remote docker engine. Here's a few examples.
## Local daemon
```cue file=./plans/docker-cli-run/local.cue
```
## Remote daemon, via SSH
```cue file=./plans/docker-cli-run/ssh.cue
```
## Remote daemon, via HTTPS
```cue file=./plans/docker-cli-run/tcp.cue
```

View File

@ -3,6 +3,7 @@ package main
import ( import (
"dagger.io/dagger" "dagger.io/dagger"
"universe.dagger.io/docker" "universe.dagger.io/docker"
"universe.dagger.io/docker/cli"
) )
dagger.#Plan & { dagger.#Plan & {
@ -13,7 +14,7 @@ dagger.#Plan & {
... ...
} }
load: docker.#Load & { load: cli.#Load & {
image: build.output image: build.output
host: client.filesystem."/var/run/docker.sock".read.contents host: client.filesystem."/var/run/docker.sock".read.contents
tag: "myimage" tag: "myimage"

View File

@ -3,6 +3,7 @@ package main
import ( import (
"dagger.io/dagger" "dagger.io/dagger"
"universe.dagger.io/docker" "universe.dagger.io/docker"
"universe.dagger.io/docker/cli"
) )
dagger.#Plan & { dagger.#Plan & {
@ -16,7 +17,7 @@ dagger.#Plan & {
... ...
} }
load: docker.#Load & { load: cli.#Load & {
image: build.output image: build.output
tag: "myimage:v2" tag: "myimage:v2"
host: "ssh://root@93.184.216.34" host: "ssh://root@93.184.216.34"

View File

@ -0,0 +1,15 @@
package main
import (
"dagger.io/dagger"
"universe.dagger.io/docker/cli"
)
dagger.#Plan & {
client: filesystem: "/var/run/docker.sock": read: contents: dagger.#Service
actions: run: cli.#Run & {
host: client.filesystem."/var/run/docker.sock".read.contents
command: name: "info"
}
}

View File

@ -0,0 +1,22 @@
package main
import (
"dagger.io/dagger"
"universe.dagger.io/docker/cli"
)
dagger.#Plan & {
client: filesystem: {
"/home/user/.ssh/id_rsa": read: contents: dagger.#Secret
"/home/user/.ssh/known_hosts": read: contents: dagger.#Secret
}
actions: run: cli.#Run & {
host: "ssh://root@93.184.216.34"
ssh: {
key: client.filesystem."/home/user/.ssh/id_rsa".read.contents
knownHosts: client.filesystem."/home/user/.ssh/known_hosts".read.contents
}
command: name: "info"
}
}

View File

@ -0,0 +1,20 @@
package main
import (
"dagger.io/dagger"
"universe.dagger.io/docker/cli"
)
dagger.#Plan & {
// Directory with certificates. Needs the following files:
// - ca.pem --> (Certificate authority that signed the registry certificate)
// - cert.pem --> (Client certificate)
// - key.pem --> (Client private key)
client: filesystem: "./certs": read: contents: dagger.#FS
actions: run: cli.#Run & {
host: "tcp://93.184.216.34:2376"
certs: client.filesystem."./certs".read.contents
command: name: "info"
}
}

View File

@ -1,47 +1,17 @@
package docker package cli
import ( import (
"dagger.io/dagger" "dagger.io/dagger"
"universe.dagger.io/docker"
) )
// Load an image into a docker daemon // See https://github.com/dagger/dagger/issues/1856
#Load: {
// Image to load
image: #Image
// Name and optionally a tag in the 'name:tag' format // Run a docker CLI command
tag: #Ref #Run: {
#RunSocket | #RunSSH | #RunTCP
// Exported image ID _image: docker.#Pull & {
imageID: _export.imageID
// Root filesystem with exported file
result: _export.output
_export: dagger.#Export & {
"tag": tag
input: image.rootfs
config: image.config
}
#_cli & {
mounts: src: {
dest: "/src"
contents: _export.output
}
command: {
name: "load"
flags: "-i": "/src/image.tar"
}
}
}
// FIXME: Move this into docker/client or
// create a better abstraction to reuse here.
#_cli: {
#_socketConn | #_sshConn | #_tcpConn
_image: #Pull & {
source: "docker:20.10.13-alpine3.15" source: "docker:20.10.13-alpine3.15"
} }
@ -49,10 +19,10 @@ import (
} }
// Connect via local docker socket // Connect via local docker socket
#_socketConn: { #RunSocket: {
host: dagger.#Service host: dagger.#Service
#Run & { docker.#Run & {
mounts: docker: { mounts: docker: {
dest: "/var/run/docker.sock" dest: "/var/run/docker.sock"
contents: host contents: host
@ -60,28 +30,8 @@ import (
} }
} }
// Connect via HTTP/HTTPS
#_tcpConn: {
host: =~"^tcp://.+"
#Run & {
env: DOCKER_HOST: host
// Directory with certificates to verify ({ca,cert,key}.pem files).
// This enables HTTPS.
certs?: dagger.#FS
if certs != _|_ {
mounts: "certs": {
dest: "/certs/client"
contents: certs
}
}
}
}
// Connect via SSH // Connect via SSH
#_sshConn: { #RunSSH: {
host: =~"^ssh://.+" host: =~"^ssh://.+"
ssh: { ssh: {
@ -90,9 +40,11 @@ import (
// Known hosts file contents // Known hosts file contents
knownHosts?: dagger.#Secret knownHosts?: dagger.#Secret
// FIXME: implement keyPassphrase
} }
#Run & { docker.#Run & {
env: DOCKER_HOST: host env: DOCKER_HOST: host
if ssh.key != _|_ { if ssh.key != _|_ {
@ -110,3 +62,23 @@ import (
} }
} }
} }
// Connect via HTTP/HTTPS
#RunTCP: {
host: =~"^tcp://.+"
docker.#Run & {
env: DOCKER_HOST: host
// Directory with certificates to verify ({ca,cert,key}.pem files).
// This enables HTTPS.
certs?: dagger.#FS
if certs != _|_ {
mounts: "certs": {
dest: "/certs/client"
contents: certs
}
}
}
}

View File

@ -0,0 +1,38 @@
package cli
import (
"dagger.io/dagger"
"universe.dagger.io/docker"
)
// Load an image into a docker daemon
#Load: {
// Image to load
image: docker.#Image
// Name and optionally a tag in the 'name:tag' format
tag: docker.#Ref
// Exported image ID
imageID: _export.imageID
// Root filesystem with exported file
result: _export.output
_export: dagger.#Export & {
"tag": tag
input: image.rootfs
config: image.config
}
#Run & {
mounts: src: {
dest: "/src"
contents: _export.output
}
command: {
name: "load"
flags: "-i": "/src/image.tar"
}
}
}

View File

@ -1,4 +1,4 @@
package docker package test
import ( import (
"dagger.io/dagger" "dagger.io/dagger"
@ -6,12 +6,13 @@ import (
"universe.dagger.io/alpine" "universe.dagger.io/alpine"
"universe.dagger.io/bash" "universe.dagger.io/bash"
"universe.dagger.io/docker" "universe.dagger.io/docker"
"universe.dagger.io/docker/cli"
) )
dagger.#Plan & { dagger.#Plan & {
client: filesystem: "/var/run/docker.sock": read: contents: dagger.#Service client: filesystem: "/var/run/docker.sock": read: contents: dagger.#Service
actions: test: load: { actions: test: {
_cli: alpine.#Build & { _cli: alpine.#Build & {
packages: { packages: {
bash: {} bash: {}
@ -27,7 +28,7 @@ dagger.#Plan & {
} }
} }
load: docker.#Load & { load: cli.#Load & {
image: _image.output image: _image.output
host: client.filesystem."/var/run/docker.sock".read.contents host: client.filesystem."/var/run/docker.sock".read.contents
tag: "dagger:load" tag: "dagger:load"
@ -51,7 +52,4 @@ dagger.#Plan & {
"""# """#
} }
} }
// FIXME: test remote connections with `docker:dind`
// image when we have long running tasks
} }

View File

@ -0,0 +1,39 @@
package test
import (
"dagger.io/dagger"
"universe.dagger.io/alpine"
"universe.dagger.io/docker/cli"
)
dagger.#Plan & {
client: filesystem: "/var/run/docker.sock": read: contents: dagger.#Service
actions: test: {
run: cli.#Run & {
host: client.filesystem."/var/run/docker.sock".read.contents
command: name: "info"
}
differentImage: {
_cli: alpine.#Build & {
packages: {
bash: {}
"docker-cli": {}
}
}
run: cli.#RunSocket & {
input: _cli.output
host: client.filesystem."/var/run/docker.sock".read.contents
command: {
name: "docker"
args: ["info"]
}
}
}
// FIXME: test remote connections with `docker:dind` image
// when we have long running tasks
}
}

View File

@ -0,0 +1,10 @@
setup() {
load '../../../bats_helpers'
common_setup
}
@test "docker/cli" {
dagger "do" -p ./run.cue test
dagger "do" -p ./load.cue test
}

View File

@ -9,5 +9,4 @@ setup() {
dagger "do" -p ./dockerfile.cue test dagger "do" -p ./dockerfile.cue test
dagger "do" -p ./run.cue test dagger "do" -p ./run.cue test
dagger "do" -p ./image.cue test dagger "do" -p ./image.cue test
dagger "do" -p ./load.cue test
} }