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










30















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?










share|improve this question
























  • Are you using non *nix platform? Because this works fine for me on Linux.

    – user
    Feb 19 '16 at 20:50











  • @user, I was using Linux.

    – Venkata Jaswanth
    Jan 9 '17 at 17:15















30















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?










share|improve this question
























  • Are you using non *nix platform? Because this works fine for me on Linux.

    – user
    Feb 19 '16 at 20:50











  • @user, I was using Linux.

    – Venkata Jaswanth
    Jan 9 '17 at 17:15













30












30








30


10






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?










share|improve this question
















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?







docker dockerfile






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jun 27 '18 at 19:38









Benjamin W.

20.6k134856




20.6k134856










asked Oct 30 '15 at 15:13









Venkata JaswanthVenkata Jaswanth

186129




186129












  • Are you using non *nix platform? Because this works fine for me on Linux.

    – user
    Feb 19 '16 at 20:50











  • @user, I was using Linux.

    – Venkata Jaswanth
    Jan 9 '17 at 17:15

















  • Are you using non *nix platform? Because this works fine for me on Linux.

    – user
    Feb 19 '16 at 20:50











  • @user, I was using Linux.

    – Venkata Jaswanth
    Jan 9 '17 at 17:15
















Are you using non *nix platform? Because this works fine for me on Linux.

– user
Feb 19 '16 at 20:50





Are you using non *nix platform? Because this works fine for me on Linux.

– user
Feb 19 '16 at 20:50













@user, I was using Linux.

– Venkata Jaswanth
Jan 9 '17 at 17:15





@user, I was using Linux.

– Venkata Jaswanth
Jan 9 '17 at 17:15












3 Answers
3






active

oldest

votes


















54














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



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





share|improve this answer




















  • 1





    It is working! Thank you!

    – Venkata Jaswanth
    Oct 30 '15 at 15:45











  • Is the $ syntax after echo a feature of Dockerfile? Because, I couldn't find about it in the documentation.

    – Venkata Jaswanth
    Nov 2 '15 at 17:29






  • 4





    It is bash syntax. See this question for more info: stackoverflow.com/a/11966402/1395437

    – Daniel Zolnai
    Nov 2 '15 at 18:25






  • 7





    Please, explain what you're doing, don't just drop an opaque solution

    – Édouard Lopez
    Sep 22 '16 at 9:34






  • 3





    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
    Aug 4 '18 at 7:30



















20














I used printf. Writing all the text in one line using "n".



Executing:



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


inserts:



example
text
here


in example.txt






share|improve this answer























  • Actually a clever and cleaner solution IMO. Thank you.

    – David Tabernero M.
    Dec 4 '18 at 18:33


















0














You can use:



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


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.






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f33439230%2fhow-to-write-commands-with-multiple-lines-in-dockerfile-while-preserving-the-new%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    54














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



    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





    share|improve this answer




















    • 1





      It is working! Thank you!

      – Venkata Jaswanth
      Oct 30 '15 at 15:45











    • Is the $ syntax after echo a feature of Dockerfile? Because, I couldn't find about it in the documentation.

      – Venkata Jaswanth
      Nov 2 '15 at 17:29






    • 4





      It is bash syntax. See this question for more info: stackoverflow.com/a/11966402/1395437

      – Daniel Zolnai
      Nov 2 '15 at 18:25






    • 7





      Please, explain what you're doing, don't just drop an opaque solution

      – Édouard Lopez
      Sep 22 '16 at 9:34






    • 3





      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
      Aug 4 '18 at 7:30
















    54














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



    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





    share|improve this answer




















    • 1





      It is working! Thank you!

      – Venkata Jaswanth
      Oct 30 '15 at 15:45











    • Is the $ syntax after echo a feature of Dockerfile? Because, I couldn't find about it in the documentation.

      – Venkata Jaswanth
      Nov 2 '15 at 17:29






    • 4





      It is bash syntax. See this question for more info: stackoverflow.com/a/11966402/1395437

      – Daniel Zolnai
      Nov 2 '15 at 18:25






    • 7





      Please, explain what you're doing, don't just drop an opaque solution

      – Édouard Lopez
      Sep 22 '16 at 9:34






    • 3





      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
      Aug 4 '18 at 7:30














    54












    54








    54







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



    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





    share|improve this answer















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



    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






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 24 '17 at 16:26

























    answered Oct 30 '15 at 15:33









    Daniel ZolnaiDaniel Zolnai

    9,64034059




    9,64034059







    • 1





      It is working! Thank you!

      – Venkata Jaswanth
      Oct 30 '15 at 15:45











    • Is the $ syntax after echo a feature of Dockerfile? Because, I couldn't find about it in the documentation.

      – Venkata Jaswanth
      Nov 2 '15 at 17:29






    • 4





      It is bash syntax. See this question for more info: stackoverflow.com/a/11966402/1395437

      – Daniel Zolnai
      Nov 2 '15 at 18:25






    • 7





      Please, explain what you're doing, don't just drop an opaque solution

      – Édouard Lopez
      Sep 22 '16 at 9:34






    • 3





      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
      Aug 4 '18 at 7:30













    • 1





      It is working! Thank you!

      – Venkata Jaswanth
      Oct 30 '15 at 15:45











    • Is the $ syntax after echo a feature of Dockerfile? Because, I couldn't find about it in the documentation.

      – Venkata Jaswanth
      Nov 2 '15 at 17:29






    • 4





      It is bash syntax. See this question for more info: stackoverflow.com/a/11966402/1395437

      – Daniel Zolnai
      Nov 2 '15 at 18:25






    • 7





      Please, explain what you're doing, don't just drop an opaque solution

      – Édouard Lopez
      Sep 22 '16 at 9:34






    • 3





      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
      Aug 4 '18 at 7:30








    1




    1





    It is working! Thank you!

    – Venkata Jaswanth
    Oct 30 '15 at 15:45





    It is working! Thank you!

    – Venkata Jaswanth
    Oct 30 '15 at 15:45













    Is the $ syntax after echo a feature of Dockerfile? Because, I couldn't find about it in the documentation.

    – Venkata Jaswanth
    Nov 2 '15 at 17:29





    Is the $ syntax after echo a feature of Dockerfile? Because, I couldn't find about it in the documentation.

    – Venkata Jaswanth
    Nov 2 '15 at 17:29




    4




    4





    It is bash syntax. See this question for more info: stackoverflow.com/a/11966402/1395437

    – Daniel Zolnai
    Nov 2 '15 at 18:25





    It is bash syntax. See this question for more info: stackoverflow.com/a/11966402/1395437

    – Daniel Zolnai
    Nov 2 '15 at 18:25




    7




    7





    Please, explain what you're doing, don't just drop an opaque solution

    – Édouard Lopez
    Sep 22 '16 at 9:34





    Please, explain what you're doing, don't just drop an opaque solution

    – Édouard Lopez
    Sep 22 '16 at 9:34




    3




    3





    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
    Aug 4 '18 at 7:30






    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
    Aug 4 '18 at 7:30














    20














    I used printf. Writing all the text in one line using "n".



    Executing:



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


    inserts:



    example
    text
    here


    in example.txt






    share|improve this answer























    • Actually a clever and cleaner solution IMO. Thank you.

      – David Tabernero M.
      Dec 4 '18 at 18:33















    20














    I used printf. Writing all the text in one line using "n".



    Executing:



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


    inserts:



    example
    text
    here


    in example.txt






    share|improve this answer























    • Actually a clever and cleaner solution IMO. Thank you.

      – David Tabernero M.
      Dec 4 '18 at 18:33













    20












    20








    20







    I used printf. Writing all the text in one line using "n".



    Executing:



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


    inserts:



    example
    text
    here


    in example.txt






    share|improve this answer













    I used printf. Writing all the text in one line using "n".



    Executing:



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


    inserts:



    example
    text
    here


    in example.txt







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Apr 19 '16 at 14:12









    CTodeaCTodea

    459417




    459417












    • Actually a clever and cleaner solution IMO. Thank you.

      – David Tabernero M.
      Dec 4 '18 at 18:33

















    • Actually a clever and cleaner solution IMO. Thank you.

      – David Tabernero M.
      Dec 4 '18 at 18:33
















    Actually a clever and cleaner solution IMO. Thank you.

    – David Tabernero M.
    Dec 4 '18 at 18:33





    Actually a clever and cleaner solution IMO. Thank you.

    – David Tabernero M.
    Dec 4 '18 at 18:33











    0














    You can use:



    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


    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.






    share|improve this answer



























      0














      You can use:



      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


      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.






      share|improve this answer

























        0












        0








        0







        You can use:



        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


        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.






        share|improve this answer













        You can use:



        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


        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 7 at 12:43









        Paulo FidalgoPaulo Fidalgo

        16k66695




        16k66695



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f33439230%2fhow-to-write-commands-with-multiple-lines-in-dockerfile-while-preserving-the-new%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            Museum of Modern and Contemporary Art of Trento and Rovereto

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully