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:
-
<<EOFtells theRUNcommand to treat the following lines as standard input until it encounters the EOF delimiter. -
> /usr/local/bin/myscript.shredirects 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.