首页 > 应用开发 > Python > 自定义一个kaniko镜像
工具与资源中心
帮助开发者更加高效的工作,提供围绕开发者全生命周期的工具与资源
https://developer.aliyun.com/tool/?spm=a1z389.11499242.0.0.65452413KFoX5Y&utm_content=g_1000295017
kaniko是一款方便我们从K8S内部构建docker容器的工具,以前我们在CI过程中,使用的是docker-in-docker技术,这种技术最主要的缺陷就是当一台机器上同时运行多个docker build流水线时,会出现阻塞的情况,因为这一批流水线用的是宿主机上的同一个docker进程。
基于这种情况,我们在droneCI流水线中换用了kaniko来进行docker镜像的创建。
Dockerfile如下:
FROM heiheidoc/kaniko-project-executor:v1.3.0 AS plugin # 1.6.0的clean up有问题 FROM heiheidoc/kaniko-project-executor:debug AS debug FROM python:3.9.5-buster COPY --from=背景plugin /kaniko /kaniko COPY --from=debug /busybox /kaniko/busybox ADD . /kaniko ENV DOCKER_CONFIG /kaniko/.docker CMD ["python3","/kaniko/main.py"]
部分python代码如下,功能是按照一定规则生成Docker镜像:
def run_shell(shell):
print_green(shell)
cmd = subprocess.Popen(shell, stdin=subprocess.PIPE, stderr=sys.stderr, close_fds=True,
stdout=sys.stdout, universal_newlines=True, shell=True,executable='/kaniko/busybox/sh', bufsize=1)
cmd.communicate()
return cmd.returncode
def run_executor():
for folder_name, sub_dir, files in os.walk(os.getcwd()):
if 'Dockerfile' in files:
Dockefile_path = folder_name + "/Dockerfile"
docker_info = folder_name.replace(os.getcwd(),'').split('/')
harbor_image_name = REGISTRY_URL + "/" + owner_name + "/" + docker_info[1] + ":" + docker_info[2]
cmd_build = "/kaniko/executor --cache=true --cache-dir=/cache --cleanup --skip-tls-verify --skip-tls-verify-pull --skip-tls-verify-registry" \
" --dockerfile=" + Dockefile_path + \
" --context=dir://" + folder_name + \
" --destination=" + harbor_image_name
assert run_shell(cmd_build) == 0, "镜像build失败: " + harbor_image_name
if __name__ == "__main__":
run_executor()
原文链接:https://developer.aliyun.com/article/792006?spm=a2c6h.12873581.0.0.6b4c767dUO7Yup&groupCode=othertech
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70003733/viewspace-2793464/ ,如需转载,请注明出处,否则将追究法律责任。