首页 > 解决方案 > pip install google-cloud-pubsub 在 docker 容器中安装失败

问题描述

我正在尝试使用 pupsub 模拟器。它开始了,但是当我尝试使用我的 python 脚本时,我收到以下错误

ModuleNotFoundError: No module named 'google'

所以我尝试安装模块。

RUN pip install google-cloud-pubsub

错误

ERROR: Command errored out with exit status 1:
     command: /usr/bin/python3.6 -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-2hyoy1ly/grpcio/setup.py'"'"'; __file__='"'"'/tmp/pip-install-2hyoy1ly/grpcio/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-m25l52fe
         cwd: /tmp/pip-install-2hyoy1ly/grpcio/
    Complete output (11 lines):
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-install-2hyoy1ly/grpcio/setup.py", line 191, in <module>
        if check_linker_need_libatomic():
      File "/tmp/pip-install-2hyoy1ly/grpcio/setup.py", line 152, in check_linker_need_libatomic
        stderr=PIPE)
      File "/usr/lib/python3.6/subprocess.py", line 729, in __init__
        restore_signals, start_new_session)
      File "/usr/lib/python3.6/subprocess.py", line 1364, in _execute_child
        raise child_exception_type(errno_num, err_msg, err_filename)
    FileNotFoundError: [Errno 2] No such file or directory: 'cc': 'cc'
    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
ERROR: Service 'praise-pubsub' failed to build: The command '/bin/sh -c pip install google-cloud-pubsub==0.24.0' returned a non-zero code: 1

完整的 Dockerfile

FROM google/cloud-sdk:alpine
RUN gcloud components install pubsub-emulator

FROM openjdk:jre-alpine


ENV PYTHONUNBUFFERED=1

RUN echo "**** install Python ****" && \
    apk add --no-cache python3 && \
    if [ ! -e /usr/bin/python ]; then ln -sf python3 /usr/bin/python ; fi && \
    \
    echo "**** install pip ****" && \
    python3 -m ensurepip && \
    rm -r /usr/lib/python*/ensurepip && \
    pip3 install --no-cache --upgrade pip setuptools wheel && \
    if [ ! -e /usr/bin/pip ]; then ln -s pip3 /usr/bin/pip ; fi

#RUN pip install google-cloud <--- still fails when this is here
#RUN pip install Cython --install-option="--no-cython-compile" <--- still fails
RUN pip install google-cloud-pubsub
COPY --from=0 /google-cloud-sdk/platform/pubsub-emulator /pubsub-emulator

标签: pythondockergoogle-cloud-pubsub

解决方案


我不确定您为什么要尝试安装 python3 和 pip3,它们都存在于基础映像中。在任何情况下,这个 Dockerfile 都会以漂亮干净的图像为您提供 python google-cloud-pubsub 库

FROM google/cloud-sdk:alpine

RUN apk add --no-cache --virtual .build-deps \
    linux-headers build-base g++ python3-dev \
    && pip3 install --no-cache-dir google-cloud-pubsub \
    && apk del .build-deps

# add your stuff here

推荐阅读