设为「特别关注」每天带你玩转网络安全运维、应用开发、物联网IOT学习!
希望各位看友【关注、点赞、评论、收藏、投币】,助力每一个梦想。

本章目录
首发地址: https://mp.weixin.qq.com/s/wchtH6i0xKrIrqSuYKmWkg
原文地址: https://blog.weiyigeek.top/2022/9-1-684.html
0x00 前言简述
快速介绍
什么是Kaniko?
kaniko 是一个在容器或 Kubernetes 集群内从 Dockerfile 构建容器镜像的工具 ( Build Container Images In Kubernetes )。

温馨提示: kaniko不是谷歌官方发布支持的产品.
为啥用Kaniko?
由于 kaniko 不依赖于 Docker 守护进程,并且完全在用户空间中执行 Dockerfile 中的每个命令,这使得能够在轻松或安全地运行在
无Docker守护程序的环境(如标准Kubernetes集群 V1.24.x)中构建容器映像。
在 Kubernetes V1.24.x 版本之后默认采用 containerd.io 作为缺省的cri,不在支持 docker-shim 意味着我们不需要安装 docker 环境
Kaniko 是如何工作的?
kaniko 执行器镜像负责从 Dockerfile 构建镜像并将其推送到注册表,其流程大致如下:
- 首先在执行者图像中,我们提取基础镜像的文件系统(Dockerfile 中的 FROM 镜像)。
- 其次我们执行 Dockerfile 中的命令,之后在用户空间中对文件系统进行快照每一个。
- 然后在每个命令之后,我们将一层更改的文件附加到基础 图像(如果有)并更新图像元数据。
Kaniko 已知功能问题
- kaniko 不支持构建 Windows 容器。
- kaniko 不支持 v1 Registry API。(由于其不安全性当前基本都是使用V2协议, 例如 Harbor)
- kaniko 不支持在除官方kaniko映像之外的任何Docker映像中运行二进制Kanico可执行文件(即YMMV)。
kaniko 构建上下文
描述: kaniko 的构建上下文与您发送 Docker 守护程序以进行映像构建的构建上下文非常相似;它代表一个包含 Dockerfile 的目录,kaniko 将使用它来构建您的图像。
例如, Dockerfile 中的 COPY 命令应该引用构建上下文中的文件, 所以您需要将构建上下文存储在 kaniko 可以访问的位置。
目前kaniko 支持以下存储解决方案:
- GCS Bucket
- S3 Bucket
- Azure Blob Storage
- Local Directory
- Local Tar
- Standard Input
- Git Repository
运行 kaniko 时,使用 --context 带有适当前缀的标志指定构建上下文的位置, 如果您不指定前缀 kaniko 将假定一个本地目录, 该参数可用值:
| Source | Prefix | Example |
|---|---|---|
| Local Directory | dir://[path to a directory in the kaniko container] | dir:///workspace |
| Local Tar Gz | tar://[path to a .tar.gz in the kaniko container] | tar://path/to/context.tar.gz |
| Standard Input | tar://[stdin] | tar://stdin |
| GCS Bucket | gs://[bucket name]/[path to .tar.gz] | gs://kaniko-bucket/path/to/context.tar.gz |
| S3 Bucket | s3://[bucket name]/[path to .tar.gz] | s3://kaniko-bucket/path/to/context.tar.gz |
| Azure Blob Storage | https://[account].[azureblobhostsuffix]/[container]/[path to .tar.gz] | https://myaccount.blob.core.windows.net/container/path/to/context.tar.gz |
| Git Repository | git://[repository url][#reference][#commit-id] | git://github.com/acme/myproject.git#refs/heads/mybranch# |
例如,要使用名为 kaniko-bucket 的 GCS 存储桶,您需要传入 --context=gs://kaniko-bucket/path/to/context.tar.gz 。
温馨提示:kaniko 允许的唯一标准输入是 .tar.gz 格式, 如果要创建压缩 tar,您可以运行 tar -C <path to build context> -zcvf context.tar.gz .命令。
$ ls cache/ Dockerfile # 压缩上下文目录 $ tar -C cache/ -zcvf context.tar.gz . ./ ./Dockerfile # 查看压缩文件 $ tar -ztvf context.tar.gz drwxr-xr-x root/root 0 2022-09-08 23:03 ./ -rw-r--r-- root/root 52 2022-09-08 23:04 ./Dockerfile
kaniko 缓存构建
- Caching Layers :kaniko 可以在远程存储库中缓存由RUN(由flag--cache-RUN-layers配置)和COPY(由flag--cache-COPY-layeers配置)命令创建的层。
在执行命令之前 kaniko 会检查层的缓存,如果存在 kaniko将拉取并提取缓存层,而不是执行命令。如果没有 kaniko将执行命令,然后将新创建的层推送到缓存。
用户可以通过设置--cache=true标志选择缓存,并且可以通过--cache-repo标志提供用于存储缓存层的远程存储库, 如果未提供此标志则将从提供的--destination推断缓存的repo。
温馨提示: 在缓存未命中后,kaniko无法从缓存中找到读取层,所有后续层都将在本地构建,而无需咨询缓存。 - Caching Base Images: kaniko 可以将图像缓存在本地目录中,该目录可以卷装载到KanikoPod中。为此必须首先填充缓存, 我们在
gcr.io/kaniko-project/warmer提供了一个kaniko缓存预热映像:
--image : 指定所需任意数量的图像, 填充缓存后 使用与上述相同的
--cache=true标志选择缓存, 本地缓存的位置通过--cache-dir标志提供,默认为/cache与缓存预热器一样, 在实践中通常与 Kubernetes 集群和持久缓存卷一起使用。
示例:docker run -v $(pwd):/workspace gcr.io/kaniko-project/warmer:latest --cache-dir=/workspace/cache --image=<image to cache> --image=<another image to cache>
0x01 部署使用
环境依赖
- kaniko 图像镜像
- 一个 Kubernetes 集群 或者 一个 Containerd 容器运行环境。
- 一个 dockerhub 帐户 用于将构建的图像公开。
小试牛刀之在Kubernetes集群中构建并发布镜像
描述: 此处我们准备在一个K8S集群中使用kaniko提供的镜像,按照提供的Dockerfile指令进行镜像构建,并上传到 docker hub 仓库中,以下为操作流程、
操作流程
步骤 01.首先, 为了加快构建速度, 我们提前在集群中拉取 gcr.io/kaniko-project/executor 镜像到本地, 由于国内无法直接拉取此处我采用这篇【使用Aliyun容器镜像服务对海外gcr、quay仓库镜像进行镜像拉取构建】 文章中的方法进行拉取构建国外gcr.io仓库中的镜像。
# 此处我已经创建了国内可以访问拉取的 executor 镜像, 不想在Aliyun容器镜像服务中进行创建拉取的朋友可以直接使用如下仓库地址。 registry.cn-hangzhou.aliyuncs.com/weiyigeek/kaniko-executor:latest # 使用 ctr 或者 crictl 进行镜像拉取 $ crictl pull registry.cn-hangzhou.aliyuncs.com/weiyigeek/kaniko-executor:latest $ crictl images | grep "kaniko-executor" registry.cn-hangzhou.aliyuncs.com/weiyigeek/kaniko-executor latest da9592dbe1de3 25.8MB
步骤 02.准备一个 Dockerfile 此处将 registry.cn-hangzhou.aliyuncs.com/weiyigeek/kaniko-executor 镜像打包上传到hub中作为演示。
# 创建存放dockerfile目录以及持久化缓存目录 mkdir -vp /storage/dev/soft/kaniko/{cache,demo} cd /storage/dev/soft/kaniko/demo tee dockerfile <<'EOF' FROM registry.cn-hangzhou.aliyuncs.com/weiyigeek/kaniko-executor:latest LABEL MAINTAINER=master@weiyigeeek.top BUILDTYPE=kaniko EOF
步骤 03.创建一个授权令牌的 Secret , 此处以公共的docker hub为例。
# 语法: ~$ kubectl create secret docker-registry dockerhub --docker-server=<your-registry-server> --docker-username=<your-name> --docker-password=<your-pword> --docker-email=<your-email> # 参数值: # <your-registry-server> is your Private Docker Registry FQDN. (https://index.docker.io/v1/ for DockerHub) # <your-name> is your Docker username. # <your-pword> is your Docker password. # <your-email> is your Docker email. # 创建示例: 此 docker-registry 将在 pod.yaml 配置中使用 ~$ kubectl create secret docker-registry dockerhub --docker-server=https://index.docker.io/v1/ --docker-username=weiyigeek --docker-password=PASSWORD --docker-email=master@weiyigeek.top # secret/dockerhub created # 查看创建的 secrets 情况 ~$ kubectl get secrets dockerhub NAME TYPE DATA AGE dockerhub kubernetes.io/dockerconfigjson 1 16s ~$ kubectl get secrets dockerhub -o yaml apiVersion: v1 data: .dockerconfigjson: eyJhdXRo*******VhsbE1qQXhPUT09In19fQ== kind: Secret metadata: name: dockerhub
步骤 04.创建一个在k8s集群中运行的Pod,其资源清单如下所示:
tee kaniko.yaml <<'EOF' apiVersion: v1 kind: Pod metadata: name: kaniko spec: containers: - name: kaniko image: registry.cn-hangzhou.aliyuncs.com/weiyigeek/kaniko-executor:latest env: - name: DOCKERHUB value: "docker.io" - name: AUTHOR value: "weiyigeek" - name: IMAGE_NAME value: "kaniko-executor" - name: IMAGE_VERSION value: "v1.9.0" args: [ "--dockerfile=/workspace/dockerfile", "--context=dir://workspace", "--destination=docker.io/weiyigeek/kaniko-executor:v1.9.0", "--cache", "--cache-dir=/cache"] volumeMounts: - name: kaniko-secret mountPath: /kaniko/.docker - name: dockerfile-storage mountPath: /workspace - name: kaniko-cache mountPath: /cache restartPolicy: Never nodeSelector: kubernetes.io/hostname: "weiyigeek-226" volumes: - name: kaniko-secret secret: secretName: dockerhub items: - key: .dockerconfigjson path: config.json - name: dockerfile-storage hostPath: path: /storage/dev/soft/kaniko/demo type: DirectoryOrCreate - name: kaniko-cache hostPath: path: /storage/dev/soft/kaniko/cache type: DirectoryOrCreate EOF # args 参数说明 --dockerfile=/workspace/dockerfile # 指定 dockerfile 路径 --context=dir://workspace # 指定构建上下文 --destination=docker.io/weiyigeek/kaniko-executor:v1.9.0 # 指定生成镜像的tag --cache # 使用缓存 --cache-dir # 指定缓存目录
温馨提示: kaniko 中的二进制可执行文件 executor 支持的参数详解(https://github.com/GoogleContainerTools/kaniko/#additional-flags)
步骤 05.执行 kubectl apply 部署资源清单生成运行 pod , 此处通过 kubectl logs 日志命令可以发现kaniko执行镜像构建,以及上传镜像到docker hub之中
kubectl apply -f kaniko.yaml # pod/kaniko created kubectl logs -f kaniko # INFO[0005] Retrieving image manifest registry.cn-hangzhou.aliyuncs.com/weiyigeek/kaniko-executor:latest # INFO[0005] Retrieving image registry.cn-hangzhou.aliyuncs.com/weiyigeek/kaniko-executor:latest from registry registry.cn-hangzhou.aliyuncs.com # INFO[0006] Built cross stage deps: map[] # INFO[0006] Retrieving image manifest registry.cn-hangzhou.aliyuncs.com/weiyigeek/kaniko-executor:latest # INFO[0006] Returning cached image manifest # INFO[0006] Executing 0 build triggers # INFO[0006] Building stage 'registry.cn-hangzhou.aliyuncs.com/weiyigeek/kaniko-executor:latest' [idx: '0', base-idx: '-1'] # INFO[0006] Skipping unpacking as no commands require it. # INFO[0006] LABEL MAINTAINER=master@weiyigeeek.top BUILDTYPE=kaniko # INFO[0006] Applying label MAINTAINER=master@weiyigeeek.top # INFO[0006] Applying label BUILDTYPE=kaniko # INFO[0006] Pushing image to docker.io/weiyigeek/kaniko-executor:v1.9.0
步骤 06.在客户端中可以使用 docker 或者 ctr 、crictl 命令将上传到hub中的镜像进行拉取, 并且查看hub仓库中的 kaniko-executor:v1.9.0 镜像信息(https://hub.docker.com/r/weiyigeek/kaniko-executor)。
docker pull weiyigeek/kaniko-executor:v1.9.0 9d4299bbd943: Already exists .............. a8dae3110e38: Already exists v1.9.0: Pulling from weiyigeek/kaniko-executor Digest: sha256:9b0ef02e7650d00d24bbca683e317bc103d6d842311ff13ec6daee60c37b1e62 Status: Downloaded newer image for weiyigeek/kaniko-executor:v1.9.0 docker.io/weiyigeek/kaniko-executor:v1.9.0

步骤 07.扩展补充,除了上述方式指定dockerfile文件和上下文外,我们还可以在运行 kaniko 时使用标准输入构建上下文,但需要添加 -i, --interactive参数, 一旦kaniko运行它将从STDIN获取数据,并将构建上下文创建为压缩tar,然后它将在启动映像构建之前解包构建上下文的压缩tar。
如何使用 .tar.gz 标准输入数据交互运行 kaniko 的完整示例,使用带有临时容器和完全无 docker 环境的 Kubernetes 命令行来进行镜像构建与发布:
echo -e 'FROM alpine nRUN echo "created from standard input"' > Dockerfile | tar -cf - Dockerfile | gzip -9 | kubectl run kaniko-executor --rm --stdin=true --image=registry.cn-hangzhou.aliyuncs.com/weiyigeek/kaniko-executor:latest --restart=Never --overrides='{ "apiVersion": "v1", "spec": { "containers": [ { "name": "kaniko-executor", "image": "registry.cn-hangzhou.aliyuncs.com/weiyigeek/kaniko-executor:latest", "stdin": true, "stdinOnce": true, "args": [ "--dockerfile=Dockerfile", "--context=tar://stdin", "--destination=docker.io/weiyigeek/alpine:v4.2" ], "volumeMounts": [ { "name": "kaniko-secret", "mountPath": "/kaniko/.docker/" } ] } ], "nodeSelector": { "kubernetes.io/hostname": "weiyigeek-226" }, "volumes": [ { "name": "kaniko-secret", "secret": { "secretName": "dockerhub", "items": [{"key":".dockerconfigjson", "path": "config.json"}] } }, { "name": "dockerfile-storage", "hostPath": { "path": "/storage/dev/soft/kaniko/demo", "type": "DirectoryOrCreate" } }, { "name": "kaniko-cache", "hostPath": { "path": "/storage/dev/soft/kaniko/cache", "type": "DirectoryOrCreate" } } ] } }'
执行结果:
INFO[0003] Retrieving image manifest alpine INFO[0003] Retrieving image alpine from registry index.docker.io INFO[0009] Built cross stage deps: map[] INFO[0009] Retrieving image manifest alpine INFO[0009] Returning cached image manifest INFO[0009] Executing 0 build triggers INFO[0009] Building stage 'alpine' [idx: '0', base-idx: '-1'] INFO[0009] Unpacking rootfs as cmd RUN echo "created from standard input" requires it. INFO[0036] RUN echo "created from standard input" INFO[0036] Initializing snapshotter ... INFO[0036] Taking snapshot of full filesystem... INFO[0036] Cmd: /bin/sh INFO[0036] Args: [-c echo "created from standard input"] INFO[0036] Running: [/bin/sh -c echo "created from standard input"] created from standard input INFO[0036] Taking snapshot of full filesystem... INFO[0037] No files were changed, appending empty layer to config. No layer added to image. INFO[0037] Pushing image to docker.io/weiyigeek/alpine:v4.2 INFO[0042] Pushed index.docker.io/weiyigeek/alpine@sha256:0ef53bcc0a6f261124e5f292fa17041d7e5f81f5542802b89c249351597167e4 pod "kaniko-executor" deleted
至此在 K8s 集群中使用 kaniko 构建镜像简单演示结束。
小试牛刀之在Containerd.io中构建并发布镜像
描述:当我们的环境中只安装了containerd.io 容器运行时没有 Docker 或者 Kubernetes 环境时,我们也可以采用kaniko进行镜像构建与发布,具体操作流程步骤如下:
环境说明
$ lsb_release -a Distributor ID: Ubuntu Description: Ubuntu 20.04.3 LTS Release: 20.04 Codename: focal $ containerd -v containerd containerd.io 1.4.12 7b11cfaabd73bb80907dd23182b9347b4245eb5d $ ctr -v ctr containerd.io 1.4.12
温馨提示: 此处使用的是 Ubuntu 20.04 操作系统, 该系统已做安全加固和内核优化符合等保2.0要求【SecOpsDev/Ubuntu-InitializeSecurity.sh at master · WeiyiGeek/SecOpsDev 】, 如你的Linux未进行相应配置环境可能与读者有些许差异, 如需要进行(windows server、Ubuntu、CentOS)安全加固请参照如下加固脚本进行加固, 请大家疯狂的star 。
加固脚本地址:【 https://github.com/WeiyiGeek/SecOpsDev/blob/master/OS-操作系统/Linux/Ubuntu/Ubuntu-InitializeSecurity.sh 】
温馨提示:如果你使用的是最新 Ubuntu 22.04 操作系统,并需要对其安全加固和内核优化以满足等保2.0要求可参考如下加固脚本 【https://github.com/WeiyiGeek/SecOpsDev/tree/master/OperatingSystem/Security/Ubuntu】。

操作流程
步骤 01.此处假设你已经安装配置好containerd.io了,如果没有安装配置请参考此篇文章【 1.Containerd容器运行时初识与尝试 - https://blog.weiyigeek.top/2021/6-27-570.html 】,此处不再累述。
步骤 02.验证 containerd.io 服务状态以及提前拉取 kaniko-executor:latest 镜像以加快构建速度,此处将镜像拉到默认的名称空间下。
$ systemctl status containerd.service ● containerd.service - containerd container runtime Loaded: loaded (/lib/systemd/system/containerd.service; enabled; vendor preset: enabled) Active: active (running) since Thu 2022-09-08 11:48:30 CST; 4h 49min ago Docs: https://containerd.io Process: 561811 ExecStartPre=/sbin/modprobe overlay (code=exited, status=0/SUCCESS) Main PID: 561812 (containerd) Tasks: 106 Memory: 4.0G $ ctr -n default images pull registry.cn-hangzhou.aliyuncs.com/weiyigeek/kaniko-executor:latest
步骤 03.准备登录 hub docker 的账号以及密码,你可以按照下述的流程进行生成config.json文件。
mkdir /storage/dev/soft/kaniko/{config,demo1} cd /storage/dev/soft/kaniko/config # 生成认证所需的凭据 # BASE64 编码,注意下述为格式为 你的hub账号:你的hub密码 AUTH=$(echo -n "weiyigeek:password" | base64) # BASE64 解码 echo ${AUTH} | base64 -d # 使用该方法可以解析变量 AUTH (值得注意) cat > config.json <<EOF { "auths": { "https://index.docker.io/v1/": { "auth": "${AUTH}" } } } EOF # 生成结果 cat config.json { "auths": { "https://index.docker.io/v1/": { "auth": "d2VpeWlnZ************AxOQ==" } } }
步骤 04.准备dockerfile文件,此处将busybox:1.35.0镜像重新构建后上传到我的账户下的hub仓库中,该文件示例如下:
cd /storage/dev/soft/kaniko/demo1 tee dockerfile <<'EOF' FROM docker.io/library/busybox:1.35.0 LABEL MAINTAINER=master@weiyigeeek.top BUILDTOOLS=kaniko BUILDENV=containerd.io; ENTRYPOINT ["/bin/sh", "-c", "echo hello,busybox"] EOF
步骤 05.当上述都准备完成后我们便可以执行containerd.io提供的ctr客户端工具直接创建容器,例如如下命令:
ctr -n default run --rm --net-host --env DOCKERHUB=docker.io --mount type=bind,src=/storage/dev/soft/kaniko/config,dst=/kaniko/.docker,options=rbind:ro --mount type=bind,src=/storage/dev/soft/kaniko/demo1,dst=/workspace,options=rbind:rw registry.cn-hangzhou.aliyuncs.com/weiyigeek/kaniko-executor:latest kaniko-executor /kaniko/executor --dockerfile=/workspace/dockerfile --context=dir://workspace --destination=docker.io/weiyigeek/busybox:1.35.0 # 参数说明 -n 指定名称空间 --rm 在退出容器时删除容器 --net-host 使用主机网络 --env 指定容器内部shell变量 --mount 指定挂载到容器内部的本地文件,src是指定宿主机上文件目录路径,而dst是指定容器内部目录。
执行结果:
INFO[0002] Retrieving image manifest docker.io/library/busybox:1.35.0 INFO[0002] Retrieving image docker.io/library/busybox:1.35.0 from registry index.docker.io INFO[0006] Built cross stage deps: map[] INFO[0006] Retrieving image manifest docker.io/library/busybox:1.35.0 INFO[0006] Returning cached image manifest INFO[0006] Executing 0 build triggers INFO[0006] Building stage 'docker.io/library/busybox:1.35.0' [idx: '0', base-idx: '-1'] INFO[0006] Skipping unpacking as no commands require it. INFO[0006] LABEL MAINTAINER=master@weiyigeeek.top BUILDTOOLS=kaniko BUILDENV=containerd.io; INFO[0006] Applying label MAINTAINER=master@weiyigeeek.top INFO[0006] Applying label BUILDTOOLS=kaniko INFO[0006] Applying label BUILDENV=containerd.io; INFO[0006] ENTRYPOINT ["/bin/sh", "-c", "echo hello,busybox"] INFO[0006] Pushing image to docker.io/weiyigeek/busybox:1.35.0 INFO[0010] Pushed index.docker.io/weiyigeek/busybox@sha256:d6ed480cc7864b9e19b40f09263abfad4689a9244a5abeb2e3eaf14a439cc55f
步骤 06.查看上传到docker hub中 的 busybox:1.35.0 镜像信息以及拉取到本地进行运行测试验证。
ctr -n default images pull docker.io/weiyigeek/busybox:1.35.0 ctr -n default run --rm docker.io/weiyigeek/busybox:1.35.0 busybox hello,busybox

至此,在containerd.io 环境中,进行镜像构建并发布到hub中实践完毕!
小试牛刀之在Docker中使用kaniko构建并发布镜像
描述:前面说到kaniko的出现实际是为了在没有docker环境的情况之下,按照 Dockerfile 文件中的指令进行镜像构建,不过此处还是简单的介绍一下在docker环境中的使用。 (实际情况中不建议如此多此一举)
步骤 01.执行如下命令生成 docker hub 认证票据(存储路径为 ~/.docker/config.json)以及提前拉取 kaniko 项目中 executor:latest 镜像。
docker login -u weiyigeek # Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one. # Login Succeeded docker pull registry.cn-hangzhou.aliyuncs.com/weiyigeek/kaniko-executor:latest
步骤 02.创建 dockerfile 文件其中 FROM 指定 K8S 集群中常用的NFS动态持久卷镜像,我们将其上传到 hub 仓库中的WeiyiGeek账户下面。
mkdir /storage/dev/soft/kaniko/demo2 cd /storage/dev/soft/kaniko/demo2 tee dockerfile <<'EOF' FROM registry.cn-hangzhou.aliyuncs.com/weiyigeek/nfs-subdir-external-provisioner:latest LABEL MAINTAINER=master@weiyigeeek.top BUILDTOOLS=kaniko BUILDENV=docker; EOF
步骤 03.使用如下示例命令进行 kaniko-executor 容器的创建运行,并进行镜像构建并上传到公共的docker hub 仓库中。
docker rm -f kaniko-executor docker run --rm --name kaniko-executor -v $HOME/.docker/:/kaniko/.docker -v /storage/dev/soft/kaniko/demo2:/workspace registry.cn-hangzhou.aliyuncs.com/weiyigeek/kaniko-executor:latest --dockerfile=/workspace/dockerfile --context=dir://workspace --destination=docker.io/weiyigeek/nfs-subdir-external-provisioner:latest
执行结果:
INFO[0002] Retrieving image manifest registry.cn-hangzhou.aliyuncs.com/weiyigeek/nfs-subdir-external-provisioner:latest INFO[0002] Retrieving image registry.cn-hangzhou.aliyuncs.com/weiyigeek/nfs-subdir-external-provisioner:latest from registry registry.cn-hangzhou.aliyuncs.com INFO[0003] Built cross stage deps: map[] INFO[0003] Retrieving image manifest registry.cn-hangzhou.aliyuncs.com/weiyigeek/nfs-subdir-external-provisioner:latest INFO[0003] Returning cached image manifest INFO[0003] Executing 0 build triggers INFO[0003] Building stage 'registry.cn-hangzhou.aliyuncs.com/weiyigeek/nfs-subdir-external-provisioner:latest' [idx: '0', base-idx: '-1'] INFO[0003] Skipping unpacking as no commands require it. INFO[0003] LABEL MAINTAINER=master@weiyigeeek.top BUILDTOOLS=kaniko BUILDENV=docker; INFO[0003] Applying label MAINTAINER=master@weiyigeeek.top INFO[0003] Applying label BUILDTOOLS=kaniko INFO[0003] Applying label BUILDENV=docker; INFO[0003] Pushing image to docker.io/weiyigeek/nfs-subdir-external-provisioner:latest INFO[0012] Pushed index.docker.io/weiyigeek/nfs-subdir-external-provisioner@sha256:4dc0d27b8fa4608c9e2d8a6f2368d2029df32b9b55f96f27a9218a620ea14828
步骤 04.查看上传到docker hub 仓库中的 nfs-subdir-external-provisioner:latest 信息 (https://hub.docker.com/r/weiyigeek/nfs-subdir-external-provisioner) 。

步骤 05.当然我们也可以在安装有docker环境中使用上下文使用标准输入,并采用docker进行创建kaniko-executor容器,从标准输入接收dockerfile文件并进行镜像构建与推送。
mkdir /storage/dev/soft/kaniko/demo3 echo -e 'FROM alpine nRUN echo "created from standard input"' > Dockerfile | tar -cf - Dockerfile | gzip -9 | docker run --interactive -v /storage/dev/soft/kaniko/demo3:/workspace -v $HOME/.docker/:/kaniko/.docker registry.cn-hangzhou.aliyuncs.com/weiyigeek/kaniko-executor:latest --context tar://stdin --destination=docker.io/weiyigeek/alpine:4.2
执行结果: 如果在交互运行期间没有数据管道传输,则需要按Ctrl+D自行发送EOF信号。
INFO[0000] To simulate EOF and exit, press 'Ctrl+D' INFO[0002] Retrieving image manifest alpine INFO[0002] Retrieving image alpine from registry index.docker.io INFO[0005] Built cross stage deps: map[] INFO[0005] Retrieving image manifest alpine INFO[0005] Returning cached image manifest INFO[0005] Executing 0 build triggers INFO[0005] Building stage 'alpine' [idx: '0', base-idx: '-1'] INFO[0005] Unpacking rootfs as cmd RUN echo "created from standard input" requires it. INFO[0008] RUN echo "created from standard input" INFO[0008] Initializing snapshotter ... INFO[0008] Taking snapshot of full filesystem... INFO[0008] Cmd: /bin/sh INFO[0008] Args: [-c echo "created from standard input"] INFO[0008] Running: [/bin/sh -c echo "created from standard input"] created from standard input INFO[0008] Taking snapshot of full filesystem... INFO[0008] No files were changed, appending empty layer to config. No layer added to image. INFO[0008] Pushing image to docker.io/weiyigeek/alpine:4.2 INFO[0016] Pushed index.docker.io/weiyigeek/alpine@sha256:49360dc74ecf57ea94fbec9d7a3b5cf59dfba8aa5e60f8802cc6299e668a3e1e
至此,在 Docker 中使用 kaniko 进行镜像构建与发布实践完毕。
0x0n 参考来源
项目地址: https://github.com/GoogleContainerTools/kaniko
kaniko 图像镜像仓库: gcr.io/kaniko-project/executor
kaniko 国内镜像仓库源:weiyigeek/kaniko-executor:latest 或者 registry.cn-hangzhou.aliyuncs.com/weiyigeek/kaniko-executor:latest
原文地址: https://blog.weiyigeek.top/2022/9-1-684.html
本文至此完毕,更多技术文章,尽情期待下一章节!
【WeiyiGeek Blog 个人博客 - 为了能到远方,脚下的每一步都不能少 】
欢迎各位志同道合的朋友一起学习交流【点击加入交流群】,如文章有误请在下方留下您宝贵的经验知识!
作者主页: 【 https://weiyigeek.top】
博客地址: 【 https://blog.weiyigeek.top 】

专栏书写不易,如果您觉得这个专栏还不错的,请给这篇专栏 【点个赞、投个币、收个藏、关个注,转个发,留个言】(人间六大情),这将对我的肯定,谢谢!。
-
echo "【点个赞】,动动你那粗壮的拇指或者芊芊玉手,亲!"
-
printf("%s", "【投个币】,万水千山总是情,投个硬币行不行,亲!")
-
fmt.Printf("【收个藏】,阅后即焚不吃灰,亲!")
-
console.info("【转个发】,让更多的志同道合的朋友一起学习交流,亲!")
-
System.out.println("【关个注】,后续浏览查看不迷路哟,亲!")
-
cout << "【留个言】,文章写得好不好、有没有错误,一定要留言哟,亲! " << endl;

更多网络安全、系统运维、应用开发、物联网实践、网络工程、全栈文章,尽在 https://blog.weiyigeek.top 之中,谢谢各位看又支持!