This repo is to demonstrate how Kaniko makes use of build cache.
-
Initial build:
% docker build -t todoapi:1 src
-
Build again with cache:
% docker build -t todoapi:2 src
-
Modify src and re-build with cache:
% touch src/empty-3 % docker build -t todoapi:3 src
Result:
Step | Time |
---|---|
1 | 3m19s |
2 | 0m5.81s |
3 | 0m15.85s |
-
Set environment variable
PROJECT_ID
andREPO
for convenience:% export PROJECT_ID=project_id_of_your_gcp_project % export REPO=repo_name_of_your_image
-
Initial build with empty cache:
% time \ docker run --rm -v $(pwd)/src:/kaniko-workspace \ gcr.io/kaniko-project/executor:latest \ --cache=true --context=/kaniko-workspace \ --destination=gcr.io/$PROJECT_ID/$REPO
-
Build again with cache:
% time \ docker run --rm -v $(pwd)/src:/kaniko-workspace \ gcr.io/kaniko-project/executor:latest \ --cache=true --context=/kaniko-workspace \ --destination=gcr.io/$PROJECT_ID/$REPO
-
Modify src and re-build with cache:
% touch src/empty-4 % time \ docker run --rm -v $(pwd)/src:/kaniko-workspace \ gcr.io/kaniko-project/executor:latest \ --cache=true --context=/kaniko-workspace \ --destination=gcr.io/$PROJECT_ID/$REPO
Result:
Step | Time |
---|---|
2 | 4m23s |
3 | 1m20s |
4 | 1m55s |
If you use dotnet/core/sdk
as base image, you may encounter error messages near the end of Kaniko build steps, similar to the following:
error building image: error building stage: Unable to add file /tmp/clr-debug-pipe-149-16587078-out to layered map: Error creating hash for /tmp/clr-debug-pipe-149-16587078-out: lstat /tmp/clr-debug-pipe-149-16587078-out: no such file or directory
A workaround is to set environment variable COMPlus_EnableDiagnostics
before dotnet publish
in Dockerfile
:
ENV COMPlus_EnableDiagnostics=0
...
dotnet publish
For more info, see GoogleContainerTools/kaniko#391 (comment)
The sample code in src
directory was extracted from the TodoApi demo in the Microsoft Docs site, retrieved on Nov 27, 2019:
-
Source code - https://github.com/aspnet/AspNetCore.Docs/tree/master/aspnetcore/tutorials/first-web-api/samples/3.0/TodoApi
The original source code to be used in this repo is packed in the TodoApi-original.zip
file for your reference.