Inserting a conditional RUN statement inside a dockerfile

lobengula3rd picture lobengula3rd · May 10, 2015 · Viewed 25.3k times · Source

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.

Answer

jwodder picture jwodder · May 11, 2015

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 ^