Issue
I want to output the following text in a Dockerfile:
*****first row *****
*****second row *****
One way is to do it like that:
cat > Dockerfile <<EOF
FROM alpine:latest
RUN echo ' *****first row ***** ' >> /home/myfile
RUN echo ' *****second row ***** ' >> /home/myfile
ENTRYPOINT cat /home/myfile; sh;
WORKDIR /home
EOF
But if I have 100 lines it takes time because it runs each command separately and make it as a layer.
Other way is like that:
FROM alpine:latest
RUN printf ' *****first row ***** \n *****second row ***** \n' >> /home/myfile
ENTRYPOINT cat /home/myfile; sh;
WORKDIR /home
but I don't like it because it make it less readable, especially when you have 100 lines.
I wonder is there a way to do something like that:
FROM alpine:latest
RUN echo ' *****first row *****
*****second row ***** ' >> /home/myfile
ENTRYPOINT cat /home/myfile; sh;
WORKDIR /home
Or is there a way to use the ARG
command to do it ?
Solution
There is another question similar to this with a solution: How to write commands with multiple lines in Dockerfile while preserving the new lines?
The answer to this question is more particular in how to use multiline strings in bash rather than how to use Docker.
Following this solution you may accomplish what you want to do as shown below:
RUN echo $' \n\
*****first row ***** \n\
*****second row ***** \n\
*****third row ***** ' >> /home/myfile
More info about this leading dollar sign here: How does the leading dollar sign affect single quotes in Bash?
Note that this syntax relies on the run command using /bin/bash, not /bin/sh.
Answered By - sirlanceoflompoc Answer Checked By - Mary Flores (WPSolving Volunteer)