2021-04-06 22:52:06 +02:00
|
|
|
// docker: build and run Docker containers
|
|
|
|
// https://docker.com
|
2021-04-06 23:20:21 +02:00
|
|
|
|
|
|
|
package docker
|
2021-04-03 02:51:56 +02:00
|
|
|
|
|
|
|
import (
|
2021-04-06 03:37:31 +02:00
|
|
|
"strings"
|
|
|
|
|
2021-04-03 02:51:56 +02:00
|
|
|
"dagger.io/dagger"
|
2021-04-06 02:27:51 +02:00
|
|
|
"dagger.io/dagger/op"
|
2021-04-03 02:51:56 +02:00
|
|
|
)
|
|
|
|
|
2021-04-06 22:52:06 +02:00
|
|
|
// Run a Docker container
|
2021-04-03 02:51:56 +02:00
|
|
|
#Container: {
|
|
|
|
|
2021-04-06 22:52:06 +02:00
|
|
|
// Container image
|
2021-04-03 02:51:56 +02:00
|
|
|
image: dagger.#Artifact
|
|
|
|
|
2021-04-06 22:52:06 +02:00
|
|
|
// Independently cacheable setup commands
|
2021-04-06 03:37:31 +02:00
|
|
|
setup: [...string]
|
2021-04-03 02:51:56 +02:00
|
|
|
|
2021-04-06 22:52:06 +02:00
|
|
|
// Command to execute
|
|
|
|
command: string
|
|
|
|
|
2021-04-03 02:51:56 +02:00
|
|
|
// Environment variables shared by all commands
|
|
|
|
env: [string]: string
|
|
|
|
|
2021-04-06 22:52:06 +02:00
|
|
|
// 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.
|
|
|
|
//
|
2021-04-03 02:51:56 +02:00
|
|
|
volume: [name=string]: {
|
2021-04-06 22:52:06 +02:00
|
|
|
// Destination path
|
2021-04-03 02:51:56 +02:00
|
|
|
dest: string | *"/"
|
2021-04-06 22:52:06 +02:00
|
|
|
|
2021-04-03 02:51:56 +02:00
|
|
|
*{
|
|
|
|
type: "mount"
|
|
|
|
from: dagger.#Artifact
|
|
|
|
source: string | *"/"
|
|
|
|
} | {
|
|
|
|
type: "copy"
|
|
|
|
from: dagger.#Artifact
|
|
|
|
source: string | *"/"
|
|
|
|
} | {
|
|
|
|
type: "tmpfs" | "cache"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-06 22:52:06 +02:00
|
|
|
// Configure the shell which executes all commands.
|
2021-04-06 03:37:31 +02:00
|
|
|
shell: {
|
2021-04-06 22:52:06 +02:00
|
|
|
// Path of the shell to execute
|
2021-04-06 03:37:31 +02:00
|
|
|
path: string | *"/bin/sh"
|
2021-04-06 22:52:06 +02:00
|
|
|
// Arguments to pass to the shell prior to the command
|
2021-04-06 03:37:31 +02:00
|
|
|
args: [...string] | *["-c"]
|
2021-04-06 22:52:06 +02:00
|
|
|
// Map of directories to search for commands
|
|
|
|
// In POSIX shells this is used to generate the $PATH
|
|
|
|
// environment variable.
|
2021-04-06 03:37:31 +02:00
|
|
|
search: [string]: bool
|
|
|
|
search: {
|
|
|
|
"/sbin": true
|
|
|
|
"/bin": true
|
|
|
|
"/usr/sbin": true
|
|
|
|
"/usr/bin": true
|
|
|
|
"/usr/local/sbin": true
|
|
|
|
"/usr/local/bin": true
|
2021-04-03 02:51:56 +02:00
|
|
|
}
|
2021-04-06 03:37:31 +02:00
|
|
|
}
|
|
|
|
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] {
|
2021-04-06 02:27:51 +02:00
|
|
|
op.#Exec & {
|
2021-04-06 03:37:31 +02:00
|
|
|
args: [shell.path] + shell.args + [cmd]
|
2021-04-03 02:51:56 +02:00
|
|
|
"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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-06 03:37:31 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
op.#Subdir & {
|
|
|
|
dir: outputDir
|
|
|
|
},
|
|
|
|
]
|
2021-04-03 02:51:56 +02:00
|
|
|
}
|