3

I have a Dockerfile like this:

FROM ubuntu:16.04
:
:
RUN git clone <url> 
:

Sometimes, the repo that I want to clone is too large.

So, before running this git clone command, I would like to check whether the repo that I want to clone, exists in the current directory( directory of dockerfile ) and if it does, then I would do a COPY <repo-name> . instruction instead of the RUN git clone <url> instruction.

I want to achieve something like this:

FROM ubuntu:16.04
:
:
if exists <repo-name>
    COPY <repo-name>
else
    RUN git clone <url> 
:

Is it even possible? If not, are there any tricks that I can do to achieve this?

scipsycho
  • 527
  • 2
  • 4
  • 13
  • 2
    You probably shouldn’t run `git clone` in your Dockerfile. It plays badly with layer caching (you are likely to get an old version of the repository on a rebuild) and you need to be very careful to not leak the credentials you need to access git. Run it on the host, assume the directory is always present, and unconditionally run the `COPY`. – David Maze Oct 22 '18 at 15:23

1 Answers1

2

If-Else isn't a thing in Dockerfiles. You would be better off using a one-liner bash command in your RUN that provided If-Else:

RUN if git clone <repo>; then echo "Exists"; else echo "Error"; fi

Or if the behavior you're after requires more scripting, put it in a script and run it that way to keep all the clutter out of your Dockerfile:

COPY git-script.sh /usr/local/bin
RUN chmod +x /usr/local/bin/git-script.sh && \
    bash git-script.sh

Found a similar answer here as well.

bluescores
  • 4,437
  • 1
  • 20
  • 34
  • Yeah, I would be better off to use the script thing and if possible, as recommended above, would remove `git clone` command entirely from the dockerfile. Thanks for your help! – scipsycho Oct 22 '18 at 15:39