2021-06-15 10:46:56 +02:00
|
|
|
// Docker container operations
|
2021-04-06 23:20:21 +02:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2021-09-28 00:55:01 +02:00
|
|
|
"strings"
|
2021-06-23 16:31:42 +02:00
|
|
|
"alpha.dagger.io/dagger"
|
|
|
|
"alpha.dagger.io/dagger/op"
|
2021-04-06 23:20:21 +02:00
|
|
|
)
|
|
|
|
|
2021-09-14 09:54:09 +02:00
|
|
|
// Build a Docker image from source
|
2021-05-01 09:14:36 +02:00
|
|
|
#Build: {
|
2021-09-14 09:54:09 +02:00
|
|
|
// Build context
|
2021-08-31 13:05:22 +02:00
|
|
|
source: dagger.#Input & {dagger.#Artifact}
|
2021-04-06 23:20:21 +02:00
|
|
|
|
2021-09-14 09:54:09 +02:00
|
|
|
// Dockerfile passed as a string
|
|
|
|
dockerfile: dagger.#Input & {*null | string}
|
|
|
|
|
2021-09-15 16:32:43 +02:00
|
|
|
args?: [string]: string | dagger.#Secret
|
2021-09-13 14:30:21 +02:00
|
|
|
|
2021-04-06 23:20:21 +02:00
|
|
|
#up: [
|
|
|
|
op.#DockerBuild & {
|
|
|
|
context: source
|
2021-09-14 09:54:09 +02:00
|
|
|
if dockerfile != null {
|
|
|
|
"dockerfile": dockerfile
|
|
|
|
}
|
2021-09-13 14:30:21 +02:00
|
|
|
if args != _|_ {
|
|
|
|
buildArg: args
|
|
|
|
}
|
2021-04-06 23:20:21 +02:00
|
|
|
},
|
|
|
|
]
|
2021-05-01 09:14:36 +02:00
|
|
|
|
2021-04-06 23:20:21 +02:00
|
|
|
}
|
|
|
|
|
2021-05-06 08:53:04 +02:00
|
|
|
// Pull a docker container
|
|
|
|
#Pull: {
|
|
|
|
// Remote ref (example: "index.docker.io/alpine:latest")
|
2021-08-31 13:05:22 +02:00
|
|
|
from: dagger.#Input & {string}
|
2021-04-06 23:20:21 +02:00
|
|
|
|
|
|
|
#up: [
|
2021-05-06 08:53:04 +02:00
|
|
|
op.#FetchContainer & {ref: from},
|
2021-04-06 23:20:21 +02:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2021-06-12 17:11:41 +02:00
|
|
|
// Push a docker image to a remote registry
|
2021-05-13 18:52:22 +02:00
|
|
|
#Push: {
|
2021-06-28 14:48:49 +02:00
|
|
|
// Remote target (example: "index.docker.io/alpine:latest")
|
2021-08-31 13:05:22 +02:00
|
|
|
target: dagger.#Input & {string}
|
2021-05-13 18:52:22 +02:00
|
|
|
|
2021-06-12 17:11:27 +02:00
|
|
|
// Image source
|
2021-08-31 13:05:22 +02:00
|
|
|
source: dagger.#Input & {dagger.#Artifact}
|
2021-05-13 18:52:22 +02:00
|
|
|
|
2021-06-18 22:01:16 +02:00
|
|
|
// Registry auth
|
2021-07-02 01:20:20 +02:00
|
|
|
auth?: {
|
2021-06-12 17:11:27 +02:00
|
|
|
// Username
|
2021-08-31 13:05:22 +02:00
|
|
|
username: dagger.#Input & {string}
|
2021-06-12 17:11:27 +02:00
|
|
|
|
|
|
|
// Password or secret
|
2021-08-31 13:05:22 +02:00
|
|
|
secret: dagger.#Input & {dagger.#Secret | string}
|
2021-06-12 17:11:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
push: #up: [
|
|
|
|
op.#Load & {from: source},
|
|
|
|
|
2021-06-18 22:01:16 +02:00
|
|
|
if auth != _|_ {
|
2021-06-12 17:11:27 +02:00
|
|
|
op.#DockerLogin & {
|
2021-06-28 14:48:49 +02:00
|
|
|
"target": target
|
2021-06-18 22:01:16 +02:00
|
|
|
username: auth.username
|
|
|
|
secret: auth.secret
|
2021-06-12 17:11:27 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-06-28 14:48:49 +02:00
|
|
|
op.#PushContainer & {ref: target},
|
2021-06-18 22:01:16 +02:00
|
|
|
|
|
|
|
op.#Subdir & {dir: "/dagger"},
|
2021-05-13 18:52:22 +02:00
|
|
|
]
|
2021-06-12 17:11:27 +02:00
|
|
|
|
2021-06-12 17:11:41 +02:00
|
|
|
// Image ref
|
2021-06-24 11:42:34 +02:00
|
|
|
ref: {
|
|
|
|
string
|
|
|
|
|
|
|
|
#up: [
|
|
|
|
op.#Load & {from: push},
|
|
|
|
|
|
|
|
op.#Export & {
|
|
|
|
source: "/image_ref"
|
|
|
|
},
|
|
|
|
]
|
2021-08-31 13:05:22 +02:00
|
|
|
} & dagger.#Output
|
2021-06-12 17:11:41 +02:00
|
|
|
|
|
|
|
// Image digest
|
2021-06-24 11:42:34 +02:00
|
|
|
digest: {
|
|
|
|
string
|
2021-06-12 17:11:41 +02:00
|
|
|
|
2021-06-24 11:42:34 +02:00
|
|
|
#up: [
|
|
|
|
op.#Load & {from: push},
|
2021-06-12 17:11:41 +02:00
|
|
|
|
2021-06-24 11:42:34 +02:00
|
|
|
op.#Export & {
|
|
|
|
source: "/image_digest"
|
|
|
|
},
|
|
|
|
]
|
2021-08-31 13:05:22 +02:00
|
|
|
} & dagger.#Output
|
2021-05-13 18:52:22 +02:00
|
|
|
}
|
|
|
|
|
2021-10-12 22:42:17 +02:00
|
|
|
// 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"
|
|
|
|
}
|
|
|
|
|
2021-10-13 00:37:01 +02:00
|
|
|
// Image ID
|
|
|
|
id: {
|
2021-10-12 22:42:17 +02:00
|
|
|
string
|
|
|
|
|
|
|
|
#up: [
|
|
|
|
op.#Load & {from: save},
|
|
|
|
|
|
|
|
op.#Export & {
|
2021-10-13 00:37:01 +02:00
|
|
|
source: "/dagger/image_id"
|
2021-10-12 22:42:17 +02:00
|
|
|
},
|
|
|
|
]
|
|
|
|
} & dagger.#Output
|
|
|
|
}
|
|
|
|
|
2021-05-20 19:25:44 +02:00
|
|
|
#Run: {
|
2021-06-05 01:57:31 +02:00
|
|
|
// Connect to a remote SSH server
|
2021-09-22 02:19:00 +02:00
|
|
|
ssh?: {
|
2021-06-05 01:57:31 +02:00
|
|
|
// ssh host
|
2021-08-31 13:05:22 +02:00
|
|
|
host: dagger.#Input & {string}
|
2021-05-20 19:25:44 +02:00
|
|
|
|
2021-06-05 01:57:31 +02:00
|
|
|
// ssh user
|
2021-08-31 13:05:22 +02:00
|
|
|
user: dagger.#Input & {string}
|
2021-05-20 19:25:44 +02:00
|
|
|
|
2021-06-05 01:57:31 +02:00
|
|
|
// ssh port
|
2021-08-31 13:05:22 +02:00
|
|
|
port: dagger.#Input & {*22 | int}
|
2021-05-20 19:25:44 +02:00
|
|
|
|
2021-06-05 01:57:31 +02:00
|
|
|
// private key
|
2021-08-31 13:05:22 +02:00
|
|
|
key: dagger.#Input & {dagger.#Secret}
|
2021-05-21 17:18:30 +02:00
|
|
|
|
2021-06-05 01:57:31 +02:00
|
|
|
// fingerprint
|
2021-08-31 13:05:22 +02:00
|
|
|
fingerprint?: dagger.#Input & {string}
|
2021-05-20 19:25:44 +02:00
|
|
|
|
2021-06-05 01:57:31 +02:00
|
|
|
// ssh key passphrase
|
2021-08-31 13:05:22 +02:00
|
|
|
keyPassphrase?: dagger.#Input & {dagger.#Secret}
|
2021-06-05 01:57:31 +02:00
|
|
|
}
|
2021-05-20 19:25:44 +02:00
|
|
|
|
2021-09-22 02:19:00 +02:00
|
|
|
// Mount local docker socket
|
|
|
|
socket?: dagger.#Stream & dagger.#Input
|
|
|
|
|
2021-05-20 19:25:44 +02:00
|
|
|
// Image reference (e.g: nginx:alpine)
|
2021-08-31 13:05:22 +02:00
|
|
|
ref: dagger.#Input & {string}
|
2021-05-20 19:25:44 +02:00
|
|
|
|
|
|
|
// Container name
|
2021-08-31 13:05:22 +02:00
|
|
|
name?: dagger.#Input & {string}
|
2021-05-20 19:25:44 +02:00
|
|
|
|
|
|
|
// Image registry
|
|
|
|
registry?: {
|
2021-05-20 19:26:24 +02:00
|
|
|
target: string
|
2021-05-20 19:25:44 +02:00
|
|
|
username: string
|
|
|
|
secret: dagger.#Secret
|
2021-08-31 13:05:22 +02:00
|
|
|
} & dagger.#Input
|
2021-05-20 19:25:44 +02:00
|
|
|
|
2021-09-28 00:55:01 +02:00
|
|
|
// local ports
|
|
|
|
ports?: [...string]
|
|
|
|
|
2021-06-11 19:01:33 +02:00
|
|
|
#command: #"""
|
2021-06-05 01:57:31 +02:00
|
|
|
# Run detach container
|
|
|
|
OPTS=""
|
2021-05-20 19:25:44 +02:00
|
|
|
|
2021-06-05 01:57:31 +02:00
|
|
|
if [ ! -z "$CONTAINER_NAME" ]; then
|
|
|
|
OPTS="$OPTS --name $CONTAINER_NAME"
|
|
|
|
fi
|
2021-05-20 19:25:44 +02:00
|
|
|
|
2021-09-28 00:55:01 +02:00
|
|
|
if [ ! -z "$CONTAINER_PORTS" ]; then
|
|
|
|
OPTS="$OPTS -p $CONTAINER_PORTS"
|
|
|
|
fi
|
|
|
|
|
2021-06-05 01:57:31 +02:00
|
|
|
docker container run -d $OPTS "$IMAGE_REF"
|
|
|
|
"""#
|
2021-05-20 19:25:44 +02:00
|
|
|
|
2021-06-11 19:01:33 +02:00
|
|
|
run: #Command & {
|
2021-09-22 02:19:00 +02:00
|
|
|
if ssh != _|_ {
|
|
|
|
"ssh": ssh
|
|
|
|
}
|
|
|
|
if socket != _|_ {
|
|
|
|
"socket": socket
|
|
|
|
}
|
|
|
|
|
2021-06-11 19:01:33 +02:00
|
|
|
command: #command
|
|
|
|
env: {
|
|
|
|
IMAGE_REF: ref
|
|
|
|
if name != _|_ {
|
|
|
|
CONTAINER_NAME: name
|
2021-05-20 19:25:44 +02:00
|
|
|
}
|
2021-09-28 00:55:01 +02:00
|
|
|
|
|
|
|
if ports != _|_ {
|
2021-09-28 04:40:55 +02:00
|
|
|
CONTAINER_PORTS: strings.Join(ports, " -p ")
|
2021-09-28 00:55:01 +02:00
|
|
|
}
|
2021-06-11 19:01:33 +02:00
|
|
|
}
|
|
|
|
}
|
2021-05-20 19:25:44 +02:00
|
|
|
}
|