Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between RUN and bash script in a dockerfile?

I've seen many dockerfiles include all build steps in a RUN statement, like:

RUN echo "Hello" &&
    cd /tmp &&
    mv a.txt b.txt &&
    ...
    and so on...

My question is: what's the benefits/drawbacks on replace these instructions by a single bash script that gives me highlight syntax, loop capabilities, etc? Something like:

COPY ./script.sh /tmp
RUN  bash /tmp/script.sh

and then

#!/bin/bash

echo "hello" ;
cd /tmp ;
mv a.txt b.txt ;
...    

Thanks!

like image 831
vbsessa Avatar asked Oct 18 '22 06:10

vbsessa


1 Answers

The primary difference is that when you COPY the bash script into the image it will be available for inspection in the running container, whereas the RUN command is a little more opaque. Putting your commands in a file like that is arguably more manageable for other reasons: changes in your VCS history will be a little more clear, and for longer or more complex scripts you will probably find it easier to format things cleanly with the script in a separate file rather than embedded in your Dockerfile in a RUN command.

Otherwise the result is the same (in both cases, you are executing the same set of commands), although the COPY and RUN will result in an extra image layer (vs. just the RUN by itself).

like image 192
larsks Avatar answered Oct 21 '22 09:10

larsks