Dancrumb
Dancrumb

Reputation: 27539

Is there any reason to favour concatenated RUN directives over RUNning a script?

When I'm creating a Dockerfile to generate an image, I have some options when it comes to installing and building stuff.

I could do

RUN a && \
    b && \ 
    c

Or

COPY install.sh /install.sh
RUN /install.sh

Where install.sh is

a
b
c

Are there any substantial reasons to favour one approach over the other?

Upvotes: 3

Views: 36

Answers (2)

Adrian Mouat
Adrian Mouat

Reputation: 46480

In contrast to the other answer, I generally prefer:

RUN a && \
    b && \ 
    c

The main reason being that it is immediately clear what is happening. If you instead use a script, you've effectively hidden the code. For a new user to understand what's happening, they now need to find the project with the build context before they can look into your script.

It is a trade-off and once things get too complex, you should refactor into a script. However, you might prefer to curl the script from a known location rather than COPY it, so that the Dockerfile remains standalone.

Upvotes: 2

spectre007
spectre007

Reputation: 1539

RUN a && b && C
vs
RUN install.sh

From Docker perspective both the approaches are same.

However the 2nd approach (running a single script and wrapping everything under the hood) is more cleaner. It allows to have better handle on managing a,b,c and it dependencies on each other. You also update install.sh without updating Dockerfile, keeping Dockerfile simple.

Upvotes: 0

Related Questions