【问题标题】:Trying to pip install a private repo in a DockerFile尝试在 DockerFile 中安装私有仓库
【发布时间】:2023-03-20 14:58:01
【问题描述】:

我正在尝试安装一个自定义 Python 包以在 Flask 服务器中运行。服务器将位于 Docker 映像中。因此,我正在尝试对RUN pip install git+ssh://git@bitbucket.org:teamName/reponame.git@dev#egg=packageName 进行操作 但是,我尝试过的任何方法都不起作用。

我尝试了找到的两种格式:

1)git+ssh://git@bitbucket.org:teamName/reponame.git@dev#egg=packageName

2)git+ssh://bitbucket.org/team/reponame.git@dev#egg=packageName

这两种技术都给出了类似的错误:

fatal: Could not read from remote repository.

  Please make sure you have the correct access rights
  and the repository exists.

ssh: Could not resolve hostname bitbucket.org:TeamName: Name does not resolve
  fatal: Could not read from remote repository. 

root@bitbucket.org: Permission denied (publickey).
  fatal: Could not read from remote repository.

即使我的公钥是在 BitBucket 中设置的

这里是 Dockerfile:

 Use an official Python runtime as a parent image
FROM python:3.6-alpine

#Preparation to pull from Github
ARG SSH_PRIVATE_KEY

RUN echo "Oh dang look at that ${SSH_PRIVATE_KEY}"

RUN apk update
RUN apk add --no-cache openssh \
    git

RUN mkdir /root/.ssh/
RUN echo "${SSH_PRIVATE_KEY}" > /root/.ssh/id_rsa

RUN chmod 600 /root/.ssh/id_rsa


RUN touch /root/.ssh/known_hosts
RUN ssh-keyscan bitbucket.org >> /root/.ssh/known_hosts

#install dependencies
RUN apk add --no-cache gcc \
    bash \
    tzdata \
    g++ \
    tiff-dev \
    openssl \
    poppler \
    poppler-dev \
    poppler-utils \
    && pip install --trusted-host pypi.python.org <THE_URL>
    && cp /usr/share/zoneinfo/America/that_place /etc/localtime \
    && echo "America/that_place" >  /etc/timezone \
    && date

# Set the working directory to /app
WORKDIR ./my_dir

# Make port 5000 available to the world outside this container
EXPOSE 5000

#Remove SSH
RUN rm /root/.ssh/id_rsa

# Define environment variable
ENV NAME __main__
ENV FLASK_APP app/app.py
ENV FLASK_RUN_HOST 0.0.0.0
ENV GOOGLE_APPLICATION_CREDENTIALS ./resources/google/credentials.json
ENV GOOGLE_CLOUD_BUCKET_NAME bucket_name

# Run app.py when the container launches
CMD ["flask", "run"]

SSH 密钥作为参数传递给带有$(cat ./ssh/id_rsa) 的构建

【问题讨论】:

    标签: python git docker ssh pip


    【解决方案1】:

    您不希望以这种方式传递 SSH 密钥:它最终会出现在生成的图像中,因此有权访问该图像的任何人都可以访问您的 SSH 密钥。

    选项:

    1. 使用 BuildKit,它具有内置的 SSH 代理转发 (https://docs.docker.com/develop/develop-images/build_enhancements/#using-ssh-to-access-private-data-in-builds)。
    2. 我在这里描述的技术,太复杂,无法在简短的回答范围内涵盖:https://pythonspeed.com/articles/docker-build-secrets/
    3. 如果您不担心泄露您的私人 SSH 密钥,请修复此设置。我猜你还需要chmod 700 /root/.ssh

    【讨论】:

    • SSH 密钥是部署“只读”部署 ssh 密钥。所以不是太戏剧化,但仍然宁愿不泄露它。修复此设置是什么意思?
    • 为了让你给出的例子有效,我怀疑chmod 700 /root/.ssh 会成功。
    猜你喜欢
    • 2020-09-23
    • 2023-02-22
    • 1970-01-01
    • 2021-11-04
    • 1970-01-01
    • 1970-01-01
    • 2022-08-11
    • 1970-01-01
    相关资源
    最近更新 更多