# 022基础镜像重构之DotNetRuntime镜像

# 构建工具选型(来自章节d008)

# kenaito-runtime:dotnet9

镜像大小:425.47MB 构建耗时:7.8s

if [ ! -f "aspnetcore-runtime-9.0.0-rc.2.24474.3-linux-x64.tar.gz" ];then
  curl -O https://builds.dotnet.microsoft.com/dotnet/aspnetcore/Runtime/9.0.0-rc.2.24474.3/aspnetcore-runtime-9.0.0-rc.2.24474.3-linux-x64.tar.gz
fi
if [ $? -ne 0 ]; then
  echo "下载 aspnetcore-runtime-9.0.0-rc.2.24474.3-linux-x64.tar.gz 文件失败"
  exit 1
fi

# 释放文件,优化大小
rm -rf build
mkdir build dotnetsdk
tar -xvf aspnetcore-runtime-9.0.0-rc.2.24474.3-linux-x64.tar.gz -C dotnetsdk
if [ $? -ne 0 ]; then
  echo "初始化失败"
  exit 1
fi
mv dotnetsdk build/dotnetsdk
cd build && pwd

# 压缩成品
tar -czvf dotnetsdk.tgz dotnetsdk
rm -rf dotnetsdk

# Dockerfile
cat > Dockerfile << 'EOF'
FROM alibaba-cloud-linux-3-registry.cn-hangzhou.cr.aliyuncs.com/alinux3/alinux3:latest
LABEL MAINTAINER="tianjun@odboy.cn"
ENV TimeZone="Asia/Shanghai"
ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=1
ENV DOTNET_CLI_TELEMETRY_OPTOUT=1
ENV DOTNET_ROOT="/home/admin/dotnetsdk"
ENV PATH="$PATH:${DOTNET_ROOT}"
WORKDIR /home/admin
ADD dotnetsdk.tgz .
RUN ln -snf /usr/share/zoneinfo/$TimeZone /etc/localtime && echo $TimeZone > /etc/timezone; \
sed -i 's/mirrors.cloud.aliyuncs.com/mirrors.aliyun.com/' /etc/yum.repos.d/*.repo; \
chmod +x /home/admin/dotnetsdk/dotnet;
CMD ["sleep","365d"]
EOF

# 构建镜像(docker可换成buildah)
docker build --no-cache -f Dockerfile -t kenaito-runtime:dotnet9 .
if [ $? -ne 0 ]; then
  echo "构建镜像失败"
  exit 1
fi

# 运行测试
docker run kenaito-runtime:dotnet9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
最近更新: 2025-09-04
022基础镜像重构之DotNetRuntime镜像

2017 - 武林秘籍   |