Skip to content

Instantly share code, notes, and snippets.

@docwhat
Last active October 24, 2023 17:08
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 docwhat/646f1977d4ed907629e0f35ef32febdd to your computer and use it in GitHub Desktop.
Save docwhat/646f1977d4ed907629e0f35ef32febdd to your computer and use it in GitHub Desktop.
Build a docker image with bash

Dockerfile as a bash script

A simple experiment.

By creating bash functions for common Dockerfile commands (e.g., RUN, COPY) you can sort of implement docker build as a bash script.

It's sort of like a cheap buildah knockoff.

It creates the image example:build.bash

Please feel free to ask questions or submit improvements.

#!/bin/bash
set -eu
cmd=
cname=bob
colour() {
tput bold
tput smso
tput setaf 2
printf " %8s " "$1"
shift
tput rmso
tput setaf 3
printf " $*"
tput sgr0
echo
}
###
### Library of Dockerfile like methods
###
FROM() {
colour FROM "$1"
docker container rm -f "$cname" >/dev/null 2>&1 || :
docker run -d -t --name="$cname" "$1" /bin/sh
}
RUN() {
colour RUN "$@"
docker exec -it "$cname" /bin/sh -c "$*"
}
CMD() {
colour CMD "$@"
cmd=$(python3 -c 'import json, sys; print((json.dumps(sys.argv[1:])))' "$@")
}
COMMIT() {
colour COMMIT "${1:-}"
local -a args=(commit)
if [[ -n $cmd ]]; then
args+=("--change=CMD ${cmd}")
fi
args+=("--author=Christian Holtje <https://docwhat.org>")
args+=("--message=build.bash")
args+=("$cname")
if [[ -n ${1:-} ]]; then
args+=("$1")
fi
docker "${args[@]}"
}
### The Dockerfile!
FROM ubuntu
CMD /bin/bash -l
RUN apt update -y
RUN apt install -y bash pigz
COMMIT
RUN id \> /root/my-id
RUN pigz -9 /root/my-id
COMMIT
RUN ls -al /bin
COMMIT example:build.bash
### Debugging info
colour SECTION "container"
docker container ls --filter=name="$cname"
# colour SECTION "image"
# docker image ls example:build.bash
colour SECTION "history"
docker image history example:build.bash
# colour SECTION "config"
# docker image inspect -f '{{ .Config | json }}' example:build.bash | jpp -c
# EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment