1. 程式人生 > 實用技巧 >使用 Dockerfile 檔案但是不使用快取生成映象

使用 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 cache 10 ---> 49a72547d567 11 Step 4/17 : EXPOSE 443 12 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 80 10 ---> Running in e7716f388165 11 Removing intermediate container e7716f388165 12 ---> fc54a6e3c0aa 13 Step 4/17 : EXPOSE 443 14 ---> Running in 449680497b7f 15 Removing intermediate container 449680497b7f 16 ---> acf106867ca0 17 Step 5/17 : FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build 18 3.1-buster: Pulling from dotnet/core/sdk 19 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.8MB 25 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 80
10  ---> Running in e7716f388165
11 Removing intermediate container e7716f388165
12  ---> fc54a6e3c0aa
13 Step 4/17 : EXPOSE 443
14  ---> Running in 449680497b7f
15 Removing intermediate container 449680497b7f
16  ---> acf106867ca0
17 Step 5/17 : FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
18 3.1-buster: Pulling from dotnet/core/sdk
19 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:bfd6083e9cd36b37b2a4e9f1722cc958b6654fa96cb3d84ef78492ecf00dcd32
27 Status: Downloaded newer image for mcr.microsoft.com/dotnet/core/sdk:3.1-buster
28  ---> 5fe503d51830
29 Step 6/17 : WORKDIR /src
30  ---> Running in 4312c5d63f3f
31 Removing intermediate container 4312c5d63f3f
32  ---> 31cfbe6739ec
33 Step 7/17 : COPY ["PatrickLiu.NetCore.MvcDemo/PatrickLiu.NetCore.MvcDemo.csproj", "PatrickLiu.NetCore.MvcDemo/"]
34  ---> ba9ba79cc7d3
35 Step 8/17 : RUN dotnet restore "PatrickLiu.NetCore.MvcDemo/PatrickLiu.NetCore.MvcDemo.csproj"
36  ---> Running in 8d2c9ee24614
37   Determining projects to restore...
38   Restored /src/PatrickLiu.NetCore.MvcDemo/PatrickLiu.NetCore.MvcDemo.csproj (in 4.64 sec).
39 Removing intermediate container 8d2c9ee24614
40  ---> 872cce519821
41 Step 9/17 : COPY . .
42  ---> 4fe062b14ab1
43 Step 10/17 : WORKDIR "/src/PatrickLiu.NetCore.MvcDemo"
44  ---> Running in 431b1c2f4959
45 Removing intermediate container 431b1c2f4959
46  ---> b9dc3acf883c
47 Step 11/17 : RUN dotnet build "PatrickLiu.NetCore.MvcDemo.csproj" -c Release -o /app/build
48  ---> Running in 639b5e3d01df
49 Microsoft (R) Build Engine version 16.7.0+7fb82e5b2 for .NET
50 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.dll
55   PatrickLiu.NetCore.MvcDemo -> /app/build/PatrickLiu.NetCore.MvcDemo.Views.dll
56 
57 Build succeeded.
58     0 Warning(s)
59     0 Error(s)
60 
61 Time Elapsed 00:00:19.21
62 Removing intermediate container 639b5e3d01df
63  ---> aeb1df18b3f6
64 Step 12/17 : FROM build AS publish
65  ---> aeb1df18b3f6
66 Step 13/17 : RUN dotnet publish "PatrickLiu.NetCore.MvcDemo.csproj" -c Release -o /app/publish
67  ---> Running in ac663a5be455
68 Microsoft (R) Build Engine version 16.7.0+7fb82e5b2 for .NET
69 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.dll
74   PatrickLiu.NetCore.MvcDemo -> /src/PatrickLiu.NetCore.MvcDemo/bin/Release/netcoreapp3.1/PatrickLiu.NetCore.MvcDemo.Views.dll
75   PatrickLiu.NetCore.MvcDemo -> /app/publish/
76 Removing intermediate container ac663a5be455
77  ---> 6ffbf30a49af
78 Step 14/17 : FROM base AS final
79  ---> acf106867ca0
80 Step 15/17 : WORKDIR /app
81  ---> Running in 3dca58d70bef
82 Removing intermediate container 3dca58d70bef
83  ---> 37d1ad5bb22b
84 Step 16/17 : COPY --from=publish /app/publish .
85  ---> 09de2a366451
86 Step 17/17 : ENTRYPOINT ["dotnet", "PatrickLiu.NetCore.MvcDemo.dll"]
87  ---> Running in 2c41d28e90dc
88 Removing intermediate container 2c41d28e90dc
89  ---> 8bf4c94fbc04
90 Successfully built 8bf4c94fbc04
91 Successfully tagged core31v1.112:latest

    結束了。