.NET 7应用构建ARM64容器报错及优化方案咨询
构建.NET 7 ARM64容器镜像部署树莓派CM4问题解决
我有一个.NET 7应用,需要部署在树莓派Compute Module 4(ARM64架构)的容器中,计划将构建好的ARM64镜像推送到容器仓库,再由树莓派拉取运行。
现有Dockerfile
# Stage 1: Build the application FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:7.0 AS build WORKDIR /app COPY . . RUN dotnet publish -c Release -o out # Stage 2: Create the final image FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS runtime WORKDIR /app COPY --from=build /app/out . ENTRYPOINT ["dotnet", "MyApp.dll"]
构建报错情况
使用命令docker build -t app --platform linux/amd64 .构建Linux/AMD64镜像可正常运行,但执行docker build -t beacon --platform linux/arm64 .构建ARM64镜像时,第8行出现如下错误:
> [build 4/4] RUN dotnet publish -c Release -o out: 0.285 exec /bin/sh: exec format error ------ Dockerfile:8 -------------------- 6 | COPY . . 7 | 8 | >>> RUN dotnet publish -c Release -o out 9 | 10 | # Stage 2: Create the final image -------------------- ERROR: failed to solve: process "/bin/sh -c dotnet publish -c Release -o out" did not complete successfully: exit code: 1
疑问点
- 当前Dockerfile或ARM64构建命令是否存在问题?
- 有无更优的ARM64镜像构建、推送并让树莓派拉取运行的方案?
已解决方案:使用Docker Buildx
通过Docker Buildx解决了跨架构构建问题,以下是支持生产和开发阶段的Makefile:
setup_builder: docker buildx create --use docker buildx create --use --name app_builder --use docker buildx inspect app_builder --bootstrap build_development: docker buildx build --platform linux/amd64 -t app:latest . --load build_production: docker buildx build --platform linux/arm64 -t app:latest . compose: docker-compose up
本地运行用的Docker Compose配置:
version: '3.4' services: my-service: image: app:latest volumes: - a_file:/app/a_file - another_file:/app/another_file - yet_another_file:/app/yet_another_file network_mode: host
内容的提问来源于stack exchange,提问作者kostas
相关产品推荐
相关产品推荐

