在Dockerfile中安装包时出错(使用cron运行pythoscript)

2024-06-06 18:13:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我必须在docker容器中安排python脚本的执行。我的脚本使用了几个必须安装的软件包,因此这是我的Dockerfile:


FROM python:3.9.0b4-alpine3.12
COPY AzureVM /bin/AzureVM
COPY root /var/spool/cron/crontabs/root
RUN pip install azure-common
RUN pip install azure-mgmt-compute
RUN pip install msrestazure
RUN pip install paramiko
RUN chmod +x /bin/AzureVM
CMD crond -l 2 -f

当我尝试构建容器时,一些包会出现巨大错误(更具体地说是msrestazureparamiko)。例如:

Step 6/8 : RUN pip install msrestazure
 ---> Running in f02284bb6aee
Collecting msrestazure
  Downloading msrestazure-0.6.4-py2.py3-none-any.whl (40 kB)
Requirement already satisfied: msrest<2.0.0,>=0.6.0 in /usr/local/lib/python3.9/site-packages (from msrestazure) (0.6.19)
Collecting adal<2.0.0,>=0.6.0
  Downloading adal-1.2.4-py2.py3-none-any.whl (55 kB)
Requirement already satisfied: six in /usr/local/lib/python3.9/site-packages (from msrestazure) (1.15.0)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.9/site-packages (from msrest<2.0.0,>=0.6.0->msrestazure) (2020.6.20)
Requirement already satisfied: requests-oauthlib>=0.5.0 in /usr/local/lib/python3.9/site-packages (from msrest<2.0.0,>=0.6.0->msrestazure) (1.3.0)
Requirement already satisfied: requests~=2.16 in /usr/local/lib/python3.9/site-packages (from msrest<2.0.0,>=0.6.0->msrestazure) (2.24.0)
Requirement already satisfied: isodate>=0.6.0 in /usr/local/lib/python3.9/site-packages (from msrest<2.0.0,>=0.6.0->msrestazure) (0.6.0)
Collecting python-dateutil>=2.1.0
  Downloading python_dateutil-2.8.1-py2.py3-none-any.whl (227 kB)
Collecting PyJWT>=1.0.0
  Downloading PyJWT-1.7.1-py2.py3-none-any.whl (18 kB)
Collecting cryptography>=1.1.0
  Downloading cryptography-3.1.tar.gz (534 kB)
  Installing build dependencies: started
  Installing build dependencies: finished with status 'error'
  ERROR: Command errored out with exit status 1:
   command: /usr/local/bin/python /usr/local/lib/python3.9/site-packages/pip install --ignore-installed --no-user --prefix /tmp/pip-build-env-8e8f308c/overlay --no-warn-script-location --no-binary :none: --only-binary :none: -i https://pypi.org/simple -- 'setuptools>=40.6.0' wheel 'cffi>=1.8,!=1.11.3; platform_python_implementation != '"'"'PyPy'"'"''
       cwd: None
  Complete output (37 lines):
  Collecting setuptools>=40.6.0
    Downloading setuptools-50.3.0-py3-none-any.whl (785 kB)
  Collecting wheel
    Downloading wheel-0.35.1-py2.py3-none-any.whl (33 kB)
  Collecting cffi!=1.11.3,>=1.8
    Downloading cffi-1.14.3.tar.gz (470 kB)
      ERROR: Command errored out with exit status 1:
       command: /usr/local/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-5nw42fq5/cffi/setup.py'"'"'; __file__='"'"'/tmp/pip-install-5nw42fq5/cffi/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-l9k0d28w
           cwd: /tmp/pip-install-5nw42fq5/cffi/
      Complete output (23 lines):
      Traceback (most recent call last):
        File "<string>", line 1, in <module>
        File "/tmp/pip-install-5nw42fq5/cffi/setup.py", line 163, in <module>
          ask_supports_thread()
        File "/tmp/pip-install-5nw42fq5/cffi/setup.py", line 81, in ask_supports_thread
          config.try_compile('__thread int some_threadlocal_variable_42;'))
        File "/usr/local/lib/python3.9/distutils/command/config.py", line 225, in try_compile
          self._compile(body, headers, include_dirs, lang)
        File "/usr/local/lib/python3.9/distutils/command/config.py", line 132, in _compile
          self.compiler.compile([src], include_dirs=include_dirs)
        File "/usr/local/lib/python3.9/distutils/ccompiler.py", line 574, in compile
          self._compile(obj, src, ext, cc_args, extra_postargs, pp_opts)
        File "/usr/local/lib/python3.9/distutils/unixccompiler.py", line 117, in _compile
          self.spawn(compiler_so + cc_args + [src, '-o', obj] +
        File "/usr/local/lib/python3.9/distutils/ccompiler.py", line 910, in spawn
          spawn(cmd, dry_run=self.dry_run)
        File "/usr/local/lib/python3.9/distutils/spawn.py", line 74, in spawn
          proc = subprocess.Popen(cmd, env=env)
        File "/usr/local/lib/python3.9/subprocess.py", line 947, in __init__
          self._execute_child(args, executable, preexec_fn, close_fds,
        File "/usr/local/lib/python3.9/subprocess.py", line 1819, in _execute_child
          raise child_exception_type(errno_num, err_msg, err_filename)
      FileNotFoundError: [Errno 2] No such file or directory: 'gcc'
      ----------------------------------------
  ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
  WARNING: You are using pip version 20.1.1; however, version 20.2.3 is available.
  You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.
  ----------------------------------------
ERROR: Command errored out with exit status 1: /usr/local/bin/python /usr/local/lib/python3.9/site-packages/pip install --ignore-installed --no-user --prefix /tmp/pip-build-env-8e8f308c/overlay --no-warn-script-location --no-binary :none: --only-binary :none: -i https://pypi.org/simple -- 'setuptools>=40.6.0' wheel 'cffi>=1.8,!=1.11.3; platform_python_implementation != '"'"'PyPy'"'"'' Check the logs for full command output.
WARNING: You are using pip version 20.1.1; however, version 20.2.3 is available.
You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.
The command '/bin/sh -c pip install msrestazure' returned a non-zero code: 1

我尝试更改基本映像(slimbusterslim-buster),并安装了所有软件包,但每次都出现以下错误:

Crond not found

当我尝试安排一个简单的脚本(没有任何奇怪的安装)时,它工作得很好(仅适用于python:3.9.0b4-alpine3.12)。我不知道该怎么办,所以请帮忙


Tags: installpipinpynonebinlibusr
1条回答
网友
1楼 · 发布于 2024-06-06 18:13:33

在运行pip命令之前添加此选项

RUN apk add build-base libffi-dev openssl-dev  no-cache

测试如下Dockerfile。您可以相应地添加其余步骤

FROM python:3.9.0b4-alpine3.12
RUN apk add build-base libffi-dev openssl-dev  no-cache
RUN pip install azure-common \
    && pip install azure-mgmt-compute \
    && pip install msrestazure \
    && pip install paramiko

相关问题 更多 >