如何在Dockerfile中使用循环?

发布于 2025-02-12 00:10:36 字数 903 浏览 0 评论 0原文

我有一个dockerfile,其4倍相同的说明,只有文件名更改:

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

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

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

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

是否可以使用for loop,以复制 and run 仅一次?类似:

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

谢谢! :)

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

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

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

COPY ./MyProject.Widgets/MyProject.Widgets.csproj /app/MyProject.Widgets/
RUN dotnet restore /app/MyProject.Widgets/MyProject.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 [MyProject.Common, MyProject.Manual, ${CLIENT_PROJECT}]
  COPY ./${FOR_PARAM}/${FOR_PARAM}.csproj /app/${FOR_PARAM}/
  RUN dotnet restore /app/${FOR_PARAM}/${FOR_PARAM}.csproj
ENDFOR

Thank you ! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风无影 2025-02-19 00:10:36

docker文件中没有for/loop指令,因此我建议您:

  1. 提供一个复制指令,该指令将所有排列为不同的子文件夹中的文件复制。
  2. 写入和复制一个外壳脚本,该脚本通过子文件夹编译它们。
  3. 为上述脚本提供单个运行指令。

问候。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文