stdlib: improved Docker package

Signed-off-by: Solomon Hykes <sh.github.6811@hykes.org>
This commit is contained in:
Solomon Hykes
2021-04-06 21:20:21 +00:00
parent d522fc3396
commit 647e4c898b
7 changed files with 49 additions and 122 deletions

126
stdlib/docker/container.cue Normal file
View File

@@ -0,0 +1,126 @@
// docker: build and run Docker containers
// https://docker.com
package docker
import (
"strings"
"dagger.io/dagger"
"dagger.io/dagger/op"
)
// Run a Docker container
#Container: {
// Container image
image: dagger.#Artifact
// Independently cacheable setup commands
setup: [...string]
// Command to execute
command: string
// Environment variables shared by all commands
env: [string]: string
// Directory in which the command is executed
dir: string | *"/"
// Directory to expose as the output.
// By default the root filesystem is the output.
outputDir: string | *"/"
// If true, the command is never cached.
// (false by default).
always: true | *false
// External volumes. There are 4 types:
//
// 1. "mount": mount any artifact.
// Changes are not included in the final output.
//
// 2. "copy": copy any artifact.
// Changes are included in the final output.
//
// 3. "tmpfs": create a temporary directory.
//
// 4. "cache": create a persistent cache diretory.
//
volume: [name=string]: {
// Destination path
dest: string | *"/"
*{
type: "mount"
from: dagger.#Artifact
source: string | *"/"
} | {
type: "copy"
from: dagger.#Artifact
source: string | *"/"
} | {
type: "tmpfs" | "cache"
}
}
// Configure the shell which executes all commands.
shell: {
// Path of the shell to execute
path: string | *"/bin/sh"
// Arguments to pass to the shell prior to the command
args: [...string] | *["-c"]
// Map of directories to search for commands
// In POSIX shells this is used to generate the $PATH
// environment variable.
search: [string]: bool
search: {
"/sbin": true
"/bin": true
"/usr/sbin": true
"/usr/bin": true
"/usr/local/sbin": true
"/usr/local/bin": true
}
}
env: PATH: string | *strings.Join([ for p, v in shell.search if v {p}], ":")
#up: [
op.#Load & {from: image},
// Copy volumes with type=copy
for _, v in volume if v.type == "copy" {
op.#Copy & {
from: v.from
dest: v.dest
src: v.source
}
},
// Execute setup commands, then main command
for cmd in setup + [command] {
op.#Exec & {
args: [shell.path] + shell.args + [cmd]
"env": env
"dir": dir
"always": always
mount: {
for _, v in volume if v.type == "cache" {
"\(v.dest)": "cache"
}
for _, v in volume if v.type == "tmpfs" {
"\(v.dest)": "tmpfs"
}
for _, v in volume if v.type == "mount" {
"\(v.dest)": {
from: v.from
path: v.source
}
}
}
}
},
op.#Subdir & {
dir: outputDir
},
]
}

View File

@@ -1,39 +0,0 @@
package docker
import (
"dagger.io/dagger"
"dagger.io/dagger/op"
)
#Image: dagger.#Artifact
#Ref: string
// Build a docker container image
#Build: {
source: dagger.#Artifact
image: #up: [
op.#DockerBuild & {context: source},
]
}
#Run: {
args: [...string]
// image may be a remote image ref, or a computed artifact
{
image: #Ref
out: #up: [
op.#FetchContainer & {ref: image},
op.#Exec & {"args": args},
]
} | {
image: _
out: #up: [
op.#Load & {from: image},
op.#Exec & {"args": args},
]
}
}

41
stdlib/docker/image.cue Normal file
View File

@@ -0,0 +1,41 @@
package docker
import (
"dagger.io/dagger"
"dagger.io/dagger/op"
)
// Build a Docker image from source, using included Dockerfile
#ImageFromSource: {
source: dagger.#Artifact
#up: [
op.#DockerBuild & {
context: source
},
]
}
// Fetch an image from a remote registry
#ImageFromRegistry: {
ref: string
#up: [
op.#FetchContainer & {
"ref": ref
},
]
}
// Build a Docker image from the provided Dockerfile contents
#ImageFromDockerfile: {
dockerfile: string
context: dagger.#Artifact
#up: [
op.#DockerBuild & {
"context": context
"dockerfile": dockerfile
},
]
}