在docker容器内导入socketio会失败吗?

时间:2017-11-16 17:03:17

标签: python docker socket.io

我已经构建了一个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

1 个答案:

答案 0 :(得分:2)

不使用Python的 slim 版本, 使用完整 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"]