Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you do cancel a dockerfile image building on the first error it encounters?

If I have an error in a RUN command in my dockerfile it just carries on to the next one.

like image 900
Richard Avatar asked Oct 25 '25 12:10

Richard


1 Answers

Are you sure the command is really returning an error? The following Dockerfile doesn't get to the echo foo:

FROM alpine
RUN false
RUN echo foo

It just gets:

# docker build .
Sending build context to Docker daemon 3.072 kB
Step 0 : FROM alpine
 ---> 0a3b5ba3277d
Step 1 : RUN false
 ---> Running in 22485c5e763c
The command '/bin/sh -c false' returned a non-zero code: 1

To check whether your command is really failing, you could try something like this:

FROM alpine
RUN false || echo failed
RUN echo foo

which then gets me:

# docker build .
Sending build context to Docker daemon 3.072 kB
Step 0 : FROM alpine
 ---> 0a3b5ba3277d
Step 1 : RUN false || echo failed
 ---> Running in 674f09ae7530
failed
 ---> 232fd66c5729
Removing intermediate container 674f09ae7530
Step 2 : RUN echo foo
 ---> Running in c7b541fdb15c
foo
 ---> dd1bece67e71
Removing intermediate container c7b541fdb15c
Successfully built dd1bece67e71
like image 50
boyvinall Avatar answered Oct 28 '25 02:10

boyvinall