if else with dockerfile instructions

Viewed 404

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?

1 Answers

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.

Related