Dockerfile中特定平台的RUN语句

huangapple go评论100阅读模式
英文:

Platform-specific RUN statements in Dockerfile

问题

Here's the translated Dockerfile section with the conditional RUN statement:

  1. ARG BASE_IMAGE=mcr.microsoft.com/dotnet/aspnet:7.0-bullseye-slim-arm64v8
  2. FROM ${BASE_IMAGE} AS base
  3. WORKDIR /app
  4. FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
  5. WORKDIR /src
  6. COPY ["src/ScreenshotCreator.Api/ScreenshotCreator.Api.csproj", "src/ScreenshotCreator.Api/"]
  7. COPY ["src/ScreenshotCreator.Logic/ScreenshotCreator.Logic.csproj", "src/ScreenshotCreator.Logic/"]
  8. RUN dotnet restore "src/ScreenshotCreator.Api/ScreenshotCreator.Api.csproj"
  9. COPY . .
  10. WORKDIR "/src/src/ScreenshotCreator.Api"
  11. RUN dotnet build "ScreenshotCreator.Api.csproj" -c Release -o /app/build
  12. FROM build AS publish
  13. RUN dotnet publish "ScreenshotCreator.Api.csproj" -c Release -o /app/publish /p:UseAppHost=false
  14. FROM base AS final
  15. WORKDIR /app
  16. COPY --from=publish /app/publish .
  17. # Conditional RUN statement
  18. RUN if [ "${PLATFORM}" = "arm" ]; then \
  19. apt-get update \
  20. && apt-get install -y wget libssl1.1 libunwind8 \
  21. && mkdir -p /opt/microsoft/powershell/7 \
  22. && wget -O /tmp/powershell.tar.gz https://github.com/PowerShell/PowerShell/releases/download/v7.2.6/powershell-7.2.6-linux-arm64.tar.gz \
  23. && tar zxf /tmp/powershell.tar.gz -C /opt/microsoft/powershell/7 \
  24. && chmod +x /opt/microsoft/powershell/7/pwsh \
  25. && ln -s /opt/microsoft/powershell/7/pwsh /usr/bin/pwsh \
  26. && rm /tmp/powershell.tar.gz \
  27. && pwsh playwright.ps1 install chromium \
  28. && pwsh playwright.ps1 install-deps chromium; \
  29. else \
  30. apt-get update \
  31. && apt-get install -y wget apt-transport-https software-properties-common \
  32. && wget -q https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/packages-microsoft-prod.deb \
  33. && dpkg -i packages-microsoft-prod.deb \
  34. && rm packages-microsoft-prod.deb \
  35. && apt-get update \
  36. && apt-get install -y powershell \
  37. && pwsh playwright.ps1 install chromium \
  38. && pwsh playwright.ps1 install-deps chromium; \
  39. fi
  40. ENTRYPOINT ["dotnet", "ScreenshotCreator.Api.dll"]

Please note that I've adjusted the conditional RUN statement to use the correct syntax in a Dockerfile. If the ${PLATFORM} environment variable is set to "arm", it will execute the first set of commands; otherwise, it will execute the second set of commands.

英文:

I have an ASP.NET Core application running in Docker on my Raspberry Pi (arm64). This is the Dockerfile:

  1. ARG BASE_IMAGE=mcr.microsoft.com/dotnet/aspnet:7.0-bullseye-slim-arm64v8
  2. FROM ${BASE_IMAGE} AS base
  3. WORKDIR /app
  4. FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
  5. WORKDIR /src
  6. COPY ["src/ScreenshotCreator.Api/ScreenshotCreator.Api.csproj", "src/ScreenshotCreator.Api/"]
  7. COPY ["src/ScreenshotCreator.Logic/ScreenshotCreator.Logic.csproj", "src/ScreenshotCreator.Logic/"]
  8. RUN dotnet restore "src/ScreenshotCreator.Api/ScreenshotCreator.Api.csproj"
  9. COPY . .
  10. WORKDIR "/src/src/ScreenshotCreator.Api"
  11. RUN dotnet build "ScreenshotCreator.Api.csproj" -c Release -o /app/build
  12. FROM build AS publish
  13. RUN dotnet publish "ScreenshotCreator.Api.csproj" -c Release -o /app/publish /p:UseAppHost=false
  14. FROM base AS final
  15. WORKDIR /app
  16. COPY --from=publish /app/publish .
  17. RUN apt-get update
  18. RUN apt-get install -y wget libssl1.1 libunwind8
  19. RUN mkdir -p /opt/microsoft/powershell/7
  20. RUN wget -O /tmp/powershell.tar.gz https://github.com/PowerShell/PowerShell/releases/download/v7.2.6/powershell-7.2.6-linux-arm64.tar.gz
  21. RUN tar zxf /tmp/powershell.tar.gz -C /opt/microsoft/powershell/7
  22. RUN chmod +x /opt/microsoft/powershell/7/pwsh
  23. RUN ln -s /opt/microsoft/powershell/7/pwsh /usr/bin/pwsh
  24. RUN rm /tmp/powershell.tar.gz
  25. RUN ["pwsh", "playwright.ps1", "install", "chromium"]
  26. RUN ["pwsh", "playwright.ps1", "install-deps", "chromium"]
  27. ENTRYPOINT ["dotnet", "ScreenshotCreator.Api.dll"]

When trying to build and run this Docker container on my Windows machine (amd64, Docker via WSL2 (Ubuntu)), it fails because a) the base image is not correct and b) the bunch of RUN statements for installing PowerShell at the end have to look different depending on the platform:

  1. # Install PWSH on Raspberry
  2. # https://learn.microsoft.com/en-us/powershell/scripting/install/install-raspbian?view=powershell-7.3#install-on-raspberry-pi-os
  3. sudo apt-get update
  4. sudo apt-get install '^libssl1.0.[0-9]$' libunwind8 -y
  5. wget https://github.com/PowerShell/PowerShell/releases/download/v7.3.4/powershell-7.3.4-linux-arm64.tar.gz
  6. mkdir ~/powershell
  7. tar -xvf ./powershell-7.3.4-linux-arm32.tar.gz -C ~/powershell
  8. ~/powershell/pwsh
  9. # Install PWSH on Ubuntu (WSL2/Docker)
  10. # https://learn.microsoft.com/en-us/powershell/scripting/install/install-ubuntu?view=powershell-7.3#installation-via-package-repository
  11. sudo apt-get update
  12. sudo apt-get install -y wget apt-transport-https software-properties-common
  13. wget -q "https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/packages-microsoft-prod.deb"
  14. sudo dpkg -i packages-microsoft-prod.deb
  15. rm packages-microsoft-prod.deb
  16. sudo apt-get update
  17. sudo apt-get install -y powershell
  18. pwsh

I solved problem a with the variable BASE_IMAGE that I set override locally, but I'm not sure how to handle b. What's the recommended pattern for this using Docker?

  1. Maintain two different Dockerfiles (arm64/Raspi and amd64/Ubuntu/WSL) and swallow the bitter pill of duplicating the first half of the Dockerfile.
  2. Using some sort of conditional RUN statement so that there is only only Dockerfile, looking like this:
  1. ARG BASE_IMAGE=mcr.microsoft.com/dotnet/aspnet:7.0-bullseye-slim-arm64v8
  2. FROM ${BASE_IMAGE} AS base
  3. WORKDIR /app
  4. FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
  5. WORKDIR /src
  6. COPY ["src/ScreenshotCreator.Api/ScreenshotCreator.Api.csproj", "src/ScreenshotCreator.Api/"]
  7. COPY ["src/ScreenshotCreator.Logic/ScreenshotCreator.Logic.csproj", "src/ScreenshotCreator.Logic/"]
  8. RUN dotnet restore "src/ScreenshotCreator.Api/ScreenshotCreator.Api.csproj"
  9. COPY . .
  10. WORKDIR "/src/src/ScreenshotCreator.Api"
  11. RUN dotnet build "ScreenshotCreator.Api.csproj" -c Release -o /app/build
  12. FROM build AS publish
  13. RUN dotnet publish "ScreenshotCreator.Api.csproj" -c Release -o /app/publish /p:UseAppHost=false
  14. FROM base AS final
  15. WORKDIR /app
  16. COPY --from=publish /app/publish .
  17. if ${PLATFORM}=="arm":
  18. RUN apt-get update
  19. RUN apt-get install -y wget libssl1.1 libunwind8
  20. RUN mkdir -p /opt/microsoft/powershell/7
  21. RUN wget -O /tmp/powershell.tar.gz https://github.com/PowerShell/PowerShell/releases/download/v7.2.6/powershell-7.2.6-linux-arm64.tar.gz
  22. RUN tar zxf /tmp/powershell.tar.gz -C /opt/microsoft/powershell/7
  23. RUN chmod +x /opt/microsoft/powershell/7/pwsh
  24. RUN ln -s /opt/microsoft/powershell/7/pwsh /usr/bin/pwsh
  25. RUN rm /tmp/powershell.tar.gz
  26. RUN ["pwsh", "playwright.ps1", "install", "chromium"]
  27. RUN ["pwsh", "playwright.ps1", "install-deps", "chromium"]
  28. else
  29. RUN apt-get update
  30. RUN apt-get install -y wget apt-transport-https software-properties-common
  31. RUN wget -q https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb
  32. RUN dpkg -i packages-microsoft-prod.deb
  33. RUN apt-get update
  34. RUN apt-get install -y powershell
  35. RUN ["pwsh", "playwright.ps1" ,"install", "chromium"]
  36. RUN ["pwsh", "playwright.ps1" ,"install-deps", "chromium"]
  37. ENTRYPOINT ["dotnet", "ScreenshotCreator.Api.dll"]

答案1

得分: 0

以下是已翻译的内容:

有人在Docker论坛给了我这个答案,非常有效。

基本上,现在每个架构都有一个PowerShell安装脚本(install-powershell-arm64.shinstall-powershell-amd64.sh),在Dockerfile中使用如下方式调用:

  1. ARG TARGETARCH
  2. COPY src/ScreenshotCreator.Api/install-powershell-$TARGETARCH.sh /scripts/install-powershell-$TARGETARCH.sh
  3. RUN chmod +x /scripts/install-powershell-$TARGETARCH.sh

这里是GitHub上对应的提交

英文:

Someone in the Docker forum gave me this answer which works like a charm.

Basically there is now a PowerShell installation script for each architecture (install-powershell-arm64.sh and install-powershell-amd64.sh) and within the Dockerfile, it is called like this:

  1. ARG TARGETARCH
  2. COPY src/ScreenshotCreator.Api/install-powershell-$TARGETARCH.sh /scripts/install-powershell-$TARGETARCH.sh
  3. RUN chmod +x /scripts/install-powershell-$TARGETARCH.sh

Here's the corresponding commit on GitHub.

huangapple
  • 本文由 发表于 2023年6月12日 23:57:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76458376.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定