Inserting a conditional RUN statement inside a doc

2019-02-21 10:02发布

问题:

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.

回答1:

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 ^