165

I want to write the following RUN command in the Dockerfile. But, docker is not preserving the new lines.

RUN echo "[repo] \
name            = YUM Repository \
baseurl         = https://example.com/packages/ \
enabled         = 1 \
gpgcheck        = 0" > /etc/yum.repos.d/Repo.repoxyz

I know that \ at the end of each line escapes the new line. But, is there any way that I can write multiple lines preserving the new line?

2
  • Are you using non *nix platform? Because this works fine for me on Linux.
    – user
    Commented Feb 19, 2016 at 20:50
  • @user, I was using Linux. Commented Jan 9, 2017 at 17:15

7 Answers 7

189

You can use what is called "ANSI-C quoting" with $'...'. It was originally a ksh93 feature but it is now available in bash, zsh, mksh, FreeBSD sh and in busybox's ash (but only when it is compiled with ENABLE_ASH_BASH_COMPAT).

As RUN uses /bin/sh as shell by default you are required to switch to something like bash first by using the SHELL instruction.

Start your command with $', end it with ' and use \n\ for newlines, like this:

SHELL ["/bin/bash", "-c"]

RUN echo $'[repo] \n\
name            = YUM Repository \n\
baseurl         = https://example.com/packages/ \n\
enabled         = 1 \n\
gpgcheck        = 0' > /etc/yum.repos.d/Repo.repoxyz
11
  • 4
    It is bash syntax. See this question for more info: stackoverflow.com/a/11966402/1395437 Commented Nov 2, 2015 at 18:25
  • 19
    Please, explain what you're doing, don't just drop an opaque solution Commented Sep 22, 2016 at 9:34
  • 35
    A word of caution: the $' ... \n\ technique depends on the shell that docker RUN uses being bash. On some systems (such as Ubuntu) the shell RUN uses is /bin/sh which is often a link to dash which is NOT bash and does not understand the $' syntax.
    – Anon
    Commented Aug 4, 2018 at 7:30
  • 3
    as @Anon said, this will NOT WORK if run anywhere else than bash. (doesn't work in Oh My Zsh either)
    – Rafa
    Commented Apr 24, 2019 at 10:03
  • 3
    @Anon well it does work in Alpine linux (currently I am on 3.10.2) which uses shell.
    – suchoss
    Commented Oct 15, 2019 at 7:35
102

Use printf to allow a single RUN command to output multiple lines of text, using \n to insert newlines.

Executing:

RUN printf 'example\ntext\nhere' >> example.txt

appends:

example
text
here

to the file example.txt

1
  • This worked for me! Echo had newline characters in the file as "\n" instead of an actual new line.
    – Deepthi
    Commented Feb 2, 2023 at 9:57
42

You can use:

RUN echo -e "\
[repo] \n\
name            = YUM Repository \n\
baseurl         = https://example.com/packages/ \n\
enabled         = 1 \n\
gpgcheck        = 0\
" > /etc/yum.repos.d/Repo.repoxyz

This way you will have a quick way to check what the file contents are. You just need to be aware that you need to end every line with \ and insert the \n when needed.

3
  • 4
    This requires echo -e to interpret \n Commented May 14, 2019 at 16:58
  • 8
    Using the -e option gave me an unexpected behaviour. The option is not interpreted as such but as part of the text to be printed. Commented Nov 3, 2021 at 8:18
  • Good solution. Interestingly, if the first line after RUN echo -e ... is *#!/bin/bash \n* it doesn't end up in the file. Fixed it by doing echo -e '#!/bin/bash \n` instead. And I'm using '` instead of " to avoid parameter expansion. Apologies for the formatting, not working as expected!
    – Nagev
    Commented Oct 18, 2022 at 16:16
30

I ended up using a combination of the examples listed here since the new line \n did not work with echo.

RUN printf 'example \n\
text \n\
here' >> example.txt

It produces the following, as expected:

example
text
here
2
  • 2
    How is your answer differs from CTodea's one? Commented Jul 14, 2020 at 17:37
  • 3
    @TheGodfather That's a multiline example. Better answers the original question too.
    – Sergey
    Commented Jul 19, 2020 at 8:29
25

As of Docker 18.09 and Dockerfile syntax 1.4, Dockerfiles support heredocs (ie what you're looking for here) natively!

  1. Enable BuildKit, eg by setting DOCKER_BUILDKIT=1.
  2. Add this line to the top of your Dockerfile:
    # syntax=docker/dockerfile:1.4
    
    or as recommended in the documentation
    # syntax=docker/dockerfile:1
    
  3. Rewrite your heredoc like so:
    COPY <<EOF /etc/yum.repos.d/Repo.repoxyz
    [repo]
    name            = YUM Repository
    baseurl         = https://example.com/packages/
    enabled         = 1
    gpgcheck        = 0
    EOF
    

You can also use this to run multiple bash commands in a single RUN block, etc. More details: Docker blog post, Dockerfile syntax docs.

3
  • 1
    In the blog of Docker, the line is # syntax=docker/dockerfile:1.3-labs.
    – Shuai
    Commented Mar 9, 2022 at 11:43
  • What is your example doing? Is it writing to a file named /etc/yum.repos.d/Repo.repoxyz ? I tried that syntax and it does not create a file
    – red888
    Commented Apr 5, 2022 at 17:09
  • @red888 hmm! Yes, that's what it's doing, same as OP's. Not sure why these steps aren't working for you, they are for me.
    – ryan
    Commented Apr 6, 2022 at 20:58
5

May be it's help you ( https://github.com/jen-soft/pydocker )

[ Dockerfile.py ]

from pydocker import DockerFile  # sudo pip install -U pydocker

d = DockerFile(base_img='debian:8.2', name='jen-soft/custom-debian:8.2')

d.RUN_bash_script('/opt/set_repo.sh', r'''
cat >/etc/apt/sources.list <<EOL
deb     http://security.debian.org/ jessie/updates main
deb-src http://security.debian.org/ jessie/updates main
EOL
apt-get clean && apt-get update
''')

d.EXPOSE = 80
d.WORKDIR = '/opt'
d.CMD = ["python", "--version"]

# d.generate_files()
d.build_img()

# sudo wget -qO- https://get.docker.com/ | sh

python Dockerfile.py
docker images
-5

You can execute RUN several times to complete your file:

RUN echo "[repo]" >> /etc/yum.repos.d/Repo.repoxyz
RUN echo "name            = YUM Repository" >> /etc/yum.repos.d/Repo.repoxyz
RUN echo "baseurl         = https://example.com/packages/" >> /etc/yum.repos.d/Repo.repoxyz
RUN echo "enabled         = 1" >> /etc/yum.repos.d/Repo.repoxyz
RUN echo "gpgcheck        = 0" >> /etc/yum.repos.d/Repo.repoxyz

This may not be the optimal solution because it creates a new layer for every RUN command. Still, every layer will be as big as the change you make, which in this case it's in the order of Bytes (first RUN layer should be 7-byte).

The benefit of this solution is that it will work with all shells.

2
  • 4
    Probably better to concat these commands with && for better caching purposes, reduced logging and faster Dockerfile build times
    – JohannesB
    Commented Sep 12, 2020 at 8:47
  • 2
    This solution adds too many layer to your image which will increase your build time.
    – Erfun
    Commented Mar 24, 2021 at 13:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.