您开发了一个很棒的Python包供公司内部使用。您希望发布它,以便您的同事可以开始使用它。由于该包仅供内部使用,因此无法在PyPI(官方Python包注册表)上发布它。相反,由于您的公司使用GCP,因此自然的选择是Artifact Registry。
如文档所述,将包发布到注册表非常简单。
我使用Poetry打包库。以下是一些您将使用的命令:
<code class="language-bash">poetry source add --priority=supplemental gcp_registry https://{LOCATION}-python.pkg.dev/{REPO}/{PACKAGE}/ poetry publish --no-interaction --build --repository gcp_registry</code>
将您的包发布到Artifact Registry后,您可以将其作为其他项目的依赖项提供。
要在本地机器上安装包,请创建一个requirements_private.txt文件:
<code>--index-url https://{LOCATION}-python.pkg.dev/{REPO}/{PACKAGE}/simple/ --extra-index-url https://pypi.org/simple {YOUR_PACKAGE_NAME}</code>
然后,使用以下命令安装包:
<code class="language-bash">pip install keyring pip install keyrings.google-artifactregistry-auth pip install -r /opt/requirements_private.txt</code>
keyring包处理Artifact Registry身份验证。确保在继续之前设置了您的应用程序默认凭据(ADC)。
在Docker中运行应用程序时,您会面临其他挑战:
解决方案很简单,但没有很好的文档记录。我花了几天时间才弄清楚这一点,所以我想节省您的时间,并帮助您在几分钟内实现它。
以下是您的Dockerfile的外观:
<code class="language-dockerfile">ARG GOOGLE_APPLICATION_CREDENTIALS COPY requirements_private.txt /opt/requirements_private.txt RUN --mount=type=secret,id=creds,target=/opt/mykey.json,mode=0444 \ pip install keyring && \ pip install keyrings.google-artifactregistry-auth && \ pip install -r /opt/requirements_private.txt COPY requirements.txt /opt/requirements.txt RUN pip install -r /opt/requirements.txt</code>
requirements_private.txt仍然相同。
<code>--index-url https://{LOCATION}-python.pkg.dev/{REPO}/{PACKAGE}/simple/ --extra-index-url https://pypi.org/simple {YOUR_PACKAGE_NAME}</code>
如您所见,您可以有多个requirements文件。在我的例子中,requirements.txt文件用于托管在pypi公共注册表中的包。 然后是您的docker_compose.yml文件
<code class="language-yaml">services: app: build: context: . args: - GOOGLE_APPLICATION_CREDENTIALS=/opt/mykey.json secrets: - creds secrets: creds: file: "C:/your/local/host/path/to/google_service_account.json"</code>
然后您可以运行构建命令:
<code class="language-bash">docker compose build</code>
希望本文能帮助您与Artifact Registry和Docker集成。
以上是如何在Docker文件中从GCP Artifact注册表中安装Python软件包的详细内容。更多信息请关注PHP中文网其他相关文章!