{"id":205082,"date":"2025-05-29T12:14:16","date_gmt":"2025-05-29T04:14:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205082\/"},"modified":"2025-05-29T12:14:16","modified_gmt":"2025-05-29T04:14:16","slug":"%e5%a6%82%e4%bd%95%e5%88%9b%e5%bb%ba%e4%ba%91%e6%9e%84%e5%bb%ba%e4%bb%a5%e5%85%81%e8%ae%b8-docker-%e4%bb%8e-artifact-registry-%e4%b8%8b%e8%bd%bd-python-%e5%8c%85","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205082\/","title":{"rendered":"\u5982\u4f55\u521b\u5efa\u4e91\u6784\u5efa\u4ee5\u5141\u8bb8 Docker \u4ece Artifact Registry \u4e0b\u8f7d Python \u5305"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u521b\u5efa\u4e91\u6784\u5efa\u4ee5\u5141\u8bb8 Docker \u4ece Artifact Registry \u4e0b\u8f7d Python \u5305<\/h1>\n<p>\u5c0f\u4f19\u4f34\u4eec\u5bf9\u6587\u7ae0\u7f16\u7a0b\u611f\u5174\u8da3\u5417\uff1f\u662f\u5426\u6b63\u5728\u5b66\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff1f\u5982\u679c\u662f\uff0c\u90a3\u4e48\u672c\u6587\u300a\u5982\u4f55\u521b\u5efa\u4e91\u6784\u5efa\u4ee5\u5141\u8bb8 Docker \u4ece Artifact Registry \u4e0b\u8f7d Python \u5305\u300b\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff0c\u672c\u7bc7\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\u3002\u5728\u4e4b\u540e\u7684\u6587\u7ae0\u4e2d\u4e5f\u4f1a\u591a\u591a\u5206\u4eab\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241123\/173232436467412c0cea5a4.jpg\" class=\"aligncenter\"><\/p>\n<p>google cloud \u7684 artifactregistry \u662f\u4e00\u4e2a\u7528\u4e8e\u7ba1\u7406\u5e94\u7528\u7a0b\u5e8f\u4f9d\u8d56\u9879\u7684\u5f3a\u5927\u5de5\u5177\u3002\u672c\u6307\u5357\u6f14\u793a\u5982\u4f55\u521b\u5efa cloud build \u7ba1\u9053\u4ee5\u4f7f docker \u80fd\u591f\u8bbf\u95ee\u5b58\u50a8\u5728 artifactregistry \u4e2d\u7684 python \u5305\u3002\u901a\u8fc7\u6267\u884c\u4ee5\u4e0b\u6b65\u9aa4\uff0c\u60a8\u53ef\u4ee5\u5b89\u5168\u5730\u7ba1\u7406\u4f9d\u8d56\u9879\u5e76\u7b80\u5316\u90e8\u7f72\u3002<\/p>\n<hr>\n<ol>\n<li> <strong>google cloud \u9879\u76ee<\/strong>\uff1a\u786e\u4fdd\u60a8\u5df2\u8bbe\u7f6e gcp \u9879\u76ee\u3002<\/li>\n<li> <strong>artifactregistry<\/strong>\uff1apython \u5b58\u50a8\u5e93\u5e94\u8be5\u5df2\u7ecf\u5728 artifactregistry \u4e2d\u914d\u7f6e\u3002<\/li>\n<li> <strong>cloud build<\/strong>\uff1a\u4e3a\u60a8\u7684\u9879\u76ee\u542f\u7528 cloud build api\u3002<\/li>\n<li> <strong>\u8eab\u4efd\u9a8c\u8bc1<\/strong>\uff1a\u914d\u7f6e\u670d\u52a1\u5e10\u6237\u6743\u9650\u4ee5\u8bbf\u95ee\u5de5\u4ef6\u6ce8\u518c\u8868\u3002<\/li>\n<\/ol>\n<hr>\n<p>\u4f7f\u7528 gcloud auth \u751f\u6210\u8bbf\u95ee\u4ee4\u724c\uff0c\u8be5\u4ee4\u724c\u5c06\u5141\u8bb8 docker \u6784\u5efa\u8fc7\u7a0b\u901a\u8fc7 artifact registry \u8fdb\u884c\u8eab\u4efd\u9a8c\u8bc1\u3002\u6267\u884c\u6b64\u64cd\u4f5c\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>steps:\n  # generate artifact registry token\n  - name: 'gcr.io\/google.com\/cloudsdktool\/cloud-sdk'\n    entrypoint: bash\n    args:\n      - '-c'\n      - |\n        art=$(gcloud auth print-access-token)\n        echo \"$art\" &gt; \/workspace\/artifact_registry_token\n        echo \"$art\"\n<\/pre>\n<p>\u4ee4\u724c\u751f\u6210\u540e\uff0c\u53ef\u4ee5\u5c06\u5176\u4f5c\u4e3a\u6784\u5efa\u53c2\u6570\u4f20\u9012\u7ed9 docker \u6784\u5efa\u8fc7\u7a0b\u3002\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>  - name: gcr.io\/cloud-builders\/docker\n    id: build\n    env:\n      - 'btf=\/workspace\/artifact_registry_token'\n    entrypoint: bash\n    args:\n      - '-c'\n      - |\n        docker build \\\n          --build-arg artifact_registry_token=$(cat $btf) \\\n          --build-arg project_id=$project_id \\\n          -t test-image:latest \\\n          -f dockerfile .\n<\/pre>\n<p>dockerfile \u914d\u7f6e\u4e3a\u4f7f\u7528\u4ee4\u724c\u4ece artifact registry \u4e0b\u8f7d python \u5305\uff1a<\/p>\n<pre># syntax=docker\/dockerfile:1\n\nfrom python:3.11-slim\n\narg artifact_registry_token\narg project_id\n\n# keeps python from buffering stdout and stderr\nenv pythonunbuffered=1\n\nworkdir \/app\n\nrun pip install --no-cache-dir -r requirements.txt\n\ncopy . .\n\n# install dependencies using the token\nrun pip install \\\n    --index-url https:\/\/pypi.org\/simple \\\n    --extra-index-url https:\/\/oauth2accesstoken:${artifact_registry_token}@us-central1-python.pkg.dev\/${project_id}\/python-packages\/simple\/ \\\n    \"your-package-name==your-package-version\"\n\n# expose the application port\nexpose 8080\n\n# command to run the application\ncmd [\"uvicorn\", \"main:app\", \"--port=8080\", \"--host=0.0.0.0\"]\n<\/pre>\n<p>\u6700\u540e\uff0c\u5b9a\u4e49\u5176\u4ed6\u914d\u7f6e\uff0c\u4f8b\u5982\u673a\u5668\u7c7b\u578b\u3001\u65e5\u5fd7\u8bb0\u5f55\u548c\u66ff\u6362\uff1a<\/p>\n<pre>options:\n  machinetype: e2_highcpu_8\n  substitutionoption: allow_loose\n  logging: cloud_logging_only\nsubstitutions:\n  _package: your-package-name==your-package-version\n  _repository: python-packages\n  _location: us-central1\n  _project_id: your-project-id\n<\/pre>\n<hr>\n<p>\u4e3a\u4e86\u66f4\u597d\u5730\u7ec4\u7ec7\u60a8\u7684\u6784\u5efa\uff0c\u8bf7\u5305\u542b\u6709\u610f\u4e49\u7684\u6807\u7b7e\uff1a<\/p>\n<pre>tags:\n  - gcp-cloud-build\n  - artifact-registry\n  - docker-python-packages\n<\/pre>\n<hr>\n<p>\u6b64\u8bbe\u7f6e\u53ef\u786e\u4fdd cloud build \u4e2d\u7684 docker \u6784\u5efa\u53ef\u4ee5\u4f7f\u7528\u8bbf\u95ee\u4ee4\u724c\u4ece artifact registry \u5b89\u5168\u5730\u63d0\u53d6 python \u4f9d\u8d56\u9879\u3002\u5c06\u63d0\u4f9b\u7684\u914d\u7f6e\u8c03\u6574\u4e3a\u7279\u5b9a\u4e8e\u9879\u76ee\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u4f8b\u5982\u5305\u540d\u79f0\u3001\u5b58\u50a8\u5e93 url \u548c\u90e8\u7f72\u76ee\u6807\u3002 <\/p>\n<p>\u5b9e\u65bd\u6b64\u7ba1\u9053\u5c06\u63d0\u9ad8\u5b89\u5168\u6027\u5e76\u4f7f\u60a8\u7684\u9879\u76ee\u65e0\u7f1d\u4f9d\u8d56\u7ba1\u7406\u3002<\/p>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8e\u6587\u7ae0\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u521b\u5efa\u4e91\u6784\u5efa\u4ee5\u5141\u8bb8 Docke&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-205082","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205082","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=205082"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205082\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205082"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205082"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205082"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}