Kaniko

简要概述

需要在容器里面构建镜像,一般有以下 3 中方式:

  1. 通过挂载宿主的运行时 socket;
  2. 通过 Docker-in-Docker 方式;
  3. 使用 buildkit 工具与运行时 socket;

以上方式均需要依赖后端的容器 daemon,可能还需特权模式,这样构建镜像不够便利以及可能存在安全风险。

这里通过 kaniko 在容器运行时构建镜像,而不依赖后端。

镜像地址

  • 官方镜像
gcr.io/kaniko-project/executor:v1.9.1
gcr.io/kaniko-project/executor:v1.9.1-slim
gcr.io/kaniko-project/executor:v1.9.1-debug

默认镜像是 “FROM scratch”,也就是不带任何 “shell”,无法登录调试。 其他 “-slim” 是瘦身精简的镜像,而 “-debug” 是带了 “busybox” 版本的 “shell” 镜像。

  • 国内镜像
registry.cn-hangzhou.aliyuncs.com/kube-image-repo/kaniko:v1.9.1
registry.cn-hangzhou.aliyuncs.com/kube-image-repo/kaniko:v1.9.1-slim
registry.cn-hangzhou.aliyuncs.com/kube-image-repo/kaniko:v1.9.1-debug

国内仅同步 “linux/amd64” 架构。

命令参数

名称默认值描述
build-arg-“MY_VAR=‘value with spaces’”
cachefalseTODO;
cache-dir/cache需配合参数 “–cache=true” 使用
cache-repo-需配合参数 “–cache=true” 使用
cache-copy-layers-TODO;
cache-run-layerstrueTODO;
cache-ttl336h0m0sTODO;
cleanup-完成时清理文件系统内容,不做清理的话,当前容器将会是 Dockerfile 构建后的结果
compressed-cachingtrue压缩缓存层,以减少编译时间,但增加内存使用
context/workspace/指向 Dockerfile 文件路径
context-sub-pathTODO;TODO;
custom-platform同宿主架构自定义构建的架构,如:linux/adm64,类型:docker build –platform
destination-构建的容器镜像全名,可添加多个,上传到指定的仓库
digest-file/dev/termination-log用于接收构建镜像的信息
dockerfileDockerfile使用哪个 Dockerfile 文件构建镜像
force-TODO;
force-build-metadata-TODO;
gitbranch=,single-branch=false,recurse-submodules=false如果编译上下问来自 git 仓库
ignore-path-TODO;
ignore-var-runtrueTODO;
image-fs-extract-retry-重试次数
image-name-tag-with-digest-file-TODO;
image-name-with-digest-file-TODO;
insecurefalse使用 HTTP 协议推送镜像至镜像中心
insecure-pullfalse使用 HTTP 协议拉取镜像
insecure-registry-使用 HTTP 协议的镜像中心列表,可添加多个
kaniko-dir/kaniko指向 kaniko 目录,优先级高于 “KANIKO_DIR” 环境变量
label-设置镜像标签
log-formatcolor日志格式,可取:text, color, json
log-timestamp-日志时间戳
no-push-不上传至镜像中心
no-push-cache-不上传至镜像中心
oci-layout-path-存放 oci 格式的镜像路径
push-retry-上传镜像的重试次数
registry-certificate-使用 tls 认证镜像中心
registry-mirror-用于 pull 镜像使用的 mirror,如:docker.io
reproducible-TODO;
single-snapshot-TODO;
skip-tls-verify-push 镜像跳过 tls 验证
skip-tls-verify-pull-pull 镜像跳过 tls 验证
skip-tls-verify-registry-对指定镜像中心跳过 tls 验证
skip-unused-stages-TODO;
snapshot-modefullTODO;
tar-path-镜像以 tar 格式保存路径
target-TODO;
use-new-run-TODO;
verbosityinfo日志级别:trace, debug, info, warn, error, fatal, panic

应用场景

启动进入 shell

docker run \
	-i -t \
	--rm \
	--entrypoint /bin/sh \
	registry.cn-hangzhou.aliyuncs.com/kube-image-repo/kaniko:v1.9.1-debug

构建容器镜像

假设需要构建 “ccr.ccs.tencentyun.com/opsaid/test2:0.1.0” 镜像,则可使用以下指令:

docker run \
	-v /tmp/coding/test2:/workspace \
	-v /tmp/docker/config.json:/kaniko/.docker/config.json:ro \
	registry.cn-hangzhou.aliyuncs.com/kube-image-repo/kaniko:v1.9.1 \
	--dockerfile /workspace/Dockerfile \
	--context /workspace \
	--destination ccr.ccs.tencentyun.com/opsaid/test2:0.1.0

脚本分析:

  1. 应用代码

/workspace/Dockerfile 内容:

  1. 添加 “ccr.ccs.tencentyun.com/opsaid” 镜像中心权限

见配置:/tmp/docker/config.json

{
	"auths": {
		"ccr.ccs.tencentyun.com": {
			"auth": "base64(username:password)"
		}
	}
}

挂载至 kaniko 容器内 “/kaniko/.docker/config.json” 地址。如果您更改了 “kaniko” 镜像,导致变量 “$HOME” 非 “/kaniko",则需要更改挂载的配置,否则 “/kaniko/executer” 会无法正确读取授权文件。