2021-06-15 10:46:56 +02:00
|
|
|
// Docker container operations
|
2021-04-06 23:20:21 +02:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"dagger.io/dagger"
|
|
|
|
"dagger.io/dagger/op"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Build a Docker image from source, using included Dockerfile
|
2021-05-01 09:14:36 +02:00
|
|
|
#Build: {
|
2021-05-26 12:23:44 +02:00
|
|
|
source: dagger.#Artifact @dagger(input)
|
2021-04-06 23:20:21 +02:00
|
|
|
|
|
|
|
#up: [
|
|
|
|
op.#DockerBuild & {
|
|
|
|
context: source
|
|
|
|
},
|
|
|
|
]
|
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-05-26 12:23:44 +02:00
|
|
|
from: string @dagger(input)
|
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-05-13 18:52:22 +02:00
|
|
|
// Push a docker image
|
|
|
|
#Push: {
|
|
|
|
// Remote ref (example: "index.docker.io/alpine:latest")
|
2021-05-26 12:23:44 +02:00
|
|
|
ref: string @dagger(input)
|
2021-05-13 18:52:22 +02:00
|
|
|
|
|
|
|
// Image
|
2021-05-26 12:23:44 +02:00
|
|
|
source: dagger.#Artifact @dagger(input)
|
2021-05-13 18:52:22 +02:00
|
|
|
|
|
|
|
#up: [
|
|
|
|
op.#Load & {from: source},
|
|
|
|
op.#PushContainer & {"ref": ref},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2021-05-20 19:25:44 +02:00
|
|
|
#Run: {
|
2021-06-05 01:57:31 +02:00
|
|
|
// Connect to a remote SSH server
|
2021-06-07 22:29:42 +02:00
|
|
|
ssh: {
|
2021-06-05 01:57:31 +02:00
|
|
|
// ssh host
|
|
|
|
host: string @dagger(input)
|
2021-05-20 19:25:44 +02:00
|
|
|
|
2021-06-05 01:57:31 +02:00
|
|
|
// ssh user
|
|
|
|
user: string @dagger(input)
|
2021-05-20 19:25:44 +02:00
|
|
|
|
2021-06-05 01:57:31 +02:00
|
|
|
// ssh port
|
|
|
|
port: *22 | int @dagger(input)
|
2021-05-20 19:25:44 +02:00
|
|
|
|
2021-06-05 01:57:31 +02:00
|
|
|
// private key
|
|
|
|
key: dagger.#Secret @dagger(input)
|
2021-05-21 17:18:30 +02:00
|
|
|
|
2021-06-05 01:57:31 +02:00
|
|
|
// fingerprint
|
|
|
|
fingerprint?: string @dagger(input)
|
2021-05-20 19:25:44 +02:00
|
|
|
|
2021-06-05 01:57:31 +02:00
|
|
|
// ssh key passphrase
|
|
|
|
keyPassphrase?: dagger.#Secret @dagger(input)
|
|
|
|
}
|
2021-05-20 19:25:44 +02:00
|
|
|
|
|
|
|
// Image reference (e.g: nginx:alpine)
|
2021-05-21 17:18:30 +02:00
|
|
|
ref: string @dagger(input)
|
2021-05-20 19:25:44 +02:00
|
|
|
|
|
|
|
// Container name
|
2021-05-21 17:18:30 +02:00
|
|
|
name?: string @dagger(input)
|
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-05-21 17:18:30 +02:00
|
|
|
} @dagger(input)
|
2021-05-20 19:25:44 +02:00
|
|
|
|
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-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 & {
|
|
|
|
"ssh": ssh
|
|
|
|
command: #command
|
|
|
|
env: {
|
|
|
|
IMAGE_REF: ref
|
|
|
|
if name != _|_ {
|
|
|
|
CONTAINER_NAME: name
|
2021-05-20 19:25:44 +02:00
|
|
|
}
|
2021-06-11 19:01:33 +02:00
|
|
|
}
|
|
|
|
}
|
2021-05-20 19:25:44 +02:00
|
|
|
}
|
2021-05-06 08:53:04 +02:00
|
|
|
|
2021-04-06 23:20:21 +02:00
|
|
|
// Build a Docker image from the provided Dockerfile contents
|
2021-05-06 08:53:04 +02:00
|
|
|
// FIXME: incorporate into #Build
|
2021-04-06 23:20:21 +02:00
|
|
|
#ImageFromDockerfile: {
|
2021-06-15 10:46:56 +02:00
|
|
|
// Dockerfile passed as a string
|
2021-06-15 10:47:39 +02:00
|
|
|
dockerfile: string @dagger(input)
|
2021-06-15 10:46:56 +02:00
|
|
|
|
|
|
|
// Build context
|
2021-06-15 10:47:39 +02:00
|
|
|
context: dagger.#Artifact @dagger(input)
|
2021-04-06 23:20:21 +02:00
|
|
|
|
|
|
|
#up: [
|
|
|
|
op.#DockerBuild & {
|
|
|
|
"context": context
|
|
|
|
"dockerfile": dockerfile
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|