{"id":762,"date":"2021-06-25T17:40:57","date_gmt":"2021-06-25T09:40:57","guid":{"rendered":"https:\/\/www.yinyubo.cn\/?p=756"},"modified":"2022-05-16T18:03:14","modified_gmt":"2022-05-16T10:03:14","slug":"kaniko","status":"publish","type":"post","link":"https:\/\/www.yinyubo.com\/?p=762","title":{"rendered":"\u81ea\u5b9a\u4e49\u4e00\u4e2akaniko\u955c\u50cf"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">\u80cc\u666f<\/h1>\n\n\n\n<p>kaniko\u662f\u4e00\u6b3e\u65b9\u4fbf\u6211\u4eec\u4eceK8S\u5185\u90e8\u6784\u5efadocker\u5bb9\u5668\u7684\u5de5\u5177\uff0c\u4ee5\u524d\u6211\u4eec\u5728CI\u8fc7\u7a0b\u4e2d\uff0c\u4f7f\u7528\u7684\u662fdocker-in-docker\u6280\u672f\uff0c\u8fd9\u79cd\u6280\u672f\u6700\u4e3b\u8981\u7684\u7f3a\u9677\u5c31\u662f\u5f53\u4e00\u53f0\u673a\u5668\u4e0a\u540c\u65f6\u8fd0\u884c\u591a\u4e2adocker build\u6d41\u6c34\u7ebf\u65f6\uff0c\u4f1a\u51fa\u73b0\u963b\u585e\u7684\u60c5\u51b5\uff0c\u56e0\u4e3a\u8fd9\u4e00\u6279\u6d41\u6c34\u7ebf\u7528\u7684\u662f\u5bbf\u4e3b\u673a\u4e0a\u7684\u540c\u4e00\u4e2adocker\u8fdb\u7a0b\u3002<br>\u57fa\u4e8e\u8fd9\u79cd\u60c5\u51b5\uff0c\u6211\u4eec\u5728droneCI\u6d41\u6c34\u7ebf\u4e2d\u6362\u7528\u4e86kaniko\u6765\u8fdb\u884cdocker\u955c\u50cf\u7684\u521b\u5efa\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u9047\u5230\u7684\u96be\u9898<\/h1>\n\n\n\n<ol class=\"wp-block-list\"><li>kaniko\u662f\u57fa\u4e8escratch\u6784\u5efa\u7684\uff0c\u91cc\u9762\u6ca1\u6709shell\uff0c\u6240\u4ee5\u60f3\u5728kaniko\u539f\u751f\u955c\u50cf\u91cc\u5728\u8c03\u7528python\u662f\u5f88\u9ebb\u70e6\u7684<\/li><li>kaniko\u521b\u5efadocker\u955c\u50cf\u4f7f\u7528\u7684\u662ffile system\u529f\u80fd\uff0c\u5982\u679c\u60f3\u5728\u4e00\u4e2akaniko\u5bb9\u5668\u91cc\u5148\u521b\u5efaubuntu\u955c\u50cf\uff0c\u518d\u521b\u5efaalpine\u955c\u50cf\uff0c \u662f\u4f1a\u6709\u5404\u79cd\u51b2\u7a81\u95ee\u9898\u7684\uff0c\u9700\u8981\u4f7f\u7528&#8211;cleanup\u529f\u80fd\u3002\u6b64\u529f\u80fd\u4f1a\u6e05\u7a7a\u6587\u4ef6\u7cfb\u7edf\uff0c\u540c\u65f6\u5982\u679c\u6709\u81ea\u5df1\u88c5\u7684shell,\u4e5f\u4f1a\u88ab\u6e05\u7a7a\uff0c\u5bfc\u81f4\u65e0\u6cd5\u518d\u6b21\u4f7f\u7528<\/li><\/ol>\n\n\n\n<h1 class=\"wp-block-heading\">\u89e3\u51b3\u65b9\u6848<\/h1>\n\n\n\n<ol class=\"wp-block-list\"><li>kaniko\u7684\u5173\u952e\u6587\u4ef6\u5176\u5b9e\u662f\/kaniko\u76ee\u5f55\u4e0b\u7684\u54ea\u4e9b \u4e8c\u8fdb\u5236\u6587\u4ef6\uff0c\u5b98\u65b9\u63a8\u8350\u662f\u7528<em>gcr<\/em>.io<em>\/kaniko<\/em>-project\/executor \u955c\u50cf\uff0c\u5176\u5b9e\u6211\u4eec\u53ef\u4ee5\u62f7\u8d1d\u8fd9\u4e2a\/kaniko\u76ee\u5f55\u5230\u6211\u4eec\u81ea\u5df1\u7684\u79c1\u6709\u955c\u50cf<\/li><li>shell\u6ca1\u6709\u7684\u8bdd\uff0c\u6211\u4eec\u53ef\u4ee5\u62f7\u8d1d\u4e00\u4e2abusybox\u8fdb\u53bb\uff0c\u8fd9\u6837\u5c31\u6709shell\u4e86<\/li><li>\u867d\u7136&#8211;cleanup\u4f1a\u6e05\u7a7afile system\uff0c\u4f46\u662f\u6839\u636e\u4ee3\u7801\u91cc\u7684ignorepath\u8bbe\u5b9a\uff0cvolume\u6302\u8f7d\u76ee\u5f55\u548c\/kaniko\u76ee\u5f55\u4f1a\u88ab\u5ffd\u7565\u6389\u3002\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u6709\u4e24\u79cd\u65b9\u5f0f\u9009\u62e9\uff1a\u4e00\u3001\u901a\u8fc7volume\u7684\u65b9\u5f0f\u54e6\u6302\u8f7dbusybox\u548c\u81ea\u5df1\u7684python\u4ee3\u7801\u5230\u79c1\u6709\u955c\u50cf\u91cc\u3002\u4e8c\u3001\u628abusybox\u548cpython\u4ee3\u7801\u52a0\u5165\/kaniko\u76ee\u5f55\u3002<\/li><\/ol>\n\n\n\n<h1 class=\"wp-block-heading\">\u793a\u4f8b\u4ee3\u7801<\/h1>\n\n\n\n<p>Dockerfile\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>FROM heiheidoc\/kaniko-project-executor:v1.3.0 AS plugin\n\n# 1.6.0\u7684clean up\u6709\u95ee\u9898 https:\/\/github.com\/GoogleContainerTools\/kaniko\/issues\/1586\n\nFROM heiheidoc\/kaniko-project-executor:debug AS debug\n\nFROM python:3.9.5-buster\n\nCOPY --from=\u80cc\u666fplugin \/kaniko \/kaniko\n\nCOPY --from=debug \/busybox \/kaniko\/busybox\n\nADD . \/kaniko\n\nENV DOCKER_CONFIG \/kaniko\/.docker\n\nCMD &#91;\"python3\",\"\/kaniko\/main.py\"]\n\n\n<\/code><\/pre>\n\n\n\n<p>\u90e8\u5206python\u4ee3\u7801\u5982\u4e0b\uff0c\u529f\u80fd\u662f\u6309\u7167\u4e00\u5b9a\u89c4\u5219\u751f\u6210Docker\u955c\u50cf\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def run_shell(shell):\n    print_green(shell)\n    cmd = subprocess.Popen(shell, stdin=subprocess.PIPE, stderr=sys.stderr, close_fds=True,\n                           stdout=sys.stdout, universal_newlines=True, shell=True,executable='\/kaniko\/busybox\/sh', bufsize=1)\n    cmd.communicate()\n    return cmd.returncode\ndef run_executor():\n    for folder_name, sub_dir, files in os.walk(os.getcwd()):\n        if 'Dockerfile' in files:\n            Dockefile_path = folder_name + \"\/Dockerfile\"\n            docker_info = folder_name.replace(os.getcwd(),'').split('\/')\n            harbor_image_name = REGISTRY_URL + \"\/\" + owner_name + \"\/\" + docker_info&#91;1] + \":\" + docker_info&#91;2]\n            cmd_build = \"\/kaniko\/executor --cache=true --cache-dir=\/cache --cleanup --skip-tls-verify --skip-tls-verify-pull --skip-tls-verify-registry\" \\\n                        \" --dockerfile=\" + Dockefile_path + \\\n                        \" --context=dir:\/\/\" + folder_name + \\\n                        \" --destination=\" + harbor_image_name\n            assert run_shell(cmd_build) == 0, \"\u955c\u50cfbuild\u5931\u8d25: \" + harbor_image_name\nif __name__ == \"__main__\":\n    run_executor()<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u80cc\u666f kaniko\u662f\u4e00\u6b3e\u65b9\u4fbf\u6211\u4eec\u4eceK8S\u5185\u90e8\u6784\u5efadocker\u5bb9\u5668\u7684\u5de5\u5177\uff0c\u4ee5\u524d\u6211\u4eec\u5728CI\u8fc7\u7a0b\u4e2d\uff0c\u4f7f\u7528\u7684\u662fdock [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,5],"tags":[],"class_list":["post-762","post","type-post","status-publish","format-standard","hentry","category-k8s","category-python"],"_links":{"self":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/762","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=762"}],"version-history":[{"count":12,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/762\/revisions"}],"predecessor-version":[{"id":827,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/762\/revisions\/827"}],"wp:attachment":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=762"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=762"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=762"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}