Skip to content

Instantly share code, notes, and snippets.

@jamcole
Last active November 7, 2023 20:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamcole/170ea6b8f40120784a240fb9ba1282da to your computer and use it in GitHub Desktop.
Save jamcole/170ea6b8f40120784a240fb9ba1282da to your computer and use it in GitHub Desktop.
Example Taskfile Task using Docker or Podman Containers to Run Commands ( https://taskfile.dev/ )
version: '3'
tasks:
container:
internal: true
run: always
requires:
vars:
- RUN_CMD
- IMAGE
- ARGS
vars:
UID:
sh: id -u
GID:
sh: id -g
cmds:
- silent: true
cmd: >
{{.RUN_CMD}} -it --rm
{{.RUN_ARGUMENTS}}
--user {{.UID}}:{{.GID}}
--volume="/etc/group:/etc/group:ro"
--volume="/etc/passwd:/etc/passwd:ro"
--volume=".:/mnt/workspace:Z"
-w /mnt/workspace
'{{.IMAGE}}'
{{.ARGS}}
podman:
desc: Run a podman container with arguments
requires:
vars:
- IMAGE
- ARGS
cmds:
- task: container
vars:
RUN_CMD: "podman run --userns=keep-id"
RUN_ARGUMENTS: "{{.RUN_ARGUMENTS}}"
IMAGE: "{{.IMAGE}}"
ARGS: "{{.ARGS}}"
docker:
desc: Run a docker container with arguments
requires:
vars:
- IMAGE
- ARGS
cmds:
- task: container
vars:
RUN_CMD: "docker run --userns=keep-id"
RUN_ARGUMENTS: "{{.RUN_ARGUMENTS}}"
IMAGE: "{{.IMAGE}}"
ARGS: "{{.ARGS}}"
podman-sh:
desc: Run a command in a podman container
requires:
vars:
- IMAGE
- SH
cmds:
- task: podman
vars:
RUN_ARGUMENTS: "--entrypoint ''"
IMAGE: "{{.IMAGE}}"
ARGS: sh -c '{{.SH}}'
docker-sh:
desc: Run a command in a docker container
requires:
vars:
- IMAGE
- SH
cmds:
- task: docker
vars:
RUN_ARGUMENTS: "--entrypoint ''"
IMAGE: "{{.IMAGE}}"
ARGS: sh -c '{{.SH}}'
FROM docker.io/ealen/echo-server:latest
USER 65534
version: '3'
includes:
container: _Taskfile.ContainerRun.yaml
tasks:
busybox-run:
desc: Run a command in a busybox container
run: always
requires:
vars:
- SH
cmds:
- task: container:podman-sh
vars:
SH: '{{.SH}}'
IMAGE: docker.io/library/busybox:latest
task1:
desc: Just a test task 1
cmds:
- task: busybox-run
vars:
SH: |-
set -x
cat /etc/*-release
echo hello 1 from $HOSTNAME
pwd; ls -la
id
task2:
desc: Just a test task 2
cmds:
- task: container:podman-sh
vars:
IMAGE: docker.io/library/alpine:latest
SH: |-
set -x
cat /etc/*-release
echo hello 2 from $HOSTNAME
pwd; ls -la
id
build-cache:
desc: Build a base image cache
cmds:
- task: container:podman
vars:
IMAGE: gcr.io/kaniko-project/warmer:latest
ARGS: --cache-dir=./build/cache --dockerfile=./Dockerfile
build:
desc: Build a custom image
deps:
- build-cache
cmds:
- task: container:podman
vars:
IMAGE: gcr.io/kaniko-project/executor:latest
ARGS: |-
--cache-dir=./build/cache --context=dir://. --no-push --tar-path=./build/image.tar --dockerfile=./Dockerfile
ls -la
@jamcole
Copy link
Author

jamcole commented Nov 7, 2023

Run with: task test-task1 test-task2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment