Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Dockerfile alternatives for heredoc
#printf
RUN printf '#!/bin/bash\n\
echo hello world from line 1\n\
echo hello world from line 2'\
>> /tmp/hello
#echo
RUN echo -e '#!/bin/bash\n\
echo hello world from line 1\n\
echo hello world from line 2'\
>> /tmp/hello
@squarism

This comment has been minimized.

Copy link

@squarism squarism commented Aug 30, 2018

Thanks. Seems so simple in retrospect but found this via google while wondering how to do multiline strings in a Dockerfile I was making.

@weivall

This comment has been minimized.

Copy link

@weivall weivall commented Jan 15, 2019

thanks!

@adamelliotfields

This comment has been minimized.

Copy link

@adamelliotfields adamelliotfields commented Mar 8, 2019

I like wrapping it in braces like they do in the official MySQL image:

RUN { \
		echo mysql-community-server mysql-community-server/data-dir select ''; \
		echo mysql-community-server mysql-community-server/root-pass password ''; \
		echo mysql-community-server mysql-community-server/re-root-pass password ''; \
		echo mysql-community-server mysql-community-server/remove-test-db select false; \
	} | debconf-set-selections
@ringerc

This comment has been minimized.

Copy link

@ringerc ringerc commented Mar 26, 2019

@adamelliotfields those braces are shell syntax that groups a set of commands; they don't affect Docker's parsing at all. See https://www.gnu.org/software/bash/manual/html_node/Command-Grouping.html#Command-Grouping . What that's doing is aggregating the stdout of all the commands in the brace-group and passing it all to debconf-set-selections. Nothing to do with a here-doc.

If Docker would let you specify the shell, you could use bash's <<<$'blah' construct like

RUN cat >>somewhere <<<$'\
whatever\n\
I\n\
want\n'

but it'll choke with "redirection unexpected" because it'll try to run with /bin/sh which is dash on Debian/Ubuntu.

@ribx

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.