Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append text to a file using docker heredocs

I'm usually using docker heredocs when I want to create a file on the fly.

COPY <<EOF somescript
    #!/usr/bin/env bash
    echo "Hello"
EOF

This is ok for files that do not exist, but how can I use heredocs and append to an existing file.

Just using >> doesn't work

COPY <<EOF >> ${USERHOME}/.bashrc

The documentation states that you can use RUN <<EOF to execute multiple lines:

RUN <<EOF
echo "Hello" >> /hello
echo "World!" >> /hello
EOF

So I tried a hack and combined docker heredocks with unix heredocs

RUN <<EOF
cat <<EOC >> /home/lxo/.bashrc
  echo "TEST"
EOC
EOF

But it doesn't work

#31 0.388 /bin/bash: line 3: warning: here-document at line 1 delimited by end-of-file (wanted `EOC')
like image 733
René Link Avatar asked Sep 03 '25 17:09

René Link


1 Answers

I figured out that I can RUN cat. E.g.

RUN cat <<EOF >> ~/.bashrc
   echo "TEST"
EOF
like image 187
René Link Avatar answered Sep 05 '25 11:09

René Link