Docker Community Forums

Share and learn in the Docker community.

Put build commands in a script, rather than the Dockerfile


(Aogriffiths) #1

Hi

Dockerfiles often become crowded, with RUN statements followed by many lines, joined together with a mixture of ampersands, semicolons and backslashes. (For example, see the last RUN statement in this php Dockerfile).

Why not just copy a build script in and run it (as in Example 2 below) rather than what most people do by putting everything in the Dockerfile (as in Example 1 below)?

I realise it leaves the script as a file in the image, but this seems like a small price to pay for more readable Dockerfiles and easily editable scripts. (Also, the examples below are simplified to illustrate the point, but in the real world the Dockerfiles are much more complex.)

Thanks!
Adam

Example 1:

Dockerfile:

FROM debian:stretch
RUN  apt-get update  \
     && apt-get install -y \
        curl \
        wget \
     && rm -rf /var/lib/apt/lists/*

Example 2:

Dockerfile:

FROM debian:stretch
COPY install-script.sh .
RUN ./install-script.sh 

install-script.sh:

apt-get update
packagelist=(
  curl
  wget
)
apt-get install -y ${packagelist[@]}
rm -rf /var/lib/apt/lists/*