{"id":148,"date":"2020-12-02T21:14:39","date_gmt":"2020-12-02T12:14:39","guid":{"rendered":"http:\/\/localhost:8000\/?p=148"},"modified":"2021-01-11T21:28:32","modified_gmt":"2021-01-11T12:28:32","slug":"gitlab-ci-kaniko-docker-build-gcr","status":"publish","type":"post","link":"http:\/\/localhost:8000\/2020\/12\/gitlab-ci-kaniko-docker-build-gcr.html","title":{"rendered":"GitLab CI + kaniko \u3067Docker\u30a4\u30e1\u30fc\u30b8\u3092\u30d3\u30eb\u30c9"},"content":{"rendered":"
\u696d\u52d9\u3067\u306fGIT\u30ea\u30dd\u30b8\u30c8\u30ea\u3068\u3057\u3066GitLab\u3092\u4f7f\u3063\u3066\u308b\u306e\u3067\u3059\u304c\u3001\u30ed\u30fc\u30ab\u30ebPC\u3067Docker\u30a4\u30e1\u30fc\u30b8\u3092\u4f5c\u6210\u3059\u308b\u904b\u7528\u3060\u3068<\/p>\n
\u306a\u3069\u306e\u554f\u984c\u304c\u3042\u308a\u5fae\u5999\u3060\u3063\u305f\u306e\u3067\u3001GitLab\u306b\u30bf\u30b0\uff081.5.2\u306a\u3069\uff09\u3092\u30d7\u30c3\u30b7\u30e5\u3057\u305f\u30bf\u30a4\u30df\u30f3\u30b0\u3067\u3001\u81ea\u52d5\u7684\u306bDocker\u30a4\u30e1\u30fc\u30b8\u3092\u4f5c\u6210\u3057\u3066GCR\u306b\u30d3\u30eb\u30c9\u3055\u305b\u308b\u3053\u3068\u306b\u3057\u307e\u3057\u305f\u3002 \u6539\u3081\u3066GitLab\u306e \u30c9\u30ad\u30e5\u30e1\u30f3\u30c8<\/a> \u3092\u898b\u3066\u307f\u308b\u3068\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u66f8\u3044\u3066\u3042\u308a\u307e\u3059\u3002<\/p>\n There are three methods to enable the use of docker build and docker run during jobs, each with their own tradeoffs. \u3069\u3046\u3084\u30893\u3064\u306e\u65b9\u6cd5\u304c\u3042\u308b\u3051\u3069\u3069\u308c\u3082\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u306e\u554f\u984c\u304c\u3042\u308b\u306e\u3067\u3001\u305d\u308c\u3092\u907f\u3051\u308b\u306a\u3089 kaniko\u3092\u4f7f\u3046<\/a> \u306e\u304c\u304a\u3059\u3059\u3081\u3063\u3066\u3053\u3068\u306e\u3088\u3046\u3067\u3059\u3002<\/p>\n \u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u306e\u554f\u984c\u3068\u3044\u3046\u306e\u306f\u3001Docker\u30b3\u30f3\u30c6\u30ca\u306e\u4e2d\u3067Docker\u30a4\u30e1\u30fc\u30b8\u3092\u4f5c\u6210\u3059\u308b\u305f\u3081\u306b\u306f\u3001\u30b3\u30f3\u30c6\u30ca\u5185\u304b\u3089\u30db\u30b9\u30c8\u30de\u30b7\u30f3\u306eDocker\u30c7\u30fc\u30e2\u30f3\u306b\u30a2\u30af\u30bb\u30b9\u3067\u304d\u308b\u3088\u3046\u306b\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u305d\u308c\u3092\u3067\u304d\u308b\u3088\u3046\u306b\u3059\u308b\uff08privileged container\u306b\u3059\u308b\uff09\u3068\u30b3\u30f3\u30c6\u30ca\u5185\u304b\u3089\u30db\u30b9\u30c8\u30de\u30b7\u30f3\u306e\u5168\u3066\u306e\u30c7\u30d0\u30a4\u30b9\u306b\u30a2\u30af\u30bb\u30b9\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u3057\u307e\u3046\u3001\u3068\u3044\u3046\u3053\u3068\u307f\u305f\u3044\u3067\u3059\u3002<\/p>\n kaniko\u3067\u3042\u308c\u3070\u3001\u30db\u30b9\u30c8\u30de\u30b7\u30f3\u306eDocker\u30c7\u30fc\u30e2\u30f3\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u5fc5\u8981\u304c\u306a\u3044\u306e\u3067\u5b89\u5168\u3068\u3044\u3046\u3053\u3068\u306a\u306e\u3067\u3001kaniko\u3092\u5229\u7528\u3059\u308b\u3053\u3068\u306b\u3057\u307e\u3057\u305f\u3002<\/p>\n \u30b5\u30f3\u30d7\u30eb\u306e.gitlab-ci.yml\u306e\u5185\u5bb9\u3092\u78ba\u8a8d\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n \u601d\u3063\u305f\u4ee5\u4e0a\u306b\u30b7\u30f3\u30d7\u30eb\u3067\u3001\u4ee5\u4e0b\u306e\u6d41\u308c\u3067\u51e6\u7406\u3055\u308c\u308b\u3088\u3046\u3067\u3059\u3002<\/p>\n \u30b5\u30f3\u30d7\u30eb\u3067\u3060\u3044\u305f\u3044\u3084\u308a\u65b9\u304c\u5206\u304b\u3063\u305f\u306e\u3067\u3001\u6b21\u306f\u5b9f\u969b\u306bGitLab CI\u3067kaniko\u3092\u4f7f\u3063\u3066Docker\u30a4\u30e1\u30fc\u30b8\u3092\u4f5c\u6210\u3057\u3066\u3001GCR\u306bpush\u3057\u3066\u307f\u3088\u3046\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n \u5b9f\u306f\u81ea\u5206\u3067\u3084\u3063\u305f\u3053\u3068\u304c\u306a\u3044\u306e\u3067\u3059\u304c\u3001\u3053\u306e\u8fba\u306e\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8<\/a>\u3092\u898b\u306a\u304c\u3089GitLab Runner\u3092\u8a2d\u5b9a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3071\u3089\u3071\u3089\u3068\u8aad\u3093\u3067\u3044\u304f\u3068\u4ee5\u4e0b\u306e\u4f5c\u696d\u304c\u5fc5\u8981\u306a\u3088\u3046\u3067\u3057\u305f\u3002<\/p>\n \u4eca\u56de\u306f.gitlab-ci.yml\u3067\u5229\u7528\u3059\u308b\u305f\u3081\u3001\u4ee5\u4e0b\u306e\u4e8c\u3064\u306e\u30ab\u30b9\u30bf\u30e0\u5909\u6570\u3092\u767b\u9332\u3057\u307e\u3059\u3002 \u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u76f4\u4e0b\u306b.gitlab-ci.yml\u3092\u4ee5\u4e0b\u306e\u5185\u5bb9\u3067\u4f5c\u6210\u3057\u307e\u3059\u3002\u30b5\u30f3\u30d7\u30eb\u3068\u307b\u307c\u540c\u3058\u3067\u3059\u304c\u3001\u4ee5\u4e0b\u306e\u70b9\u3092\u4fee\u6b63\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n GCR\u4e0a\u306e\u30a4\u30e1\u30fc\u30b8\u540d\u306b\u30ab\u30b9\u30bf\u30e0\u5909\u6570 \u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u5185\u5bb9\u3092\u5143\u306bDerwent\u30a4\u30e1\u30fc\u30b8\u3092\u4f5c\u6210\u3059\u308b\u305f\u3081\u306eDockerfile\u3092\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u76f4\u4e0b\u306b\u4f5c\u6210\u3057\u307e\u3059\u3002\u96d1\u3067\u3059\u304c\u3001ubuntu\u306e\u30a4\u30e1\u30fc\u30b8\u306b\u3001 \u3042\u3068\u306f\u3001Git\u306e\u9069\u5f53\u306acommit\u306b\u30bf\u30b0\u3092\u6253\u3063\u3066\u3001push\u3059\u308b\u3068\u81ea\u52d5\u7684\u306b\u30d1\u30a4\u30d7\u30e9\u30a4\u30f3\u304c\u767b\u9332\u30fb\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002\u5b9f\u969b\u3001\u601d\u3063\u305f\u3088\u308a\u3042\u3063\u3055\u308a\u3068\u52d5\u3044\u3066\u304f\u308c\u307e\u3057\u305f\u3002<\/p>\n \u4e45\u3005\u306bGitLab CI\u3092\u89e6\u308a\u307e\u3057\u305f\u304c\u3001kaniko\u3092\u4f7f\u3063\u3066Docker\u30a4\u30e1\u30fc\u30b8\u3092\u30d3\u30eb\u30c9\u3059\u308b\u90e8\u5206\u306f\u307b\u3068\u3093\u3069\u4f55\u3082\u5f15\u3063\u304b\u304b\u3089\u305a\u3001\u30b5\u30af\u30c3\u3068\u3067\u304d\u307e\u3057\u305f\u3002<\/p>\n \u5b9f\u88c5\u3084\u8a2d\u5b9a\u3082\u975e\u5e38\u306b\u30b7\u30f3\u30d7\u30eb\u3067\u3059\u3057\u3001\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u306e\u554f\u984c\u3082\u306a\u3044\u306e\u3067Docker\u30a4\u30e1\u30fc\u30b8\u3092GitLab CI\u3067\u4f5c\u6210\u3059\u308b\u306a\u3089kaniko\u4e00\u629e\u3067\u554f\u984c\u306a\u3044\u304b\u306a\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":" \u696d\u52d9\u3067\u306fGIT\u30ea\u30dd\u30b8\u30c8\u30ea\u3068\u3057\u3066GitLab\u3092\u4f7f\u3063\u3066\u308b\u306e\u3067\u3059\u304c\u3001\u30ed\u30fc\u30ab\u30ebPC\u3067Docker\u30a4\u30e1\u30fc\u30b8\u3092\u4f5c\u6210\u3059\u308b\u904b\u7528\u3060\u3068 \u30ed\u30fc\u30ab\u30eb\u306e\u5909\u66f4\u304c\u6df7\u3058\u3063\u3066\u3057\u307e\u3046\u53ef\u80fd\u6027\u304c\u3042\u308b \u30a4\u30e1\u30fc\u30b8\u3092\u4f5c\u6210\u3059\u308b\u30bf\u30a4\u30df\u30f3\u30b0\u304c\u3042\u3044\u307e\u3044\u3060\u3057\u3001\u4f5c\u6210\u5fd8\u308c\u304c\u3042\u3063\u305f\u308a\u3059\u308b \u306a\u3069\u306e\u554f\u984c\u304c\u3042\u308a\u5fae\u5999\u3060\u3063\u305f\u306e\u3067\u3001GitLab\u306b\u30bf\u30b0\uff081.5.2\u306a\u3069\uff09\u3092\u30d7\u30c3\u30b7\u30e5\u3057\u305f\u30bf\u30a4\u30df\u30f3\u30b0\u3067\u3001\u81ea\u52d5\u7684\u306bDocker\u30a4\u30e1\u30fc\u30b8\u3092\u4f5c\u6210\u3057\u3066GCR\u306b\u30d3\u30eb\u30c9\u3055\u305b\u308b\u3053\u3068\u306b\u3057\u307e\u3057\u305f\u3002 \u5c11\u3057\u524d\u306b \u4f1a\u793e\u306e\u30d6\u30ed\u30b0 \u3067 kaniko \u3092\u4f7f\u3046\u30b1\u30fc\u30b9\u3092\u7d39\u4ecb\u3057\u3066\u304f\u308c\u3066\u3044\u305f\u306e\u3067\u3001\u3084\u3063\u3066\u307f\u308b\u3053\u3068\u306b\u3057\u307e\u3057\u305f\u3002 GitLab\u3067Docker\u30a4\u30e1\u30fc\u30b8\u3092\u30d3\u30eb\u30c9\u3059\u308b\u65b9\u6cd5\u306e\u5019\u88dc \u6539\u3081\u3066GitLab\u306e \u30c9\u30ad\u30e5\u30e1\u30f3\u30c8 \u3092\u898b\u3066\u307f\u308b\u3068\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u66f8\u3044\u3066\u3042\u308a\u307e\u3059\u3002 There are three methods to enable the use of docker build and docker run duri <\/span>Continue Reading<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[13,14,15],"tags":[],"_links":{"self":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts\/148"}],"collection":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/comments?post=148"}],"version-history":[{"count":1,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts\/148\/revisions"}],"predecessor-version":[{"id":149,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/posts\/148\/revisions\/149"}],"wp:attachment":[{"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/media?parent=148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/categories?post=148"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/localhost:8000\/wp-json\/wp\/v2\/tags?post=148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}
\n\u5c11\u3057\u524d\u306b \u4f1a\u793e\u306e\u30d6\u30ed\u30b0<\/a> \u3067 kaniko<\/a> \u3092\u4f7f\u3046\u30b1\u30fc\u30b9\u3092\u7d39\u4ecb\u3057\u3066\u304f\u308c\u3066\u3044\u305f\u306e\u3067\u3001\u3084\u3063\u3066\u307f\u308b\u3053\u3068\u306b\u3057\u307e\u3057\u305f\u3002<\/p>\nGitLab\u3067Docker\u30a4\u30e1\u30fc\u30b8\u3092\u30d3\u30eb\u30c9\u3059\u308b\u65b9\u6cd5\u306e\u5019\u88dc<\/h2>\n
\n
\nAn alternative to using docker build is to use kaniko. This avoids having to execute a runner in privileged mode.<\/p>\n<\/blockquote>\nkaniko<\/a>\u3068\u306f<\/h2>\n
\n
\u516c\u5f0f\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8<\/a>\u306e\u30b5\u30f3\u30d7\u30eb\u3092\u78ba\u8a8d<\/h2>\n
build:\n stage: build\n image:\n name: gcr.io\/kaniko-project\/executor:debug\n entrypoint: [""]\n script:\n - mkdir -p \/kaniko\/.docker\n - echo "{\\"auths\\":{\\"$CI_REGISTRY\\":{\\"username\\":\\"$CI_REGISTRY_USER\\",\\"password\\":\\"$CI_REGISTRY_PASSWORD\\"}}}" > \/kaniko\/.docker\/config.json\n - \/kaniko\/executor --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR\/Dockerfile --destination $CI_REGISTRY_IMAGE:$CI_COMMIT_TAG\n only:\n - tags<\/code><\/pre>\n
\n
\/kaniko\/.docker<\/code> \u30d5\u30a9\u30eb\u30c0\u3092\u4f5c\u3063\u3066<\/li>\n
config.json<\/code>\u306b\u51fa\u529b\u3057\u3066<\/li>\n
\/kaniko\/executor<\/code>\u30b3\u30de\u30f3\u30c9\u3067\u3001Docker\u30a4\u30e1\u30fc\u30b8\u3092\u4f5c\u6210\u3057\u3066\u30ec\u30b8\u30b9\u30c8\u30ea\u306bpush\u3059\u308b\n
\n
--context<\/code>: \u30d3\u30eb\u30c9\u5185\u5bb9\u3092\u6307\u5b9a\u3002\u3053\u3053\u3067\u306f\u5bfe\u8c61\u306e\u30bf\u30b0\u306b\u304a\u3051\u308b\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u304c\u6307\u5b9a\u3055\u308c\u3066\u3044\u308b<\/li>\n
--dockerfile<\/code>: \u4f7f\u7528\u3059\u308bDockerfile\u306e\u30d1\u30b9\u3092\u6307\u5b9a<\/li>\n
--destination<\/code>: Docker\u30a4\u30e1\u30fc\u30b8\u540d\u3092\u6307\u5b9a<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n
$CI_REGISTRY<\/code>\u306a\u3069\u306e\u74b0\u5883\u5909\u6570\u306f\u3069\u3046\u3084\u3089\u5168\u3066 \u4e8b\u524d\u5b9a\u7fa9\u3055\u308c\u305f\u74b0\u5883\u5909\u6570<\/a> \u306e\u3088\u3046\u3067\u3059\u3002
\nGCR\u3084ECR\u306a\u3069\u306e\u30d7\u30e9\u30a4\u30d9\u30fc\u30c8\u30b3\u30f3\u30c6\u30ca\u30ec\u30b8\u30b9\u30c8\u30ea\u306b\u51fa\u529b\u3059\u308b\u5834\u5408\u306f\u3001\u8a8d\u8a3c\u60c5\u5831\u3092 UI\u304b\u3089\u30ab\u30b9\u30bf\u30e0\u5909\u6570\u3068\u3057\u3066\u8a2d\u5b9a<\/a> \u3057\u3066\u3001\u30b9\u30af\u30ea\u30d7\u30c8\u3067\u53c2\u7167\u3059\u308b\u3053\u3068\u306b\u306a\u308a\u305d\u3046\u3067\u3059\u3002<\/p>\n\u5b9f\u969b\u306b\u3084\u3063\u3066\u307f\u308b<\/h2>\n
GitLab Runner\u306e\u8a2d\u5b9a<\/h3>\n
\n
\n
docker<\/code>\u3092\u9078\u629e\u3059\u308b<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n
\u30ab\u30b9\u30bf\u30e0\u5909\u6570\u3092\u767b\u9332<\/h3>\n
\nGitLab\u7ba1\u7406\u753b\u9762\u306e\u5bfe\u8c61\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3067 Settings > CI\/CD > Variables > Add variable \u3067\u767b\u9332\u3067\u304d\u307e\u3059\u3002<\/p>\n\n
GCR_CREDENTIAL<\/code>: GCP\u30b5\u30fc\u30d3\u30b9\u30a2\u30ab\u30a6\u30f3\u30c8\u306eJSON\u30ad\u30fc\u306e\u4e2d\u8eab\u306eJSON<\/li>\n
GCR_REGISTRY_IMAGE<\/code>: GCR\u4e0a\u306eDocker\u30a4\u30e1\u30fc\u30b8\u540d<\/li>\n<\/ul>\n
.gitlab-ci.yml\u3092\u4f5c\u6210<\/h3>\n
\n
GCR_CREDENTIAL<\/code>\u3092json\u30d5\u30a1\u30a4\u30eb\u306b\u51fa\u529b\u3059\u308b<\/li>\n
GOOGLE_APPLICATION_CREDENTIALS<\/code>\u306b\u51fa\u529b\u3057\u305fjson\u30d5\u30a1\u30a4\u30eb\u306e\u30d1\u30b9\u3092\u8a2d\u5b9a\u3059\u308b<\/li>\n
GCR_REGISTRY_IMAGE<\/code>\u3092\u6307\u5b9a\u3059\u308b<\/p>\n
stages:\n - build_stage\n\nbuild:\n stage: build_stage\n image:\n name: gcr.io\/kaniko-project\/executor:debug\n entrypoint: [""]\n script:\n - echo "$GCR_CREDENTIAL" > $CI_PROJECT_DIR\/service_account_key.json\n - export GOOGLE_APPLICATION_CREDENTIALS=$CI_PROJECT_DIR\/service_account_key.json\n - \/kaniko\/executor --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR\/Dockerfile --destination $GCR_REGISTRY_IMAGE:$CI_COMMIT_TAG\n only:\n - tags<\/code><\/pre>\n<\/li>\n<\/ul>\n
Dockerfile\u306e\u4f5c\u6210<\/h3>\n
src<\/code>\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3092\u30b3\u30d4\u30fc\u3059\u308b\u3060\u3051\u306e\u8d85\u30b7\u30f3\u30d7\u30eb\u306a\u5185\u5bb9\u306b\u3057\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n
FROM ubuntu:20.04\n\nCOPY src .\/src\/<\/code><\/pre>\n
\u30d1\u30a4\u30d7\u30e9\u30a4\u30f3\u3092\u5b9f\u884c<\/h3>\n
\u3055\u3044\u3054\u306b<\/h2>\n