pcsram pcsram - 1 year ago 106
Bash Question

Writing data to file in Dockerfile

I have a shell script, script.sh, that writes some lines to a file:

#!/usr/bin/env bash
printf "blah
blah\n" | sudo tee file.txt

Now in my Dockerfile, I add this script and run it, then attempt to add the generated

ADD script.sh .
RUN chmod 755 script.sh && ./script.sh
ADD file.txt .

When I do the above, I just get an error referring to the
ADD file.txt .

lstat file.txt: no such file or directory

Why can't docker locate the file that my shell script generates?
Where would I be able to find it?

Answer Source

When you RUN chmod 755 script.sh && ./script.sh it actually execute this script inside the docker container (ie: in the docker layer).

When you ADD file.txt . you try to add a file on your local filesystem inside the docker container (ie: in a new docker layer).

You cant do that because the file.txt doesn't exist on your computer.

In fact you already have this file inside docker, try docker run --rm -ti mydockerimage cat file.txt and you should see it's content displayed

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download