# Treating Dockerfiles as shell scripts

DevFeed: [Treating Dockerfiles as shell scripts](<https://devfeed.tech/articles/treating-dockerfiles-as-shell-scripts-20980.md>)

Original publisher: [Read original article](<https://jakewharton.com/treating-dockerfiles-as-shell-scripts/>)

Published: 2020-12-03T00:00:00Z

Content type: tutorial

Language: en

Sources: [Jake Wharton](<https://devfeed.tech/sources/jake-wharton.md>)

Topics: [Dockerfile](<https://devfeed.tech/topics/dockerfile.md>), [Docker](<https://devfeed.tech/topics/docker.md>), [Containers](<https://devfeed.tech/topics/containers.md>), [Scripting, bash](<https://devfeed.tech/topics/scripting-bash.md>), [Command-line interface](<https://devfeed.tech/topics/cli.md>), [Tool](<https://devfeed.tech/topics/tool.md>)

Tags: [bash](<https://devfeed.tech/tags/bash.md>), [chmod](<https://devfeed.tech/tags/chmod.md>), [containers](<https://devfeed.tech/tags/containers.md>), [docker](<https://devfeed.tech/tags/docker.md>), [dockerfiles](<https://devfeed.tech/tags/dockerfiles.md>), [implementation](<https://devfeed.tech/tags/implementation.md>), [tools](<https://devfeed.tech/tags/tools.md>), [usability](<https://devfeed.tech/tags/usability.md>)

## AI overview

The article presents a utility that lets executable Dockerfiles behave like shell scripts. A shebang invokes a helper that conditionally runs docker build and then docker run, improving the edit-and-run workflow for containerized tools.

## Source excerpt

I use Docker to run a lot of tools. With the tools all wrapped up in containers, my computers are free of Python and Go and the various other dependencies needed for their use. While this is a nice win for isolation and reproducibility, the user experience is sub-par. To run a tool, I just use docker run: $ docker run --rm tool arguments... But the editing workflow is something like: $ nano tool.dockerfile # hack hack hack... $ docker build -t tool - < tool.dockerfile $ docker run --rm tool arguments... I also use a lot of bash scripts with easy-to-remember names. To run a script I just type its name: ./update_containers.sh. To edit, I open it in an editor, save, and then run. The user experience of this is top-notch! Can we combine the two? Executable Dockerfiles If the first line of an executable starts with #!, unix-y systems will treat what follows on that line as an executable for interpreting the rest of the file. This is called the shebang, and the bash scripts I use start with one: #!/usr/bin/env bash. In order to do this with a Dockerfile, though, we need a program which will conditionally run docker build and then docker run the resulting image. Thankfully docker build is already conditional and won't rebuild anything unless necessary, so we can always run it. #!/usr/bin/env bash NAME=$(basename "$1") docker build -t "$NAME" - < "$1" > /dev/null shift # Remove script name from arguments docker run --rm --name "$NAME" "$NAME" "$@" With this saved as dockerfile-shebang.sh, we can add it as the shebang in a Dockerfile. #!/path/to/dockerfile-shebang.sh FROM alpine:latest ENTRYPOINT ["echo"] Saving this as echo.dockerfile and running chmod +x echo.dockerfile provides the user experience we're after: $ ./echo.dockerfile Hello, world! Hello, world! It's Dockerfile-Shebang! I have wrapped up this utility into an executable, dockerfile-shabang. You can find it at github.com/JakeWharton/dockerfile-shebang. The implementation is a bit more complicated than above for