How to write commands with multiple lines in Dockerfile while preserving the new lines?

后端 未结 6 1188
情深已故
情深已故 2020-12-07 14:51

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

RUN echo \"[repo] \\
name            = YUM Reposito         


        
6条回答
  •  我在风中等你
    2020-12-07 15:25

    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
    

提交回复
热议问题