Python 在阿尔卑斯山的docker中建造zeep

Python 在阿尔卑斯山的docker中建造zeep,python,linux,docker,dockerfile,Python,Linux,Docker,Dockerfile,第一件事:我在docker阿尔卑斯山的图像中努力构建zeep pacakge,但我最终让它工作了。我希望这篇文章能帮助其他人在docker的阿尔卑斯山上的zeep 问题是:在zeep构建开始工作后,我遇到了一个简单的问题:将构建结果复制到新图像。我最终使用了tar和COPY FROM,但我想知道是否有更好的方法将构建结果从构建映像复制到部署映像。我不清楚我是否获得了所有依赖项,但模块加载成功 最后,这是我确定的Dockerfile: FROM alpine AS build-stage RUN

第一件事:我在docker阿尔卑斯山的图像中努力构建zeep pacakge,但我最终让它工作了。我希望这篇文章能帮助其他人在docker的阿尔卑斯山上的zeep

问题是:在zeep构建开始工作后,我遇到了一个简单的问题:将构建结果复制到新图像。我最终使用了
tar
COPY FROM
,但我想知道是否有更好的方法将构建结果从构建映像复制到部署映像。我不清楚我是否获得了所有依赖项,但模块加载成功

最后,这是我确定的Dockerfile:

FROM alpine AS build-stage
RUN apk add python3-dev build-base libxml2-dev libxslt-dev 
RUN pip3 install --upgrade pip
RUN  pip3 install zeep 
RUN cd /usr/lib/python3.7/site-packages/ && tar zcf /packages.tgz *

FROM alpine AS deploy-stage
RUN apk add python3  libxml2 libxslt 
RUN pip3 install --upgrade pip
COPY --from=build-stage /packages.tgz  /
RUN cd /usr/lib/python3.7/site-packages/ && tar zxf /packages.tgz  && rm /packages.tgz
这个问题的下一部分是表达我在让zeep构建时遇到的一些困难。我不是在这里要求分析,而是在这里包含这些错误消息,以使其他有类似问题的人更容易搜索到这篇文章(换句话说,停止阅读这里)

我使用centos的
构建了一个python docker映像,该映像使用了zeep(用于SOAP)。然而,为了缩小图像大小(这是一幅1G的图像),我想使用alpine的
。一旦我迁移到alpine,pip-3安装zeep就会失败

/bin/sh:pip-3:未找到

阿尔卑斯山上的蟒蛇3叫pip
pip3

gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Os -fomit-frame-pointer -g -Os -fomit-frame-pointer -g -Os -fomit-frame-pointer -g -DTHREAD_STACK_SIZE=0x100000 -fPIC -DCYTHON_CLINE_IN_TRACEBACK=0 -Isrc -Isrc/lxml/includes -I/usr/include/python3.7m -c src/lxml/etree.c -o build/temp.linux-x86_64-3.7/src/lxml/etree.o -w
unable to execute 'gcc': No such file or directory
Compile failed: command 'gcc' failed with exit status 1
creating tmp
cc -I/usr/include/libxml2 -c /tmp/xmlXPathInitg55bfhan.c -o tmp/xmlXPathInitg55bfhan.o
unable to execute 'cc': No such file or directory
*********************************************************************************
Could not find function xmlCheckVersion in library libxml2. Is libxml2 installed?
*********************************************************************************
error: command 'gcc' failed with exit status 1
----------------------------------------
ERROR: Command errored out with exit status 1: /usr/bin/python3.7 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-cq2qcz1c/lxml/setup.py'"'"';
__file__='"'"'/tmp/pip-install-cq2qcz1c/lxml/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"',
'"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))'
install --record /tmp/pip-record-nmuulbim/install-record.txt
--single-version-externally-managed --compile Check the logs for full command output.
显然,zeep包的安装需要编译“某物”。据我所知,docker将允许我在一个图像中构建内容并将其复制到另一个图像中。这允许我安装编译器,构建zeep,然后将最终结果复制到我的新映像中,扔掉我用来构建它的所有工具

为了纠正对编译器的需求,我在Dockerfile中添加了行
runapkaddbuildbase

我下一步遇到了这个错误:

 #include "libxml/xpath.h"
          ^~~~~~~~~~~~~~~~
compilation terminated.
*********************************************************************************
Could not find function xmlCheckVersion in library libxml2. Is libxml2 installed?
*********************************************************************************
error: command 'gcc' failed with exit status 1
----------------------------------------
gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Os -fomit-frame-pointer -g -Os -fomit-frame-pointer -g -Os -fomit-frame-pointer -g -DTHREAD_STACK_SIZE=0x100000 -fPIC -DCYTHON_CLINE_IN_TRACEBACK=0 -I/usr/include/libxml2 -Isrc -Isrc/lxml/includes -I/usr/include/python3.7m -c src/lxml/etree.c -o build/temp.linux-x86_64-3.7/src/lxml/etree.o -w
src/lxml/etree.c:97:10: fatal error: Python.h: No such file or directory
#include "Python.h"
让我尝试添加libxml2库。我添加了这一行
运行apk add libxml2
,得到了以下结果:

ERROR: b'/bin/sh: xslt-config: not found\n'
** make sure the development packages of libxml2 and libxslt are installed **
然而,这一行在错误结果中是很高的(并且很容易错过)。因此,根据这一建议,我将上一个
addlibxml2
替换为
runapkaddlibxml2-dev-libxslt-dev
。现在我已经安装了这些库的开发版本,我遇到了以下错误:

 #include "libxml/xpath.h"
          ^~~~~~~~~~~~~~~~
compilation terminated.
*********************************************************************************
Could not find function xmlCheckVersion in library libxml2. Is libxml2 installed?
*********************************************************************************
error: command 'gcc' failed with exit status 1
----------------------------------------
gcc -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Os -fomit-frame-pointer -g -Os -fomit-frame-pointer -g -Os -fomit-frame-pointer -g -DTHREAD_STACK_SIZE=0x100000 -fPIC -DCYTHON_CLINE_IN_TRACEBACK=0 -I/usr/include/libxml2 -Isrc -Isrc/lxml/includes -I/usr/include/python3.7m -c src/lxml/etree.c -o build/temp.linux-x86_64-3.7/src/lxml/etree.o -w
src/lxml/etree.c:97:10: fatal error: Python.h: No such file or directory
#include "Python.h"
看起来我还必须包含python的头文件。我修改了我的
apkadd
命令如下:
运行apkaddpython3devlibxml2devlibxslt dev
。现在,把它放在一起(并结合
apkadd
命令),我有了这个来构建zeep。此时,我的Dockerfile是这样的:

FROM alpine AS build-stage
RUN apk add python3-dev build-base libxml2-dev libxslt-dev 
RUN pip3 install --upgrade pip
RUN  pip3 install zeep 
这将生成一个可用于导入zeep的图像:

$ docker run -it f1ac77dacbda sh
/ # python3 
Python 3.7.5 (default, Oct 17 2019, 12:25:15) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import zeep
>>> 
看起来不错。真正好的消息是,我的图像大小是312MB,低于1.05GB(我原来的centos图像)。现在,我已经构建了zeep,我想放弃所有较大的devel包,只使用运行时。我还需要获取我的zeep包(及其依赖项),并将它们添加到部署映像中

老实说,我不知道如何在不花费大量时间的情况下准确地确定zeep依赖关系。所以,我只想从packages目录复制zeep。我通过以下方式发现了我的包位置:

$ docker run -it 2b53acb67b25 sh
/ # python3 -m site
sys.path = [
    '/',
    '/usr/lib/python37.zip',
    '/usr/lib/python3.7',
    '/usr/lib/python3.7/lib-dynload',
    '/usr/lib/python3.7/site-packages',
]
USER_BASE: '/root/.local' (doesn't exist)
USER_SITE: '/root/.local/lib/python3.7/site-packages' (doesn't exist)
ENABLE_USER_SITE: True
要复制生成结果,我尝试了以下命令:

COPY --from=build-stage /usr/lib/python3.7/site-packages/zeep/ /usr/lib/python3.7/site-packages/zeep/
这复制了我的zeep包,但没有复制其依赖项包:

$ docker run -it 1292276a94da  sh
/ # python3
Python 3.7.5 (default, Oct 17 2019, 12:25:15) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import zeep
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.7/site-packages/zeep/__init__.py", line 1, in <module>
    from zeep.client import CachingClient, Client  # noqa
  File "/usr/lib/python3.7/site-packages/zeep/client.py", line 4, in <module>
    from zeep.settings import Settings
  File "/usr/lib/python3.7/site-packages/zeep/settings.py", line 4, in <module>
    import attr
ModuleNotFoundError: No module named 'attr'
是否有更准确的方法来复制构建结果,捕获所有依赖项,并排除不必要的包(可能有一些包只是为了构建而下载的)