【问题标题】:Import socketio fails inside a docker containers?在 docker 容器中导入 socketio 失败?
【发布时间】:2017-11-16 17:03:17
【问题描述】:

我已经构建了一个 docker 镜像,每个使用该镜像的容器都会运行一个名为“run.py”的应用程序。

run.py 使用 socket.io:import socketio

docker运行后出现如下错误:

Traceback (most recent call last):
  File "run.py", line 1, in <module>
    import socketio
  File "/usr/local/lib/python2.7/site-packages/socketio/__init__.py", line 8, in <module>
    from .zmq_manager import ZmqManager
  File "/usr/local/lib/python2.7/site-packages/socketio/zmq_manager.py", line 5, in <module>
    import eventlet.green.zmq as zmq
  File "/usr/local/lib/python2.7/site-packages/eventlet/__init__.py", line 10, in <module>
    from eventlet import convenience
  File "/usr/local/lib/python2.7/site-packages/eventlet/convenience.py", line 6, in <module>
    from eventlet.green import socket
  File "/usr/local/lib/python2.7/site-packages/eventlet/green/socket.py", line 21, in <module>
    from eventlet.support import greendns
  File "/usr/local/lib/python2.7/site-packages/eventlet/support/greendns.py", line 69, in <module>
    setattr(dns.rdtypes.IN, pkg, import_patched('dns.rdtypes.IN.' + pkg))
  File "/usr/local/lib/python2.7/site-packages/eventlet/support/greendns.py", line 59, in import_patched
    return patcher.import_patched(module_name, **modules)
  File "/usr/local/lib/python2.7/site-packages/eventlet/patcher.py", line 119, in import_patched
    *additional_modules + tuple(kw_additional_modules.items()))
  File "/usr/local/lib/python2.7/site-packages/eventlet/patcher.py", line 93, in inject
    module = __import__(module_name, {}, {}, module_name.split('.')[:-1])
  File "/usr/local/lib/python2.7/site-packages/eventlet/support/dns/rdtypes/IN/WKS.py", line 24, in <module>
    _proto_udp = socket.getprotobyname('udp')
socket.error: protocol not found

我的 Dockerfile 提供了一个官方的 python 镜像,它安装了下面描述的要求,并在 /etc 中添加了文件协议。最后一步是按照https://github.com/eventlet/eventlet/issues/370 中的建议完成的。

#parent image: official python runtime
FROM python:2.7-slim
#workdir: /app
WORKDIR /app
#copy the current directory contents into the container in the path /app
ADD . /app
ADD protocols /etc
#install packages contained in the text 
RUN pip install -r requirements.txt
#make port 80 available to the world (outside the container)
EXPOSE 8081
#execute a py app
CMD ["python", "run.py"]

要求是:

Netbase
python-socketio
eventlet
socketIO-client

【问题讨论】:

    标签: python docker socket.io


    【解决方案1】:

    不使用 slim 版本的 Python, 使用完整 Python 版本。

    您的 dockerfile 将如下所示:

    #parent image: official python runtime
    FROM python:2.7
    #workdir: /app
    WORKDIR /app
    #copy the current directory contents into the container in the path /app
    ADD . /app
    ADD protocols /etc
    #install packages contained in the text 
    RUN pip install -r requirements.txt
    #make port 80 available to the world (outside the container)
    EXPOSE 8081
    #execute a py app
    CMD ["python", "run.py"]
    

    【讨论】:

      猜你喜欢
      • 2021-03-19
      • 1970-01-01
      • 2021-12-02
      • 2020-02-08
      • 1970-01-01
      • 2023-03-25
      • 2019-06-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多