Docker卷绑定会破坏python脚本CLI entry_point

时间:2017-12-20 20:58:24

标签: python docker

好的,所以我有一个相当基本的python <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" xmlns:card_view="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:showIn="@layout/activity_lotto_number_generator"> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/Generator_BannerAd" android:layout_alignParentStart="true"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="5dp" android:layout_marginBottom="10dp"> <android.support.v7.widget.CardView android:id="@+id/Generator_Lines" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="5dp" card_view:cardCornerRadius="2dp" card_view:contentPadding="10dp"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/Generator_LinesHeading" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="Numbers Wanted?" android:textAppearance="@style/TextAppearance.AppCompat.Large" android:textColor="@color/colorPrimary" android:textStyle="bold" /> <TableLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/Generator_LinesHeading"> <TableRow android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/Generator_LinesMinus" android:layout_width="0dp" android:layout_height="100dp" android:layout_gravity="center_vertical|center_horizontal" android:layout_margin="5dp" android:layout_weight="1" android:background="@drawable/button" android:gravity="center_vertical" android:padding="0dp" android:text="-" android:textAlignment="center" android:textColor="@color/colorPrimaryDark" android:textSize="55sp" android:textStyle="bold" /> <TextView android:id="@+id/Generator_LinesAmount" android:layout_width="0dp" android:layout_height="match_parent" android:layout_margin="5dp" android:layout_weight="1" android:gravity="center_vertical|center_horizontal" android:text="1" android:textAlignment="center" android:textColor="@color/colorPrimaryDark" android:textSize="60sp" android:textStyle="bold" /> <Button android:id="@+id/Generator_LinsPlus" android:layout_width="0dp" android:layout_height="100dp" android:layout_gravity="center_vertical|center_horizontal" android:layout_margin="5dp" android:layout_weight="1" android:background="@drawable/button" android:gravity="center_vertical" android:padding="0dp" android:text="+" android:textAlignment="center" android:textColor="@color/colorPrimaryDark" android:textSize="55sp" android:textStyle="bold" /> </TableRow> <TableRow> <Button android:id="@+id/Generator_Generate" android:layout_width="0dp" android:layout_height="50dp" android:layout_margin="5dp" android:layout_weight="1" android:background="@drawable/button" android:text="Generate Numbers" android:textColor="@color/colorPrimaryDark" android:textSize="18sp" /> </TableRow> </TableLayout> <Space android:layout_width="1dp" android:layout_height="10dp" /> </RelativeLayout> </android.support.v7.widget.CardView> <android.support.v7.widget.RecyclerView android:id="@+id/Generator_RecyclerView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/Generator_Lines" android:scrollbars="none"> </android.support.v7.widget.RecyclerView> </RelativeLayout> </ScrollView> <LinearLayout android:id="@+id/Generator_BannerAd" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="vertical" android:layout_marginTop="5dp"></LinearLayout> </RelativeLayout> CLI应用程序,当在容器中运行绑定卷(文件上的实时开发)时,似乎打破了click entry_point setup.py }

运行以下任一命令

$ docker run -it -v $(pwd):/opt/app gdax
$ docker run -it --mount src=$(pwd),target=/opt/app,type=bind gdax

我得到以下

Traceback (most recent call last):
  File "/usr/local/bin/gdax", line 6, in <module>
    from pkg_resources import load_entry_point
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3144, in <module>
    @_call_aside
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3128, in _call_aside
    f(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3157, in _initialize_master_working_set
    working_set = WorkingSet._build_master()
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 666, in _build_master
    ws.require(__requires__)
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 984, in require
    needed = self.resolve(parse_requirements(requirements))
  File "/usr/local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 870, in resolve
    raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'gdax-cli' distribution was not found and is required by the application

Dockerfile

FROM python:3.6

RUN mkdir /opt/app
COPY . /opt/app


WORKDIR /opt/app

RUN pip install --editable .

CMD ["gdax", "--help"]

setup.py

from setuptools import setup


setup(
    name='gdax-cli',
    version='0.1',
    py_modules=['app'],
    install_requires=[
        'click==6.7',
        'gdax==1.0.6'
    ],
    entry_points='''
        [console_scripts]
        gdax=app:cli
    ''',
)

/ usr / local / bin / gdax&lt; - on container

#!/usr/local/bin/python
# EASY-INSTALL-ENTRY-SCRIPT: 'gdax-cli','console_scripts','gdax'
__requires__ = 'gdax-cli'
import re
import sys
from pkg_resources import load_entry_point

if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
    sys.exit(
        load_entry_point('gdax-cli', 'console_scripts', 'gdax')()
    )

如果我没有绑定卷,我可以很好地执行脚本,但是我无法编辑主机上的文件。我非常有信心这与如何绑定&#34;覆盖&#34;这些文件会破坏/usr/local/bin/gdax加载setuptools放置的文件的能力。无论如何都要绕过这个(在容器中调用python app.py之外)?

2 个答案:

答案 0 :(得分:1)

对于有兴趣在开发期间通过entry_points调用Python docker-compose run控制台脚本的人。将.egg-info文件夹从正在运行的容器中复制到已装载的主机目录中一次适用于我。

.egg-info目录从正在运行的容器中复制到主机。事先关闭安装,以便不覆盖.egg-info目录。

sudo docker cp <container id>:/path/to/package/in/container/package.egg-info /path/to/mounted/package/on/host/package.egg-info

重新启用安装,现在需要.egg-info文件夹,因此安装的entry_points应该可以运行,您可以运行以下命令:

sudo docker-compose run <service name> <entry_point console script name> [OPT] [ARG]

为了实现这一点,需要在映像中以开发模式安装Python包,就像使用RUN pip install -e /path/to/package/一样,因为否则.egg-info文件将不在符号链接的源代码目录中,I猜测。

当然,替代方案,如@Adam提及,总是直接调用CLI脚本,如:

sudo docker-compose run <service name> python /path/to/package/cli.py [OPT] [ARG]

答案 1 :(得分:0)

在当前目录中以可编辑的方式安装pip时,Python需要存在<package>.egg-info目录才能找到已安装的软件包。

一个人可以将目录“白名单”,以免被docker-compose.yml挂载点覆盖,就像这样:

services:
  myapp:
    image: mafrosis/myapp:dev
    build:
      context: .
    volumes:
      - ./:/app
      - /app/.git
      - /app/myapp.egg-info

在此示例中,容器启动时,.gitmyapp.egg-info目录都将出现。