【问题标题】:How to install custom modules from another repo using docker如何使用 docker 从另一个仓库安装自定义模块
【发布时间】:2018-11-18 17:29:40
【问题描述】:

我在将自定义模块从另一个 repo 安装到这个 project_name 时遇到了一些问题。 当我使用 PyCharm 时,一切都很好。那么,如何在 docker 中使用 ssh 部署密钥从另一个 repo 安装自定义模块?

项目结构:

project_name
|-core
|  |-models
|  |  |- __init__.py
|  |  |- ...py
|  |-start.py
|  |-Dockerfiles
|  |  |- Dockerfile
|  |  |- id_rsa
|  |  |- id_rsa.pub
|  |  |- ssh.config
|  |-start.py
|- config.py 
|- setup.py

Setup.py

from setuptools import setup, find_packages
from os.path import join, dirname

setup(
   name='core',
   version='0.1',
   url='https://gitlab.ru/username/repo_name.git',
   install_requires=['custom_module>=0.1',
                     'sqlalchemy>=1.2.2',
                     'redis>=2.10.0',
                     'hiredis>=0.2',
                     'python-socketio',
                     'aioredis',
                     'psycopg2',
                     'alembic',
                     'flask',
                     'flask-admin',
                     'flask_basicauth'],
  dependency_links=['git+ssh://git@gitlab.ru/username/custom_module.git'],
  include_package_data=True,
  packages=find_packages(),
  entry_points={
     'console_scripts':
         ['core = core.start']
      }
)

Dockerfile:

FROM python:3.6
RUN mkdir -p /var/project_name
RUN mkdir -p ~/.ssh

RUN apt install -y  openssh-client git

COPY . /var/project_name/
COPY Dockerfiles/id_rsa /var/HEAVEN-CORE/id_rsa
COPY Dockerfiles/id_rsa.pub /var/HEAVEN-CORE/id_rsa.pub
COPY Dockerfiles/ssh.config /var/HEAVEN-CORE/ssh.config

RUN cat /var/project_name/id_rsa > ~/.ssh/id_rsa
RUN cat /var/project_name/id_rsa.pub > ~/.ssh/id_rsa.pub
RUN cat /var/project_name/ssh.config > ~/.ssh/config

RUN eval `ssh-agent -s` && chmod 0600 ~/.ssh/id_rsa  && ssh-add 
~/.ssh/id_rsa && cd /var/project_name  && pip3 install 
git+ssh://git@gitlab.ru/username/repo_name.git 

ENV PYTHONPATH $PYTHONPATH:/var/project_name

WORKDIR /var/project_name

我在终端有异常:

Collecting git+ssh://git@gitlab.ru/username/repo_name.git
Cloning ssh://git@gitlab.ru/username/repo_name.git to 
/tmp/pip-req-build-0m86uyex

Warning: Permanently added 'gitlab.ru,62.76.114.78' 
(ECDSA) to the list of known hosts.

Collecting git+ssh://git@gitlab.ru/username/custom_module.git
Cloning ssh://git@gitlab.ru/username/custom_module.git to 
/tmp/pip-req-build-zu7jupsg

Collecting custom_module>=0.1 (from core==0.1)
Could not find a version that satisfies the requirement 
custom_module>=0.1 (from core==0.1) (from versions: )

No matching distribution found for custom_module>=0.1 (from core==0.1) 
ERROR: Service 'core' failed to build: The command '/bin/sh -c eval 
`ssh-agent -s` && chmod 0600 ~/.ssh/id_rsa  && ssh-add ~/.ssh/id_rsa 
&& cd /var/project_name  && pip3 install 
git+ssh://git@gitlab.ru/username/repo_name.git' returned a 
non-zero code: 1

当我想在 Gitlab SSH URL 中使用 pip install git+ssh://git@gitlab.ru:username/repo_name.git 时,这是错误的。错误:无法从远程存储库中读取。

【问题讨论】:

    标签: python-3.x docker docker-compose gitlab


    【解决方案1】:

    尝试将 gitlab 主机密钥回显到已知主机:

    RUN ssh-keyscan gitlab.ru >> ~/.ssh/known_hosts
    

    更多关于https://docs.gitlab.com/ee/ci/ssh_keys/的信息。

    如果您从公共存储库安装模块,您可以尝试使用 https 而不是 ssh:

    pip3 install git+https://gitlab.ru/username/repo_name.git
    

    【讨论】:

    • 它没有帮助。我需要安装私有仓库
    【解决方案2】:

    它可能会帮助别人。我解决了这个问题

    install_requires=['custom_module',
                      ...
                      ],
    dependency_links=['git+ssh://gitlab.skytracking.ru/username/custom_module.git@branch#egg=custom_module-0']
    

    @branch = 这是你的分支(master 或 smth else)。 最后添加 -0,而不是版本。

    添加这个你可以从私人仓库安装自定义模块

    【讨论】:

      【解决方案3】:

      我遇到了类似的问题,但项目结构不同。为了完整起见,我在这里添加我的解决方案。

      如果你有以下文件结构:

      project_name
      |-core
      |  |-models
      |  |  |- __init__.py
      |  |  |- ...py
      |  |-start.py
      |-requierements
      |  |- prod.txt
      |  |- dev.txt
      |- Dockerfile
      |- Makefile
      |- ...
      

      依赖项存储在文件prod.txtdev.txt 上的位置。您还可以在任一 txt 文件中使用以下语句创建对分支的依赖项:

      custom_module @ git+ssh://gitlab.skytracking.ru/username/custom_module.git@branch#egg=custom_module-0

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-12-31
        • 2021-11-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-01
        • 2020-08-14
        相关资源
        最近更新 更多