31

I am trying to write a docker file which will run a RUN command to search for a word in a package.json file and act upon it:

this is my simple dockerfile:

FROM ubuntu:14.04

COPY package.json package.json

RUN if grep -q "grunt" package.json; then echo succeed fi

as you can see i just want a simple if statement but it get this error:

Step 2 : RUN if grep -q "grunt" package.json; then echo succeed fi
 ---> Running in af460df45239
/bin/sh: 1: Syntax error: end of file unexpected (expecting "fi")
INFO[0001] The command [/bin/sh -c if grep -q "grunt" package.json; then echo succeed fi] returned a non-zero code: 2

How should i run my command? thanks.

jwodder
  • 54,758
  • 12
  • 108
  • 124
lobengula3rd
  • 1,821
  • 4
  • 26
  • 39

1 Answers1

49

Just like when typing at the shell, you need either a newline or a semicolon before fi:

RUN if grep -q "grunt" package.json; then echo succeed; fi
                                             add this ^
jwodder
  • 54,758
  • 12
  • 108
  • 124
  • 13
    Note that it will create a layer no matter what though. – Vincent Demeester May 11 '15 at 05:49
  • 3
    Note also that this will only propagate the exit code of the last statement in the “then” block. This isn’t an issue if there’s only one statement (as in the above example), but if there are multiple statements, any failure in a statement other than the last one will NOT cause the image build to fail. – wjv Jun 16 '17 at 10:59