当前位置: 首页 > news >正文

使用 Dockerfile 构建基于 .NET9 的跨平台基础镜像

官方基础镜像准备

微软官方 dotnet sdk 基础镜像:

docker pull mcr.microsoft.com/dotnet/sdk:9.0

拉取 ubuntu 镜像:

docker pull ubuntu:24.04

更多资源请参考:

  • dotnet sdk images,https://mcr.microsoft.com/en-us/artifact/mar/dotnet/sdk/tags
  • github docker buildx ,https://github.com/docker/buildx
  • 阿里云开源镜像站,https://developer.aliyun.com/mirror/
  • 微软官方包源,https://packages.microsoft.com/config/

构建私有化基础镜像

此处使用到的相关资源:

  • dotnet sdk 9.0
  • dotnet aspire
  • PowerShell
  • Ubuntu Server 24.04

Ubuntu 24.04 软件源位置调整

Ubuntu 24.04 的软件源位置调整后,需要根据新的文件路径或结构进行修改。

以下是调整方法:

1. 确认源文件路径

注意 Ubuntu 24.04 的源文件路径已从传统的 /etc/apt/sources.list 文件迁移到使用 sources.list.d/ 目录下的单独 .sources 文件。例如:

  • 原路径:/etc/apt/sources.list
  • 新路径:/etc/apt/sources.list.d/ubuntu.sources
2. 修改源地址

在修改 Ubuntu 的源文件之前,建议先进行备份。

RUN cp /etc/apt/sources.list.d/ubuntu.sources /etc/apt/sources.list.d/ubuntu.sources.bak

如果发现软件源位置有调整,可以使用以下命令来替换为阿里云的镜像源:

RUN sed -i 's|http://archive.ubuntu.com|https://mirrors.aliyun.com|g' /etc/apt/sources.list.d/ubuntu.sources && \sed -i 's|http://security.ubuntu.com|https://mirrors.aliyun.com|g' /etc/apt/sources.list.d/ubuntu.sources
3. 更新包索引

修改完成后,运行以下命令更新包索引:

apt update
4. 验证源是否生效

可以通过安装一个测试包来验证新源是否正常工作。

如果系统提示找不到源文件,请检查路径是否正确,并确保你正在编辑的是当前系统实际使用的 .sources 文件。

构建 Ubuntu 基础镜像

制作 .NET 平台的 Ubuntu 基础镜像,完整的 Dockerfile 内容如下:

# 使用官方的 Ubuntu 24.04 镜像作为基础镜像
FROM --platform=$TARGETPLATFORM ubuntu:24.04# 设置环境变量,避免交互式安装时的提示
ENV DEBIAN_FRONTEND=noninteractive# 创建目录结构
WORKDIR /app# 备份原始源文件并更换为阿里云的镜像源
RUN cp /etc/apt/sources.list /etc/apt/sources.list.bak && \cp /etc/apt/sources.list.d/ubuntu.sources /etc/apt/sources.list.d/ubuntu.sources.bak && \sed -i 's|http://archive.ubuntu.com|https://mirrors.aliyun.com|g' /etc/apt/sources.list && \sed -i 's|http://security.ubuntu.com|https://mirrors.aliyun.com|g' /etc/apt/sources.list.d/ubuntu.sources# 更新包列表并安装常用工具,并立即清理缓存
RUN apt update && \apt upgrade -y && \apt install -y wget software-properties-common gnupg curl && \rm -rf /var/lib/apt/lists/* && \apt clean# 安装 PowerShell 的依赖并清理
RUN apt update && \apt install -y --no-install-recommends \apt-transport-https \ca-certificates && \rm -rf /var/lib/apt/lists/* && \apt clean# 添加 Microsoft 包仓库并安装 PowerShell
RUN wget -q https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/packages-microsoft-prod.deb && \dpkg -i packages-microsoft-prod.deb && \rm packages-microsoft-prod.deb && \apt update && \apt install -y powershell && \rm -rf /var/lib/apt/lists/* && \apt clean# 安装 .NET SDK 并清理
RUN wget https://dot.net/v1/dotnet-install.sh -O dotnet-install.sh && \chmod +x dotnet-install.sh && \./dotnet-install.sh -c Current && \rm dotnet-install.sh# 将 .NET CLI 添加到 PATH 环境变量
ENV PATH="/root/.dotnet:${PATH}"# 安装 .NET Aspire 和 ASP.NET Core 的代码生成器工具并清理
RUN dotnet workload install aspire && \dotnet tool install -g dotnet-aspnet-codegenerator && \dotnet nuget locals all --clear# 创建非 root 用户和组(安全处理已存在的情况)
RUN if getent group appgroup > /dev/null; then groupdel appgroup; fi && \if getent passwd appuser > /dev/null; then userdel appuser; fi && \groupadd -g 1000 appgroup && \useradd -u 1000 -g appgroup -m appuser# 设置工作目录权限
RUN chown -R appuser:appgroup /app# 切换到非 root 用户
USER appuser# 设置默认 Shell 为 PowerShell(可选)
SHELL ["pwsh", "-Command"]# 输出测试信息
CMD ["sh", "-c", "echo 'Ubuntu 镜像源已成功更换为阿里云!PowerShell 和 .NET SDK 已安装。'"]

✅ 优化点说明

  • APT 缓存清理:每次使用 apt install 后都执行 rm -rf /var/lib/apt/lists/*apt clean,以清除包索引和缓存。
  • 临时文件清理:如安装 .NET SDK 后删除了脚本文件 dotnet-install.sh
  • NuGet 缓存清理:使用 dotnet nuget locals all --clear 清除 NuGet 包缓存。

这样可以显著减少最终镜像的体积,同时保持功能不变。

✅ 构建命令

docker build --platform linux/amd64 -t dotnet-sdk:ubuntu-24.04 ./

✅ 运行容器并验证安装

docker run --rm dotnet-sdk:ubuntu-24.04 pwsh -c "dotnet --info"

我们还可以进入容器内进行调试:

docker run -it dotnet-sdk:ubuntu-24.04 pwsh

此处我们就构建了基于 Ubuntu 24.04.NET SDK 基础镜像。


使用示例

如何使用 Dockerfile 构建 .NET 的基础镜像?

docker buildx

✅ 构建基础镜像

要使用提供的 Dockerfile 构建一个基于 amd64 架构的基础镜像,我们可以通过指定 --platform 参数为 linux/amd64 来强制构建特定架构的镜像。

  1. 构建单个平台镜像
docker build --platform linux/amd64 -t your-amd64-image-name:tag ./
  1. 使用 buildx 构建多平台镜像

或者我们也可以指定多个目标平台,例如同时构建 amd64arm64

docker buildx build \--platform linux/amd64,linux/arm64 \-t your-dockerhub-username/image:tag \--push \./
📌 参数解释

🔹--platform linux/amd64,linux/arm64:指定目标平台为 amd64(x86_64)arm64(RISC) 架构。
🔹-t your-dockerhub-username/image:tag:为镜像打标签,如 aspire-dotnet-sdk:9.0
🔹 --push 表示构建后推送到远程仓库(需登录 Docker Hub 或私有镜像仓库)。
🔹 如果你不希望推送,可以去掉 --push 并使用 --load 加载本地镜像(但仅限当前平台)。
🔹./:表示当前目录为构建上下文。

说明:使用 docker buildx 实现真正的多平台镜像构建能力。

🧼 构建示例

此处我们构建一个 Linux 环境多平台(amd64,arm64)支持的基础镜像,命令如下:

docker buildx build --platform linux/amd64,linux/arm64 -t aspire-dotnet-sdk-amd64:9.0 --load ./

⚠️ 注意事项:

  1. 确保 Docker 支持多平台构建
    • 推荐使用支持 buildxDocker 版本(建议 20.10+)。
    • 若提示 --platform 不被支持,请启用 buildx
  • 启用 buildx 构建器并设置为默认
# 创建并使用一个支持多平台的构建器
docker buildx create --use --name mybuilder# 启动构建器(如果刚创建可能不需要再启动)
docker buildx inspect --bootstrap
  • 此时你应该看到类似输出:
Name:   mybuilder
Driver: docker-container
Nodes:
Name:      mybuilder0
OS/Arch:   linux/amd64

docker buildx 使用 docker-container 驱动,支持多平台交叉编译。

  1. 若需推送到镜像仓库,请在构建后使用 docker push 命令:
docker push your-image-name:tag

🔁 小结

操作命令
创建 buildx 构建器docker buildx create --use --name mybuilder
构建并推送多平台镜像docker buildx build --platform linux/amd64,linux/arm64 -t user/repo:tag --push .
本地加载单平台镜像docker buildx build --platform linux/amd64 -t local-image:tag --load .
查看多平台镜像信息docker buildx imagetools inspect user/repo:tag
http://www.dtcms.com/a/265756.html

相关文章:

  • 安卓开机自启动方案
  • Kafka生态整合深度解析:构建现代化数据架构的核心枢纽
  • Sklearn安装使用教程
  • 机器人焊接电源节气阀
  • 工程化实践——标准化Eslint、PrettierTS
  • 读书笔记:《DevOps实践指南》
  • android 网络访问拦截器的编写的几种方式
  • React 学习(3)
  • springboot 中使用 websocket
  • PHP:从入门到实践——构建高效Web应用的利器
  • 2011年英语一
  • AlpineLinux安装x11vnc服务端实现远程桌面登录
  • Zephyr RTOS 防止中断影响数据写入
  • cv610将音频chn0配置为g711a,chn1配置为 aac编码,记录
  • ARM SMMUv3故障和错误(五)
  • mac 电脑安装Homebrew来安装npm与node成功后,安装nvm的流程
  • macOS 26正式发布,全新Liquid Glass设计语言亮相
  • join性能问题,distinct和group by性能,备库自增主键问题
  • 微信小程序在用户拒绝授权后无法使用wx.opensetting再次获取定位授权
  • 针孔相机模型
  • python学习打卡day59
  • 【轨物洞见】光伏机器人与组件、支架智能化协同白皮书
  • Linux操作系统之文件(二):重定向
  • Android 系统默认的Launcher3,Android 系统的导航栏(通常是屏幕底部)显示的 4 个快捷应用图标,如何替换这4个应用图标为客户想要的。
  • Fiddler中文版抓包工具在后端API调试与Mock中的巧用
  • Treap树
  • thinkphp8接管异常处理类
  • linux系统 weblogic10.3.6(jar) 下载及安装
  • 后端 Maven打包 JAR 文件、前端打包dist文件、通过后端服务访问前端页面、Nginx安装与部署
  • Josn模块的使用