Skip to content

Instantly share code, notes, and snippets.

@jrussellsmyth
Created July 12, 2025 21:16
Show Gist options
  • Select an option

  • Save jrussellsmyth/79a98f3474066edea1ac3ee3c91101cf to your computer and use it in GitHub Desktop.

Select an option

Save jrussellsmyth/79a98f3474066edea1ac3ee3c91101cf to your computer and use it in GitHub Desktop.
Dockerfile HERE document

Self Contained Dockerfiles with addtitional file needs

Sometimes you need to create a docker container that has additional files included. One way to do this is to have the files available in the docker contex (typicallly the file you are running the docker command in) and using the COPY command.

This however requires multiple files and management of the context when building the image. This can be a problematic requirement.

For small text files, such as configuration documents or small shell scripts used for setup or execution, another possible soluton is a the use of a "Here Document" in conjunction with the RUN command to include the content of the file within the Dockerfile itself

FROM busybox

RUN <<EOF > /usr/local/bin/myscript.sh
#!/bin/sh
echo "Hello from an inline script!"
EOF

# Make the script executable
RUN chmod +x /usr/local/bin/myscript.sh

CMD ["myscript.sh"]

In this example:

  • <<EOF tells the RUN command to treat the following lines as standard input until it encounters the EOF delimiter.

  • > /usr/local/bin/myscript.sh redirects that input and writes it to the specified file inside the container.

  • RUN chmod +x ... is a separate step to make the newly created script executable.

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