4

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.

Cerin
  • 60,957
  • 96
  • 316
  • 522
  • Take a look at [this](https://stackoverflow.com/questions/30716937/dockerfile-build-possible-to-ignore-error/30717108) answer, I think it's what you are looking for. – Federico Moya Nov 01 '19 at 19:58
  • Some `apt` errors are in fact Warning messages that still return the exit code as `0`. Take a look at [this](https://unix.stackexchange.com/q/175146/218771) question, it gives many options to catch these warnings and manipulate the exit code base on them. – Eduardo Baitello Nov 01 '19 at 20:02
  • 1
    @FedericoMoya Isn't that the opposite of what I'm asking? I want Docker to stop on an error and it isn't. That question's saying they don't want Docker to stop on an error but it is. – Cerin Nov 01 '19 at 20:08
  • Any time the command executed by `RUN` returns a non-zero exit code, the build will stop. Make sure you chain your commands with `&&` rather than `;`. – BMitch Nov 02 '19 at 15:52

1 Answers1

2

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

Vrangz
  • 285
  • 3
  • 13