Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if else with dockerfile instructions

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?

like image 476
scipsycho Avatar asked Apr 20 '26 10:04

scipsycho


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.

like image 56
bluescores Avatar answered Apr 22 '26 04:04

bluescores



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!