Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop building When I use Dockerfile build image?

I write some shell codes in my Dockerfile below to check some directories in container, if it doesn't have those folders I want to stop building.So what should I write in the Dockerfile to stop it?

FROM XXX
...
RUN if [ -d "/app/bin" -a  -d "/app/lib" -a -d "/app/conf" -a -d "/app/resource" -a -d "/app/log" -a -f "/app/bin/start.sh" ]; then mkdir -p /app/control/bin;  else SOME_CODES_TO_STOP_BUILDING; fi
...
like image 395
fighter Avatar asked Oct 21 '25 02:10

fighter


1 Answers

Any shell command that fails (returns an exit status other than 0) will stop the build. Some examples:

# Probably the most idiomatic shell scripting: if the test
# returns false, then the "&&" won't run mkdir, and the whole
# command returns false
RUN test -d /app/bin -a ... \
 && mkdir /app/control/bin

# "test" and "[" are the same thing
RUN [ -d /app/bin -a ... ] \
 && mkdir /app/control/bin

# This first step will just blow up the build if it fails
RUN [ -d /app/bin -a ... ]
# Then do the on-success command
RUN mkdir /app/control/bin

# Your original suggestion
# /bin/false does nothing but returns 1
RUN if [ -d /app/bin -a ... ]; \
    then mkdir /app/control/bin; \
    else false; \
    fi
like image 167
David Maze Avatar answered Oct 22 '25 16:10

David Maze



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!