Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop docker when run command returns error code?

Tags:

docker

How do you tell Docker to stop executing a Dockefile when one of its RUN commands returns an error code?

I have a Dockerfile like:

FROM ubuntu:18.04
RUN apt install -yq `cat requirements.txt | tr "\\n" " "`
RUN ./some_other_setup.sh
RUN ./my_tests.sh

and occasionally the apt install will fail if there's a brief network outage. However, the Docker will continue executing the other RUN commands, even though they too will fail because they depend on the apt install succeeding.

like image 566
Cerin Avatar asked Sep 18 '25 20:09

Cerin


1 Answers

If you want to catch error and stop you can do like linux script

RUN set -e && apt install -yq `cat requirements.txt | tr "\\n" " "`

so set -e will catch all errors and stop if any happened.

OR

you can prevent failing apt install by adding RUN apt-get install -f which will install missing dependencies.

Also if apt-get/apt install has any errors you can do

apt-get install [your stuff] || true

which will always pass even with errors and then you do apt-get install -f so you always have all dependencies needed

like image 200
Vrangz Avatar answered Sep 20 '25 11:09

Vrangz