Skip to content

Instantly share code, notes, and snippets.

@zsteinkamp
Last active February 21, 2026 11:39
Show Gist options
  • Select an option

  • Save zsteinkamp/7235cebd34166a12ef0ba3bb1bae3758 to your computer and use it in GitHub Desktop.

Select an option

Save zsteinkamp/7235cebd34166a12ef0ba3bb1bae3758 to your computer and use it in GitHub Desktop.
A single bash script that bootstraps a container, then runs itself inside that container. Use any "FROM" line. The only requirement is that /bin/bash is present. See also [Turdokken](https://github.com/zsteinkamp/turdokken).
#!/bin/bash
# Shell code to run in the container
cmd () {
echo "Hello, ${OUTER_USER}. I am $(whoami)."
cat /etc/issue
}
# Dockerfile definition
read -r -d '' DOCKERFILE <<EOD
FROM alpine:3.12
RUN apk add --no-cache bash
###### lines below are required ######
ENV IN_CONTAINER=true
ENTRYPOINT ["/runme"]
EOD
# error out
set -e
# Logic to build/run the container or run the script
if [[ -z $IN_CONTAINER ]]; then
COMMAND_FILE="$0"
if [[ "$COMMAND_FILE" != /* ]]; then
# Add the current dir to COMMAND_FILE if we only have the relative path
COMMAND_FILE="$PWD/$COMMAND_FILE"
fi
COMMAND_FILE_BASE=$(basename "$COMMAND_FILE")
TAG="dockerscript-$(basename ${COMMAND_FILE_BASE//[^A-Za-z0-9-_]/})";
echo "Building container..." 1>&2
docker build --quiet --tag $TAG - <<< "${DOCKERFILE}" 1>&2
echo "Running container..." 1>&2
echo 1>&2
exec docker run --rm -it -e OUTER_USER=${USER} --mount type=bind,source="${COMMAND_FILE}",target=/runme $TAG $@
else
cmd ## run the script
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment