Docker启container遇见:gunicorn 报错 Worker failed to boot. 解决办法

问题背景:

在开发机上构建了一个镜像,然后启动容器,这个容器的status一直在restarting
使用 $ docker logs containerID
来定位是哪里的问题,报错如下:

File "/app/run.py", line 41, in <module>
    engine = RuleEngine(ctx_init)
  File "/app/rule_engine/rule_engine.py", line 17, in __init__
    rule.init(ctx_init)
  File "/app/rules/extract_name_rule.py", line 101, in init
    data = [item.strip() for item in f.readlines()]
  File "/usr/lib64/python3.6/encodings/ascii.py", line 26, in decode
    return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 0: ordinal not in range(128)
............
During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/bin/gunicorn", line 8, in <module>
    sys.exit(run())
  File "/usr/local/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 58, in run
    WSGIApplication("%(prog)s [OPTIONS] [APP_MODULE]").run()
  File "/usr/local/lib/python3.6/site-packages/gunicorn/app/base.py", line 228, in run
    super().run()
  File "/usr/local/lib/python3.6/site-packages/gunicorn/app/base.py", line 72, in run
    Arbiter(self).run()
  File "/usr/local/lib/python3.6/site-packages/gunicorn/arbiter.py", line 229, in run
    self.halt(reason=inst.reason, exit_status=inst.exit_status)
  File "/usr/local/lib/python3.6/site-packages/gunicorn/arbiter.py", line 342, in halt
    self.stop()
  File "/usr/local/lib/python3.6/site-packages/gunicorn/arbiter.py", line 393, in stop
    time.sleep(0.1)
  File "/usr/local/lib/python3.6/site-packages/gunicorn/arbiter.py", line 242, in handle_chld
    self.reap_workers()
  File "/usr/local/lib/python3.6/site-packages/gunicorn/arbiter.py", line 525, in reap_workers
    raise HaltServer(reason, self.WORKER_BOOT_ERROR)
gunicorn.errors.HaltServer: <HaltServer 'Worker failed to boot.' 3>

解决方法:

大致总结一下:
1.代码中有不能编码的文字,说明编译环境有点问题,这个本地是可以运行的,封装到容器后,字符解码不了,docker默认是英文语言状态,中文环境需要另外设置。这个可以再构建Dockerfile中添加中文信息即可解决中文乱码
2.没有打印详细报错信息,光看一串内容,难看出是哪里的错。
在gunicorn命令的时候在后边加上参数 –preload,便可看到详细的报错信息
3.可能是端口冲突,用了被占用的端口。Connection in use,此时就是端口被占用了,如果你之前起过容器,此时会报这个错,解决办法:把之前的容器删除,这样就行了。
不管是啥原因,把能想到的点,都改正了,封装镜像,起服务,问题解决。

原来的:
gunicorn -c gunicorn.py run:app 
现在的:
gunicorn -c gunicorn.py run:app --preload
上一篇:gunicorn的参数详解


下一篇:django+nginx+gunicorn+supervisro部署