镜像缓存
1.镜像缓存:在构建或者下载镜像时候,当镜像层已经存在的时候,直接使用使用缓存, 不需要进行重新构建镜像,如果我们希望在构建镜像时不使用缓存,可以在 docker build 命令中加上 –no-cache 参数。如果我们改变 Dockerfile 指令的执行顺序,或者修改或添加指令,都会使缓存失效。
2.Dockerfile 在执行的时候,当有执行过相同的代码并且顺序也一致的情况下,就会使用缓存镜像层进行构建新的镜像。Dockerfile 中每一个指令都会创建一个镜像层,上层是依赖于下层的。注:镜像层只是存在一个ID,镜像的内容存在host文件系统上,当需要的时候就使用了缓存。
由于每一步的构建过程都将结果提交为镜像,所以docker的构建过程就显得非常聪明。它将之前得镜像层看做缓存。
比如。在我们的调试例子里,我们不需要再第一步到第三步之间记性任何修改。因此docker会将 之前构建时创建的镜像当做缓存并作为新的开始点。
参考链接https://www.pianshen.com/article/49531206577/
如果想要略过缓存功能,可以使用docker build 的–no-cache标志
使用 Dockerfile 文件但是不使用缓存生成镜像
前一段时候使用 Dockerfile 重新部署 NetCore3.1 项目的时候很顺利,由来由于一些原因,我把以前的镜像删除,如果我们大家继续使用 docker build 命令去生成镜像的话就会报错,例如:
1 [root@localhost PatrickLiu.NetCore]# docker build -t core31v1.112 -f Dockerfile . 2 Sending build context to Docker daemon 4.425MB 3 Step 1/17 : FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base 4 —> e3559b2d50bb 5 Step 2/17 : WORKDIR /app 6 —> Using cache 7 —> 1c0ab1a505e2 8 Step 3/17 : EXPOSE 80 9 —> Using cache10 —> 49a72547d56711 Step 4/17 : EXPOSE 44312 unable to find image “sha256:c5ed536f38a6742b7c084fa87e6ac885d9697960a20860f7fd0299da578cf2c8”
这样肯定是不行的,我们肯定希望使用没有缓存的方式重新加载文件生成镜像,怎么做呢?
问题
你想不用缓存重建Dockerfile。
解决方法
构建镜像时使用 –no-cache 参数。
讨论
为了强制docker构建镜像时不用缓存,执行带–no-cache参数的docker build命令。下面的示例是使用了–no-cache构建镜像。
效果如下:
1 [root@localhost PatrickLiu.NetCore]# docker build –no-cache -t core31v1.112 -f Dockerfile . 2 Sending build context to Docker daemon 4.425MB 3 Step 1/17 : FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base 4 —> e3559b2d50bb 5 Step 2/17 : WORKDIR /app 6 —> Running in e8178063fe45 7 Removing intermediate container e8178063fe45 8 —> 0a1d582b30d4 9 Step 3/17 : EXPOSE 8010 —> Running in e7716f38816511 Removing intermediate container e7716f38816512 —> fc54a6e3c0aa13 Step 4/17 : EXPOSE 44314 —> Running in 449680497b7f15 Removing intermediate container 449680497b7f16 —> acf106867ca017 Step 5/17 : FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build18 3.1-buster: Pulling from dotnet/core/sdk19 e4c3d3e4f7b0: Pull complete
20 101c41d0463b: Pull complete
21 8275efcd805f: Pull complete
22 751620502a7a: Pull complete
23 8e306865fd07: Pull complete
24 9d2f53e752c2: Downloading [===========================> ] 69.19MB/123.8MB25 143a93e01eba: Download complete
镜像文件重新生成,没有使用缓存,每天进步一点点,把问题记录下来,这就是成长了。继续努力。
镜像最后生成的胜利步骤。
1 [root@localhost PatrickLiu.NetCore]# docker build –no-cache -t core31v1.112 -f Dockerfile . 2 Sending build context to Docker daemon 4.425MB 3 Step 1/17 : FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base 4 —> e3559b2d50bb 5 Step 2/17 : WORKDIR /app 6 —> Running in e8178063fe45 7 Removing intermediate container e8178063fe45 8 —> 0a1d582b30d4 9 Step 3/17 : EXPOSE 8010 —> Running in e7716f38816511 Removing intermediate container e7716f38816512 —> fc54a6e3c0aa13 Step 4/17 : EXPOSE 44314 —> Running in 449680497b7f15 Removing intermediate container 449680497b7f16 —> acf106867ca017 Step 5/17 : FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build18 3.1-buster: Pulling from dotnet/core/sdk19 e4c3d3e4f7b0: Pull complete
20 101c41d0463b: Pull complete
21 8275efcd805f: Pull complete
22 751620502a7a: Pull complete
23 8e306865fd07: Pull complete
24 9d2f53e752c2: Pull complete
25 143a93e01eba: Pull complete
26 Digest: sha256:bfd6083e9cd36b37b2a4e9f1722cc958b6654fa96cb3d84ef78492ecf00dcd3227 Status: Downloaded newer image for mcr.microsoft.com/dotnet/core/sdk:3.1-buster28 —> 5fe503d5183029 Step 6/17 : WORKDIR /src30 —> Running in 4312c5d63f3f31 Removing intermediate container 4312c5d63f3f32 —> 31cfbe6739ec33 Step 7/17 : COPY [“PatrickLiu.NetCore.MvcDemo/PatrickLiu.NetCore.MvcDemo.csproj”, “PatrickLiu.NetCore.MvcDemo/“]34 —> ba9ba79cc7d335 Step 8/17 : RUN dotnet restore “PatrickLiu.NetCore.MvcDemo/PatrickLiu.NetCore.MvcDemo.csproj”36 —> Running in 8d2c9ee2461437 Determining projects to restore…38 Restored /src/PatrickLiu.NetCore.MvcDemo/PatrickLiu.NetCore.MvcDemo.csproj (in 4.64 sec).39 Removing intermediate container 8d2c9ee2461440 —> 872cce51982141 Step 9/17 : COPY . .42 —> 4fe062b14ab143 Step 10/17 : WORKDIR “/src/PatrickLiu.NetCore.MvcDemo”44 —> Running in 431b1c2f495945 Removing intermediate container 431b1c2f495946 —> b9dc3acf883c47 Step 11/17 : RUN dotnet build “PatrickLiu.NetCore.MvcDemo.csproj” -c Release -o /app/build48 —> Running in 639b5e3d01df49 Microsoft (R) Build Engine version 16.7.0+7fb82e5b2 for .NET50 Copyright (C) Microsoft Corporation. All rights reserved.51 52 Determining projects to restore…53 All projects are up-to-date for restore.54 PatrickLiu.NetCore.MvcDemo -> /app/build/PatrickLiu.NetCore.MvcDemo.dll55 PatrickLiu.NetCore.MvcDemo -> /app/build/PatrickLiu.NetCore.MvcDemo.Views.dll56 57 Build succeeded.58 0 Warning(s)59 0 Error(s)60 61 Time Elapsed 00:00:19.2162 Removing intermediate container 639b5e3d01df63 —> aeb1df18b3f664 Step 12/17 : FROM build AS publish65 —> aeb1df18b3f666 Step 13/17 : RUN dotnet publish “PatrickLiu.NetCore.MvcDemo.csproj” -c Release -o /app/publish67 —> Running in ac663a5be45568 Microsoft (R) Build Engine version 16.7.0+7fb82e5b2 for .NET69 Copyright (C) Microsoft Corporation. All rights reserved.70 71 Determining projects to restore…72 All projects are up-to-date for restore.73 PatrickLiu.NetCore.MvcDemo -> /src/PatrickLiu.NetCore.MvcDemo/bin/Release/netcoreapp3.1/PatrickLiu.NetCore.MvcDemo.dll74 PatrickLiu.NetCore.MvcDemo -> /src/PatrickLiu.NetCore.MvcDemo/bin/Release/netcoreapp3.1/PatrickLiu.NetCore.MvcDemo.Views.dll75 PatrickLiu.NetCore.MvcDemo -> /app/publish/76 Removing intermediate container ac663a5be45577 —> 6ffbf30a49af78 Step 14/17 : FROM base AS final79 —> acf106867ca080 Step 15/17 : WORKDIR /app81 —> Running in 3dca58d70bef82 Removing intermediate container 3dca58d70bef83 —> 37d1ad5bb22b84 Step 16/17 : COPY –from=publish /app/publish .85 —> 09de2a36645186 Step 17/17 : ENTRYPOINT [“dotnet”, “PatrickLiu.NetCore.MvcDemo.dll”]87 —> Running in 2c41d28e90dc88 Removing intermediate container 2c41d28e90dc89 —> 8bf4c94fbc0490 Successfully built 8bf4c94fbc0491 Successfully tagged core31v1.112:latest
结束了。
