Home > Software engineering >  How to use for loop in Dockerfile?
How to use for loop in Dockerfile?

Time:06-30

I have a Dockerfile with 4 times the same instructions, only the file name changes:

COPY ./DynaplayUp.Common/DynaplayUp.Common.csproj /app/DynaplayUp.Common/
RUN dotnet restore /app/DynaplayUp.Common/DynaplayUp.Common.csproj

COPY ./DynaplayUp.Manual/DynaplayUp.Manual.csproj /app/DynaplayUp.Manual/
RUN dotnet restore /app/DynaplayUp.Manual/DynaplayUp.Manual.csproj

COPY ./DynaplayUp.Widgets/DynaplayUp.Widgets.csproj /app/DynaplayUp.Widgets/
RUN dotnet restore /app/DynaplayUp.Widgets/DynaplayUp.Widgets.csproj

COPY ./${CLIENT_PROJECT}/${CLIENT_PROJECT}.csproj /app/${CLIENT_PROJECT}/
RUN dotnet restore /app/${CLIENT_PROJECT}/${CLIENT_PROJECT}.csproj

Is it possible to use a for loop, to have COPY and RUN only once ? something like:

FOR [DynaplayUp.Common, DynaplayUp.Manual, ${CLIENT_PROJECT}]
  COPY ./${FOR_PARAM}/${FOR_PARAM}.csproj /app/${FOR_PARAM}/
  RUN dotnet restore /app/${FOR_PARAM}/${FOR_PARAM}.csproj
ENDFOR

Thank you ! :)

CodePudding user response:

There are no FOR/LOOP instructions in docker file, so I suggest you to:

  1. Provide a single COPY instruction which copies all the files arranged into distinct subfolders.
  2. Write and COPY a shell script which loops through subfolder compiling them.
  3. Provide a single RUN instruction for the above script.

Regards.

  • Related