This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
Files
dagger/stdlib/docker/docker.cue
Andrea Luzzardi 5a1d4bff62 Support loading artifacts into a Docker Engine
This adds support to loading artifacts (e.g. docker.#Build,
os.#Container, ...) into any arbitrary docker engine (through a
dagger.#Stream for UNIX sockets or SSH for a remote engine)

Implementation:
- Add op.#SaveImage which serializes an artifact into an arbitrary path
  (docker tarball format)
- Add docker.#Load which uses op.#SaveImage to serialize to disk and
  executes `docker load` to load it back

Caveats: Because we're doing this in userspace rather than letting
dagger itself load the image, the performance is pretty bad.

The buildkit API is meant for streaming (get a stream of a docker image
pipe it into docker load). Because of userspace, we have to load the
entire docker image into memory, then serialize it in a single WriteFile
LLB operation.

Example:

```cue
package main

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

source: dagger.#Input & dagger.#Artifact

dockersocket: dagger.#Input & dagger.#Stream

build: docker.#Build & {
	"source": source
}

load: docker.#Load & {
	source: build
	tag:    "testimage"
	socket: dockersocket
}
```

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
2021-10-12 14:16:01 -07:00

262 lines
4.1 KiB
CUE

// Docker container operations
package docker
import (
"strings"
"alpha.dagger.io/dagger"
"alpha.dagger.io/dagger/op"
)
// Build a Docker image from source
#Build: {
// Build context
source: dagger.#Input & {dagger.#Artifact}
// Dockerfile passed as a string
dockerfile: dagger.#Input & {*null | string}
args?: [string]: string | dagger.#Secret
#up: [
op.#DockerBuild & {
context: source
if dockerfile != null {
"dockerfile": dockerfile
}
if args != _|_ {
buildArg: args
}
},
]
}
// Pull a docker container
#Pull: {
// Remote ref (example: "index.docker.io/alpine:latest")
from: dagger.#Input & {string}
#up: [
op.#FetchContainer & {ref: from},
]
}
// Push a docker image to a remote registry
#Push: {
// Remote target (example: "index.docker.io/alpine:latest")
target: dagger.#Input & {string}
// Image source
source: dagger.#Input & {dagger.#Artifact}
// Registry auth
auth?: {
// Username
username: dagger.#Input & {string}
// Password or secret
secret: dagger.#Input & {dagger.#Secret | string}
}
push: #up: [
op.#Load & {from: source},
if auth != _|_ {
op.#DockerLogin & {
"target": target
username: auth.username
secret: auth.secret
}
},
op.#PushContainer & {ref: target},
op.#Subdir & {dir: "/dagger"},
]
// Image ref
ref: {
string
#up: [
op.#Load & {from: push},
op.#Export & {
source: "/image_ref"
},
]
} & dagger.#Output
// Image digest
digest: {
string
#up: [
op.#Load & {from: push},
op.#Export & {
source: "/image_digest"
},
]
} & dagger.#Output
}
// Load a docker image into a docker engine
#Load: {
// Connect to a remote SSH server
ssh?: {
// ssh host
host: dagger.#Input & {string}
// ssh user
user: dagger.#Input & {string}
// ssh port
port: dagger.#Input & {*22 | int}
// private key
key: dagger.#Input & {dagger.#Secret}
// fingerprint
fingerprint?: dagger.#Input & {string}
// ssh key passphrase
keyPassphrase?: dagger.#Input & {dagger.#Secret}
}
// Mount local docker socket
socket?: dagger.#Stream & dagger.#Input
// Name and optionally a tag in the 'name:tag' format
tag: dagger.#Input & {string}
// Image source
source: dagger.#Input & {dagger.#Artifact}
save: #up: [
op.#Load & {from: source},
op.#SaveImage & {
"tag": tag
dest: "/image.tar"
},
]
load: #Command & {
if ssh != _|_ {
"ssh": ssh
}
if socket != _|_ {
"socket": socket
}
copy: "/src": from: save
command: "docker load -i /src/image.tar"
}
// Image ref
ref: {
string
#up: [
op.#Load & {from: save},
op.#Export & {
source: "/dagger/image_ref"
},
]
} & dagger.#Output
// Image digest
digest: {
string
#up: [
op.#Load & {from: save},
op.#Export & {
source: "/dagger/image_digest"
},
]
} & dagger.#Output
}
#Run: {
// Connect to a remote SSH server
ssh?: {
// ssh host
host: dagger.#Input & {string}
// ssh user
user: dagger.#Input & {string}
// ssh port
port: dagger.#Input & {*22 | int}
// private key
key: dagger.#Input & {dagger.#Secret}
// fingerprint
fingerprint?: dagger.#Input & {string}
// ssh key passphrase
keyPassphrase?: dagger.#Input & {dagger.#Secret}
}
// Mount local docker socket
socket?: dagger.#Stream & dagger.#Input
// Image reference (e.g: nginx:alpine)
ref: dagger.#Input & {string}
// Container name
name?: dagger.#Input & {string}
// Image registry
registry?: {
target: string
username: string
secret: dagger.#Secret
} & dagger.#Input
// local ports
ports?: [...string]
#command: #"""
# Run detach container
OPTS=""
if [ ! -z "$CONTAINER_NAME" ]; then
OPTS="$OPTS --name $CONTAINER_NAME"
fi
if [ ! -z "$CONTAINER_PORTS" ]; then
OPTS="$OPTS -p $CONTAINER_PORTS"
fi
docker container run -d $OPTS "$IMAGE_REF"
"""#
run: #Command & {
if ssh != _|_ {
"ssh": ssh
}
if socket != _|_ {
"socket": socket
}
command: #command
env: {
IMAGE_REF: ref
if name != _|_ {
CONTAINER_NAME: name
}
if ports != _|_ {
CONTAINER_PORTS: strings.Join(ports, " -p ")
}
}
}
}