dockerdocker-build

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')

Solution

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

    RUN cat <<EOF >> ~/.bashrc
       echo "TEST"
    EOF