Inside of my Dockerfiles I would like to COPY a file into my image if it exists, the requirements.txt file for pip seems like a good candidate but how would this be achieved?
COPY (requirements.txt if test -e requirements.txt; fi) /destination
...
RUN if test -e requirements.txt; then pip install -r requirements.txt; fi
or
if test -e requirements.txt; then
COPY requiements.txt /destination;
fi
RUN if test -e requirements.txt; then pip install -r requirements.txt; fi
Here is a simple workaround:
Make sure
foo
exists, sinceCOPY
needs at least one valid source.If
file-which-may-exist
is present, it will also be copied.NOTE: You should take care to ensure that your wildcard doesn't pick up other files which you don't intend to copy. To be more careful, you could use
file-which-may-exist?
instead (?
matches just a single character).Or even better, use a character class like this to ensure that only one file can be matched:
Maybe you just don't need! Let's see your code again:
Maybe you can just switch to:
Now you have this:
Work around Solution
I had requirement on copy FOLDER to server based on ENV Variables. I took the empty server image. created required deployment folder structure at in local folder. then added below line to DockerFile copy the folder to container. In last line added entry point to execute init file.sh before docker start the server.
Then create the custom-init.sh file in local with script something like below
In docker-compose file below lines.
environment: - BUILD_EVN=TEST
These changes copy folder to container during docker build. when we execute docker-compose up it copy or deploy the actual required folder to server before server starts.
This isn't currently supported (as I suspect it would lead to non-reproducible image, since the same Dockerfile would copy or not the file, depending on its existence).
This is still requested, in issue 13045, using wildcards: "
COPY foo/* bar/" not work if no file in foo
" (May 2015).It won't be implemented for now (July 2015) in Docker, but another build tool like bocker could support this.