繁体   English   中英

从 Dockerfile 中的私有 gitlab 存储库安装 python 包

[英]Installing python package from private gitlab repo in Dockerfile

我目前正在尝试从私人 gitlab 存储库安装 python 包。 不幸的是,我遇到了凭据问题。 有没有什么方法可以在不将我的凭据写入 Dockerfile 或将我的个人 ssh 密钥添加到其中的情况下安装这个包?

Dockerfile:

FROM python:3.9.12-buster AS production

RUN apt-get update && apt-get install -y git

COPY ./requirements.txt /app/requirements.txt

RUN pip install -r /app/requirements.txt

要求.txt:

fastapi
uvicorn
cycler~=0.10.0
networkx
python-multipart
git+https://gitlab.private.net/group/private-repo.git@commit_hash#egg=foo

错误信息:

#10 3.760   Cloning https://gitlab.private.net/group/private-repo.git (to revision commit_hash) to /tmp/pip-install-q9wtmf_q/foo_commit_hash     
#10 3.769   Running command git clone --filter=blob:none --quiet https://gitlab.private.net/group/private-repo.git /tmp/pip-install-q9wtmf_q/foo_commit_hash
#10 4.039   fatal: could not read Username for 'https://gitlab.private.net/group/private-repo.git': No such device or address
#10 4.060   error: subprocess-exited-with-error

一般来说,您可以使用多阶段 docker 构建来确保您的凭据不会留在映像中。

在您的情况下,您可能会执行以下操作:

FROM python:3.9.12-buster as download
RUN apt-get update && apt-get install -y git
RUN pip install --upgrade pip wheel
ARG GIT_USERNAME
ARG GIT_PASSWORD

WORKDIR /build
COPY requirements.txt .
# add password to requirements file
RUN sed -i -E "s|gitlab.private.net|$GIT_USERNAME:$GIT_PASSWORD@gitlab.private.net|" requirements.txt

# download dependencies and build wheels to /build/dist
RUN python -m pip wheel -w /build/dist -r requirements.txt

FROM python:3.9.12-buster as production
WORKDIR /app
COPY --from=download /build/dist /wheelhouse
# install dependencies from the wheels created in previous build stage
RUN pip install --no-index /wheelhouse/*.whl

COPY . .
# ... the rest of your dockerfile

在 GitLab CI 中,您可以像这样使用构建命令:

script:
  # ...
  - docker build --build-arg GIT_USERNAME=gitlab-ci-token --build-arg GIT_PASSWORD=$CI_JOB_TOKEN -t $CI_REGISTRY_IMAGE .

然后将构建您的图像,最终图像将不包含您的凭据。 它也会更小,因为您不必安装git :)

作为旁注,您可以通过使用GitLab PyPI 包注册表来稍微简化这一点。

所以我还必须从我的 python 项目的私有包存储库中安装我的依赖项。 这是我用于构建项目的 Dockerfile。

ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
RUN apt-get update &&\
    apt-get install -y binutils libproj-dev gettext gcc libpq-dev python3-dev build-essential python3-pip python3-setuptools python3-wheel python3-cffi libcairo2 libpango-1.0-0 libpangocairo-1.0-0 libgdk-pixbuf2.0-0 libffi-dev shared-mime-info

RUN pip config set global.extra-index-url https://<personal_access_token_name>:<personal_access_token>@gitlab.com/simple/  
# you need to configure pip to pull packages from remote private repository.
# for gitlab you require personal access token to access them with read permissions


COPY . /code/

RUN --mount=type=cache,target=/root/.cache pip install -r requirements.txt

RUN --mount=type=cache,target=/root/.cache pip install -r /code/webapi/requirements.txt

WORKDIR /code/webapi

ENTRYPOINT /code/webapi/entrypoint.sh

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM