以我们家庭好医.netcore项目做个示例,来看看怎么进一步优化Docker镜像的大小。
这里以先编译再做镜像的方法为示例,当然你也可以把编译部分也写进Dockerfile里。
先上Dockerfile
1 2 3 4 5 6 7 8 |
FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base WORKDIR /app EXPOSE 5000 FROM base AS final WORKDIR /app COPY . . ENTRYPOINT ["dotnet", "JTHY.Web.dll"] |
看看生成的镜像大小:265MB
Dockerfile
1 2 3 4 5 6 7 8 |
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base WORKDIR /app EXPOSE 5000 FROM base AS final WORKDIR /app COPY /. /app ENTRYPOINT ["dotnet", "JTHY.Web.dll"] |
镜像大小还是265MB,并没有变小。
Dockerfile
1 2 3 4 5 6 7 8 |
ROM mcr.microsoft.com/dotnet/aspnet:3.1-alpine AS base WORKDIR /app EXPOSE 5000 FROM base AS final WORKDIR /app COPY /. /app ENTRYPOINT ["dotnet", "JTHY.Web.dll"] |
再来看看镜像大小:146MB,是不是很神奇~