python - 當通過docker-compose運行時,Flask CLI拋出'OSError:[Errno 8] Exec格式錯誤'




windows (3)

@CristiFati的答案為我工作了一步:

我還必須將EOL從 \r\n 更正為 \n

對不起,我沒有足夠的積分來添加評論,必須打開一個新答案......

我正在運行帶有 自定義腳本 的Flask應用程序。 或者試著,無論如何。

我在Windows 10上,應用程序應該使用以下命令在Linux Docker容器中運行:

docker-compose up api

Docker-compose是 version 1.23.2 。 在dockerfile中, api 服務通過以下命令運行:

command: python manage.py run --host "0.0.0.0" --with-threads

當它試圖啟動時,我看到了異常

OSError: [Errno 8] Exec format error: '/api/manage.py'

我最初認為這將是可怕的Windows Line Endings,再次為我而來,但在我的所有源文件上運行 dos2unix 並沒有解決問題。

我怎樣才能避免這個錯誤?

manage.py

    import click
    from flask.cli import FlaskGroup

    from my_app_api import create_app


    def create_my_app(info):
        return create_app()


    @click.group(cls=FlaskGroup, create_app=create_my_app)
    def cli():
        pass


    if __name__ == "__main__":
        cli()

完全追溯

api_1          | Traceback (most recent call last):
api_1          |   File "manage.py", line 22, in <module>
api_1          |     cli()
api_1          |   File "/usr/local/lib/python3.6/site-packages/click/core.py", line 764, in __call__
api_1          |     return self.main(*args, **kwargs)
api_1          |   File "/usr/local/lib/python3.6/site-packages/flask/cli.py", line 380, in main
api_1          |     return AppGroup.main(self, *args, **kwargs)
api_1          |   File "/usr/local/lib/python3.6/site-packages/click/core.py", line 717, in main
api_1          |     rv = self.invoke(ctx)
api_1          |   File "/usr/local/lib/python3.6/site-packages/click/core.py", line 1137, in invoke
api_1          |     return _process_result(sub_ctx.command.invoke(sub_ctx))
api_1          |   File "/usr/local/lib/python3.6/site-packages/click/core.py", line 956, in invoke
api_1          |     return ctx.invoke(self.callback, **ctx.params)
api_1          |   File "/usr/local/lib/python3.6/site-packages/click/core.py", line 555, in invoke
api_1          |     return callback(*args, **kwargs)
api_1          |   File "/usr/local/lib/python3.6/site-packages/click/decorators.py", line 64, in new_func
api_1          |     return ctx.invoke(f, obj, *args, **kwargs)
api_1          |   File "/usr/local/lib/python3.6/site-packages/click/core.py", line 555, in invoke
api_1          |     return callback(*args, **kwargs)
api_1          |   File "/usr/local/lib/python3.6/site-packages/flask/cli.py", line 438, in run_command
api_1          |     use_debugger=debugger, threaded=with_threads)
api_1          |   File "/usr/local/lib/python3.6/site-packages/werkzeug/serving.py", line 988, in run_simple
api_1          |     run_with_reloader(inner, extra_files, reloader_interval, reloader_type)
api_1          |   File "/usr/local/lib/python3.6/site-packages/werkzeug/_reloader.py", line 332, in run_with_reloader
api_1          |     sys.exit(reloader.restart_with_reloader())
api_1          |   File "/usr/local/lib/python3.6/site-packages/werkzeug/_reloader.py", line 176, in restart_with_reloader
api_1          |     exit_code = subprocess.call(args, env=new_environ, close_fds=False)
api_1          |   File "/usr/local/lib/python3.6/subprocess.py", line 287, in call
api_1          |     with Popen(*popenargs, **kwargs) as p:
api_1          |   File "/usr/local/lib/python3.6/subprocess.py", line 729, in __init__
api_1          |     restore_signals, start_new_session)
api_1          |   File "/usr/local/lib/python3.6/subprocess.py", line 1364, in _execute_child
api_1          |     raise child_exception_type(errno_num, err_msg, err_filename)
api_1          | OSError: [Errno 8] Exec format error: '/api/manage.py'

如果禁用調試模式(不傳遞 debug=True 或設置 FLASK_DEBUG=0 ),則不會使用重新加載器,因此不會發生此問題。 權衡是您不再擁有重新加載器。

if __name__ == "__main__":
    connexion_app.run(host="0.0.0.0", port=constants.API_PORT, debug=True)

最好通過確保標記為可執行文件的文件具有解釋器行來修復此問題,例如 #!/usr/bin/env python3 (來自 share )。


這是Werkzeug 0.15中的一種新行為。 降級到Werkzeug 0.14.1可能有效,但不再支持0.14,因此您最好用其他答案中描述的文件糾正問題。





docker-compose