我正在尝试为 Mac 上的 .NET8 API 创建一个 docker 容器,以便部署到 CloudRun,这在使用 .NET6 时运行良好,因为我们已经在生产中运行了一些大约一年。但是……
我正在尝试为 Mac 上的 .NET8 API 创建一个 docker 容器,以便部署到 CloudRun,这在使用 .NET6 时运行得很好,因为我们已经在生产中运行了一些大约一年。但是当我执行
docker build .-t testapi:alpine-secure-test-runtime-f Dockerfile-TestAPI
我收到以下错误: [build 7/10] RUN dotnet restore -vd \'XYZ.Test.API/XYZ.Test.API.csproj\':0.114 [/usr/share/dotnet/sdk/8.0.303/dotnet.runtimeconfig.json] 中发生 JSON 解析异常,偏移量 0(第 1 行,第 1 列):文档为空。0.114 无效的 Runtimeconfig.json [/usr/share/dotnet/sdk/8.0.303/dotnet.runtimeconfig.json] [/usr/share/dotnet/sdk/8.0.303/dotnet.runtimeconfig.dev.json]
看起来这是预期的路径 /usr/share/dotnet/sdk/8.0.303/ 但它实际位于 /usr/local/share/dotnet/sdk/8.0.303/
基于此,我在 Mac 上运行了命令 grep -r '/usr/share/dotnet/sdk/8.0.303',我从名为 **./.docker/scout/sbom/sha256/d42c4838eac81b78f129fac7d901a005615e9d4a0a5eb08d1898d442f36f2e6a/sbom.json ** 的 docker 文件中得到了数百个匹配项。
目前,我不确定这是否是破坏容器创建过程的原因,或者如何修复它。我期望找到一个可配置的实例,一旦正确设置,它就可以解决问题。
还尝试修复警告 FromPlatformFlagConstDisallowed,并通过在 FROM 之前添加 ARG 来存储它来完成。不幸的是,它仍然显示相同的错误。
我访问了 Rider 社区论坛、Docker 社区论坛/Git 等,寻求对我的问题的澄清,但不幸的是,我没有找到任何可以指明正确方向的东西。
使用.Net6 或使用.NET8 自动生成的项目 DockerFile 时此过程不会失败,仅在使用 alpine 时才会失败。
请指教。
开发机器: MacMini M1 16 GBOS:Sonoma 14.5 (23F79).NET版本:8.0.303
Docker 桌面版本: 版本:4.33.0 (160616)引擎:27.1.1Compose:v2.29.1-desktop.1Credential Helper:v0.8.2Kubernetes:v1.30.2
Docker文件:
` FROM --platform=linux/amd64 mcr.microsoft.com/dotnet/runtime-deps:8.0-alpine AS base
WORKDIR /app
EXPOSE 8080
ENV ASPNETCORE_ENVIRONMENT=Development
ENV ASPNETCORE_URLS=http://+:8080
#add ICU en-us (icu-libs) to Alpine and TimezoneInfo (tzdata)
RUN apk add --no-cache icu-libs
#only for languages that are not English
#RUN apk add --no-cache icu-data-full
RUN apk add --no-cache tzdata
RUN adduser -u 1111 --disabled-password --gecos "" appuser && chown -R appuser /app
USER appuser
FROM mcr.microsoft.com/dotnet/sdk:8.0-alpine AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["XYZ.Test.API/XYZ.Test.API.csproj", "XYZ.Test.API/"]
COPY ["XYZ.Test.Common/XYZ.Test.Common.csproj", "XYZ.Test.Common/"]
COPY ["XYZ.Test.Data/XYZ.Test.Data.csproj", "XYZ.Test.Data/"]
COPY ["XYZ.Test.Domain/XYZ.Test.Domain.csproj", "XYZ.Test.Domain/"]
RUN dotnet restore "XYZ.Test.API/XYZ.Test.API.csproj"
COPY . .
WORKDIR "/src/XYZ.Test.API"
RUN dotnet build "XYZ.Test.API.csproj" -c $BUILD_CONFIGURATION -o /app/build
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "XYZ.Test.API.csproj" -c $BUILD_CONFIGURATION -o /app/publish \
-r linux-musl-x64 \
--self-contained true \
-p:PublishTrimmed=true \
-p:PublishSingleFile=true
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["./XYZ.Test.API"]`
Docker 日志:
`(base) dev@dev-Mac-mini XYZ.Test % docker build . -t testapi:alpine-secure-test-runtime -f Dockerfile-TestAPI
Building 0.2s (18/23) docker:desktop-linux
[internal] load build definition from Dockerfile-TestAPI 0.0s
transferring dockerfile: 1.93kB 0.0s
WARN: FromPlatformFlagConstDisallowed: FROM --platform flag should not use constant value "linux/amd64" (line 2) 0.0s
[internal] load metadata for mcr.microsoft.com/dotnet/sdk:8.0-alpine 0.0s
[internal] load metadata for mcr.microsoft.com/dotnet/runtime-deps:8.0-alpine 0.0s
[internal] load .dockerignore 0.0s
transferring context: 417B 0.0s
[build 1/10] FROM mcr.microsoft.com/dotnet/sdk:8.0-alpine 0.0s
[internal] load build context 0.1s transferring context: 47.30kB 0.1s
[base 1/5] FROM mcr.microsoft.com/dotnet/runtime-deps:8.0-alpine 0.0s
CACHED [base 2/5] WORKDIR /app 0.0s
CACHED [base 3/5] RUN apk add --no-cache icu-libs 0.0s
CACHED [base 4/5] RUN apk add --no-cache tzdata 0.0s
CACHED [base 5/5] RUN adduser -u 8888 --disabled-password --gecos "" appuser && chown -R appuser /app 0.0s
CACHED [final 1/2] WORKDIR /app 0.0s
CACHED [build 2/10] WORKDIR /src 0.0s
CACHED [build 3/10] COPY [XYZ.Test.API/XYZ.Test.API.csproj, XYZ.Test.API/] 0.0s
CACHED [build 4/10] COPY [XYZ.Test.Common/XYZ.Test.Common.csproj, XYZ.Test.Common/] 0.0s
CACHED [build 5/10] COPY [XYZ.Test.Data/XYZ.Test.Data.csproj, XYZ.Test.Data/] 0.0s
CACHED [build 6/10] COPY [XYZ.Test.Domain/XYZ.Test.Domain.csproj, XYZ.Test.Domain/] 0.0s
ERROR [build 7/10] RUN dotnet restore -v d "XYZ.Test.API/XYZ.Forms.API.csproj" 0.1s
**# [build 7/10] RUN dotnet restore -v d "XYZ.Test.API/XYZ.Test.API.csproj":
0.114 A JSON parsing exception occurred in[/usr/share/dotnet/sdk/8.0.303/dotnet.runtimeconfig.json], offset 0 (line 1, column 1): The document is empty.
0.114 Invalid runtimeconfig.json [/usr/share/dotnet/sdk/8.0.303/dotnet.runtimeconfig.json] [/usr/share/dotnet/sdk/8.0.303/dotnet.runtimeconfig.dev.json]
------**
1 warning found (use docker --debug to expand):
- FromPlatformFlagConstDisallowed: FROM --platform flag should not use constant value "linux/amd64" (line 2) Dockerfile-TestAPI:27
--------------------
** 25 | COPY ["XYZ.Test.Data/XYZ.Test.Data.csproj", "XYZ.Test.Data/"]
26 | COPY ["XYZ.Forms.Domain/XYZ.Test.Domain.csproj", "XYZ.Test.Domain/"]
27 | >>> RUN dotnet restore -v d "XYZ.Test.API/XYZ.Test.API.csproj"
28 | COPY . .
29 | WORKDIR "/src/XYZ.Test.API"
--------------------**
ERROR: failed to solve: process "/bin/sh -c dotnet restore -v d XYZ.Test.API/XYZ.Test.API.csproj\"" did not complete successfully: exit code: 147`