Cryptography Python Docker 多阶段构建



我有一个在 docker 容器中运行的 Python 项目,我正在尝试转换为多阶段 docker 构建过程。我的项目依赖于加密包。我的 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 以外的其他目录进行复制?

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

如果您在阿尔卑斯山或只是想自己编译它,那么 密码学需要一个编译器,Python的标头(如果你不是 使用 pypy(,以及 OpenSSL 和 libffi 库的标头 在您的系统上可用。

Alpine 如果您使用的是 Python 2,请将 python3-dev 替换为 python-dev。

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

如果你在使用openssl-dev时遇到错误,你可能不得不使用libressl-dev。

文档可以在这里找到

我希望我的答案会很有用。

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

仅此而已。完整的码头工人多阶段示例

# 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

最新更新