1

I'm curious on how to write to a file with docker commands etc..

Let's say i want to build a file like so:

RUN touch newfile.php

Now how do i write to that file after it's built in the dockerfile succession?

The text doesn't need to be placed anywhere special. I'm just not sure how using docker commands you would open nano or whatever to write to the file i just touched.

  • Need more clarity: Did you mean you have a dockerfile, wherein first you're creating a file using 'RUN touch filename'. Now in subsequent commands, you wish to add some content to it? It'll also be great if you can tell what kind of content you'll add. Will it be just new appends at the end, or you require replacement of text in middle of the file? – BhaveshDiwan May 31 '19 at 18:59
  • Yes, i would like to add some content to it.. like how do i docker command to open nano or vim or something and then write to to it @BhaveshDiwan – yesyesyes May 31 '19 at 19:57
  • did the answer below helped you? – BhaveshDiwan Jun 23 '19 at 18:42

1 Answers1

1

One possible way would be to put an echo statement under RUN command in your dockerfile. With this, you can echo the data into that file. The following is the sample from what i've been using in my projects.

RUN \
    echo $'\n\
        Hello World.\n\
        I am inserted in this file during docker image creationg.\n\
        The back-slash-n charachters give me a new line in created file.\n\
    ' > /path/to/newfile.php

PS: Consider upvoting and accepting this as an answer if it helped you. This will help others to follow, as well as encourage one answering.

BhaveshDiwan
  • 11,516