密码学Python Docker多级构建

时间:2018-03-02 18:14:57

标签: python docker docker-multi-stage-build

我有一个在docker容器中运行的Python项目,我正在尝试转换为多级docker构建过程。我的项目取决于cryptography包。我的Dockerfile包含:

# Base                                                                          
FROM python:3.6 AS base                                                         

RUN pip install cryptography                                                    

# Production                                                                    
FROM python:3.6-alpine                                                          

COPY --from=base /root/.cache /root/.cache                                      

RUN pip install cryptography \                                                  
        && rm -rf /root/.cache                                                  

CMD python

我尝试用例如

构建
docker build -t my-python-app .

此过程适用于我测试过的许多其他Python要求,例如pycryptopsutil,但会为cryptography引发以下错误:

Step 5/6 : RUN pip install cryptography         && rm -rf /root/.cache
 ---> Running in ebc15bd61d43
Collecting cryptography
  Downloading cryptography-2.1.4.tar.gz (441kB)
Collecting idna>=2.1 (from cryptography)
  Using cached idna-2.6-py2.py3-none-any.whl
Collecting asn1crypto>=0.21.0 (from cryptography)
  Using cached asn1crypto-0.24.0-py2.py3-none-any.whl
Collecting six>=1.4.1 (from cryptography)
  Using cached six-1.11.0-py2.py3-none-any.whl
Collecting cffi>=1.7 (from cryptography)
  Downloading cffi-1.11.5.tar.gz (438kB)
    Complete output from command python setup.py egg_info:

        No working compiler found, or bogus compiler options passed to
        the compiler from Python's standard "distutils" module.  See
        the error messages above.  Likely, the problem is not related
        to CFFI but generic to the setup.py of any Python package that
        tries to compile C code.  (Hints: on OS/X 10.8, for errors about
        -mno-fused-madd see http://stackoverflow.com/questions/22313407/
        Otherwise, see https://wiki.python.org/moin/CompLangPython or
        the IRC channel #python on irc.freenode.net.)

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-uyh9_v63/cffi/

显然我希望不必在我的生产映像上安装任何编译器。我是否需要复制/root/.cache以外的其他目录?

2 个答案:

答案 0 :(得分:1)

Alpine没有manylinux轮,所以你需要自己编译。以下是从安装文档中粘贴的。在同一命令中安装和删除构建依赖项,仅将程序包保存到docker映像层。

  

如果你在阿尔卑斯山或者只想自己编译   加密需要一个编译器,Python的标题(如果你不是   使用pypy)和OpenSSL和libffi库的头文件   可在您的系统上使用。

     

如果您使用的是Python 2,则使用python-dev替换python3-dev。

$ sudo apk add gcc musl-dev python3-dev libffi-dev openssl-dev
     

如果openssl-dev出错,可能需要使用libressl-dev。

Docs can be found here

答案 1 :(得分:0)

我希望我的回答会有用。

  1. 您应该使用--user选项在基础阶段通过pip安装密码。示例:RUN pip install --user cryptography。此选项意味着,所有文件都将安装在以下位置的.local目录中: 当前用户的主目录。
  2. COPY --from=base /root/.local /root/.local,因为加密技术安装在/root/.local中。

仅此而已。完整示例docker multistage

# Base                                                                          
FROM python:3.6 AS base                                                         

RUN pip install --user cryptography

# Production
FROM python:3.6-alpine

COPY --from=base /root/.local /root/.local

RUN pip install cryptography \
        && rm -rf /root/.cache

CMD python