ubuntu Dockerfile中特定于平台的RUN语句

jgovgodb  于 2023-08-03  发布在  Docker
关注(0)|答案(1)|浏览(109)

我有一个ASP.NET Core应用程序在我的Raspberry Pi(arm64)上的Docker中运行。这就是Dockerfile

ARG BASE_IMAGE=mcr.microsoft.com/dotnet/aspnet:7.0-bullseye-slim-arm64v8
FROM ${BASE_IMAGE} AS base
WORKDIR /app

FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["src/ScreenshotCreator.Api/ScreenshotCreator.Api.csproj", "src/ScreenshotCreator.Api/"]
COPY ["src/ScreenshotCreator.Logic/ScreenshotCreator.Logic.csproj", "src/ScreenshotCreator.Logic/"]
RUN dotnet restore "src/ScreenshotCreator.Api/ScreenshotCreator.Api.csproj"
COPY . .
WORKDIR "/src/src/ScreenshotCreator.Api"
RUN dotnet build "ScreenshotCreator.Api.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "ScreenshotCreator.Api.csproj" -c Release -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
RUN apt-get update
RUN apt-get install -y wget libssl1.1 libunwind8
RUN mkdir -p /opt/microsoft/powershell/7
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
RUN tar zxf /tmp/powershell.tar.gz -C /opt/microsoft/powershell/7
RUN chmod +x /opt/microsoft/powershell/7/pwsh
RUN ln -s /opt/microsoft/powershell/7/pwsh /usr/bin/pwsh
RUN rm /tmp/powershell.tar.gz
RUN ["pwsh", "playwright.ps1", "install", "chromium"]
RUN ["pwsh", "playwright.ps1", "install-deps", "chromium"]
ENTRYPOINT ["dotnet", "ScreenshotCreator.Api.dll"]

字符串
当尝试在我的Windows机器上构建和运行这个Docker容器时(amd64,Docker通过WSL 2(Ubuntu)),它失败了,因为a)基础映像不正确,b)最后安装PowerShell的一堆RUN语句必须根据平台的不同而看起来不同:

# Install PWSH on Raspberry
# https://learn.microsoft.com/en-us/powershell/scripting/install/install-raspbian?view=powershell-7.3#install-on-raspberry-pi-os
sudo apt-get update
sudo apt-get install '^libssl1.0.[0-9]$' libunwind8 -y
wget https://github.com/PowerShell/PowerShell/releases/download/v7.3.4/powershell-7.3.4-linux-arm64.tar.gz
mkdir ~/powershell
tar -xvf ./powershell-7.3.4-linux-arm32.tar.gz -C ~/powershell
~/powershell/pwsh

# Install PWSH on Ubuntu (WSL2/Docker)
# https://learn.microsoft.com/en-us/powershell/scripting/install/install-ubuntu?view=powershell-7.3#installation-via-package-repository
sudo apt-get update
sudo apt-get install -y wget apt-transport-https software-properties-common
wget -q "https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/packages-microsoft-prod.deb"
sudo dpkg -i packages-microsoft-prod.deb
rm packages-microsoft-prod.deb
sudo apt-get update
sudo apt-get install -y powershell
pwsh


我用变量BASE_IMAGE解决了问题a,我在本地设置了override,但我不确定如何处理b。使用Docker的推荐模式是什么?
1.维护两个不同的Dockerfilearm64/Raspi和amd64/Ubuntu/WSL),并吞下复制Dockerfile前半部分的苦果。
1.使用某种 * 条件RUN语句 *,以便只有Dockerfile,看起来像这样:

ARG BASE_IMAGE=mcr.microsoft.com/dotnet/aspnet:7.0-bullseye-slim-arm64v8
FROM ${BASE_IMAGE} AS base
WORKDIR /app

FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["src/ScreenshotCreator.Api/ScreenshotCreator.Api.csproj", "src/ScreenshotCreator.Api/"]
COPY ["src/ScreenshotCreator.Logic/ScreenshotCreator.Logic.csproj", "src/ScreenshotCreator.Logic/"]
RUN dotnet restore "src/ScreenshotCreator.Api/ScreenshotCreator.Api.csproj"
COPY . .
WORKDIR "/src/src/ScreenshotCreator.Api"
RUN dotnet build "ScreenshotCreator.Api.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "ScreenshotCreator.Api.csproj" -c Release -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .

if ${PLATFORM}=="arm":
    RUN apt-get update
    RUN apt-get install -y wget libssl1.1 libunwind8
    RUN mkdir -p /opt/microsoft/powershell/7
    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
    RUN tar zxf /tmp/powershell.tar.gz -C /opt/microsoft/powershell/7
    RUN chmod +x /opt/microsoft/powershell/7/pwsh
    RUN ln -s /opt/microsoft/powershell/7/pwsh /usr/bin/pwsh
    RUN rm /tmp/powershell.tar.gz
    RUN ["pwsh", "playwright.ps1", "install", "chromium"]
    RUN ["pwsh", "playwright.ps1", "install-deps", "chromium"]
else 
    RUN apt-get update
    RUN apt-get install -y wget apt-transport-https software-properties-common
    RUN wget -q https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb
    RUN dpkg -i packages-microsoft-prod.deb
    RUN apt-get update
    RUN apt-get install -y powershell
    RUN ["pwsh", "playwright.ps1" ,"install", "chromium"]
    RUN ["pwsh", "playwright.ps1" ,"install-deps", "chromium"]

ENTRYPOINT ["dotnet", "ScreenshotCreator.Api.dll"]

2w2cym1i

2w2cym1i1#

Docker论坛上有人给了我this answer,它的工作原理很有魅力。
基本上,现在每个架构(install-powershell-arm64.shinstall-powershell-amd64.sh)都有一个PowerShell安装脚本,在Dockerfile中,它的调用如下:

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

字符串
Here's the corresponding commit on GitHub

相关问题