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

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

温馨提示: kaniko不是谷歌官方发布支持的产品.
由于 kaniko 不依赖于 Docker 守护进程,并且完全在用户空间中执行 Dockerfile 中的每个命令,这使得能够在轻松或安全地运行在
无Docker守护程序的环境(如标准Kubernetes集群 V1.24.x)中构建容器映像。
在 Kubernetes V1.24.x 版本之后默认采用 containerd.io 作为缺省的cri,不在支持 docker-shim 意味着我们不需要安装 docker 环境
kaniko 执行器镜像负责从 Dockerfile 构建镜像并将其推送到注册表,其流程大致如下:
描述: kaniko 的构建上下文与您发送 Docker 守护程序以进行映像构建的构建上下文非常相似;它代表一个包含 Dockerfile 的目录,kaniko 将使用它来构建您的图像。
例如, Dockerfile 中的 COPY 命令应该引用构建上下文中的文件, 所以您需要将构建上下文存储在 kaniko 可以访问的位置。
目前kaniko 支持以下存储解决方案:
运行 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 会检查层的缓存,如果存在 kaniko将拉取并提取缓存层,而不是执行命令。如果没有 kaniko将执行命令,然后将新创建的层推送到缓存。
用户可以通过设置--cache=true标志选择缓存,并且可以通过--cache-repo标志提供用于存储缓存层的远程存储库, 如果未提供此标志则将从提供的--destination推断缓存的repo。
温馨提示: 在缓存未命中后,kaniko无法从缓存中找到读取层,所有后续层都将在本地构建,而无需咨询缓存。
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>
描述: 此处我们准备在一个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 容器运行时没有 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中实践完毕!
描述:前面说到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 进行镜像构建与发布实践完毕。
项目地址: 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 之中,谢谢各位看又支持!
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev
我正在编写一个包含C扩展的gem。通常当我写一个gem时,我会遵循TDD的过程,我会写一个失败的规范,然后处理代码直到它通过,等等......在“ext/mygem/mygem.c”中我的C扩展和在gemspec的“扩展”中配置的有效extconf.rb,如何运行我的规范并仍然加载我的C扩展?当我更改C代码时,我需要采取哪些步骤来重新编译代码?这可能是个愚蠢的问题,但是从我的gem的开发源代码树中输入“bundleinstall”不会构建任何native扩展。当我手动运行rubyext/mygem/extconf.rb时,我确实得到了一个Makefile(在整个项目的根目录中),然后当
这是一道面试题,我没有答对,但还是很好奇怎么解。你有N个人的大家庭,分别是1,2,3,...,N岁。你想给你的大家庭拍张照片。所有的家庭成员都排成一排。“我是家里的friend,建议家庭成员安排如下:”1岁的家庭成员坐在这一排的最左边。每两个坐在一起的家庭成员的年龄相差不得超过2岁。输入:整数N,1≤N≤55。输出:摄影师可以拍摄的照片数量。示例->输入:4,输出:4符合条件的数组:[1,2,3,4][1,2,4,3][1,3,2,4][1,3,4,2]另一个例子:输入:5输出:6符合条件的数组:[1,2,3,4,5][1,2,3,5,4][1,2,4,3,5][1,2,4,5,3][
我已经构建了一些serverspec代码来在多个主机上运行一组测试。问题是当任何测试失败时,测试会在当前主机停止。即使测试失败,我也希望它继续在所有主机上运行。Rakefile:namespace:specdotask:all=>hosts.map{|h|'spec:'+h.split('.')[0]}hosts.eachdo|host|begindesc"Runserverspecto#{host}"RSpec::Core::RakeTask.new(host)do|t|ENV['TARGET_HOST']=hostt.pattern="spec/cfengine3/*_spec.r
我们的git存储库中目前有一个Gemfile。但是,有一个gem我只在我的环境中本地使用(我的团队不使用它)。为了使用它,我必须将它添加到我们的Gemfile中,但每次我checkout到我们的master/dev主分支时,由于与跟踪的gemfile冲突,我必须删除它。我想要的是类似Gemfile.local的东西,它将继承从Gemfile导入的gems,但也允许在那里导入新的gems以供使用只有我的机器。此文件将在.gitignore中被忽略。这可能吗? 最佳答案 设置BUNDLE_GEMFILE环境变量:BUNDLE_GEMFI
这似乎非常适得其反,因为太多的gem会在window上破裂。我一直在处理很多mysql和ruby-mysqlgem问题(gem本身发生段错误,一个名为UnixSocket的类显然在Windows机器上不能正常工作,等等)。我只是在浪费时间吗?我应该转向不同的脚本语言吗? 最佳答案 我在Windows上使用Ruby的经验很少,但是当我开始使用Ruby时,我是在Windows上,我的总体印象是它不是Windows原生系统。因此,在主要使用Windows多年之后,开始使用Ruby促使我切换回原来的系统Unix,这次是Linux。Rub
我在使用自定义RailsFormBuilder时遇到了问题,从昨天晚上开始我就发疯了。基本上我想对我的构建器方法之一有一个可选block,以便我可以在我的主要content_tag中显示其他内容。:defform_field(method,&block)content_tag(:div,class:'field')doconcatlabel(method,"Label#{method}")concattext_field(method)capture(&block)ifblock_given?endend当我在我的一个Slim模板中调用该方法时,如下所示:=f.form_field:e
我有一个像这样的ruby类:require'logger'classTdefdo_somethinglog=Logger.new(STDERR)log.info("Hereisaninfomessage")endend测试脚本行如下:#!/usr/bin/envrubygem"minitest"require'minitest/autorun'require_relative't'classTestMailProcessorClasses当我运行这个测试时,out和err都是空字符串。我看到消息打印在stderr上(在终端上)。有没有办法让Logger和capture_io一起玩得
我正在构建一个小部件来显示奥运会的奖牌数。我有一个“国家”对象的集合,其中每个对象都有一个“名称”属性,以及奖牌计数的“金”、“银”、“铜”。列表应该排序:1.首先是奖牌总数2.如果奖牌相同,按类型分割(金>银>铜,即2金>1金+1银)3.如果奖牌和类型相同,则按字母顺序子排序我正在用ruby做这件事,但我想语言并不重要。我确实找到了一个解决方案,但如果感觉必须有更优雅的方法来实现它。这是我做的:使用加权奖牌总数创建一个虚拟属性。因此,如果他们有2个金牌和1个银牌,加权总数将为“3.020100”。1金1银1铜为“3.010101”由于我们希望将奖牌数排序为最高的,因此列表按降序排