【Bug】- Docker for Windows 上面执行 flask 报错 OSError: [Errno 8] Exec format error

运行共享目录里面的代码,出现 OSError: [Errno 8] Exec format error: '/root/code/main.py' 错误(在容器里面其他地方创建文件代码不会出现此问题)

通过以下方式生成容器,并运行代码:

docker run -it -d -p 8000-8010:8000-8010 -p 5000:5000 \
  --mount type=bind,source="${PWD}",target=/root/code --name office_ps office

docker exec -it office_ps /usr/bin/zsh

# docker ps 里面
cd code
python3 main.py

main.py 如下:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return '

Hello World

' if __name__ == "__main__": app.run(host='0.0.0.0', port=5000, debug=True)

错误如下:

 * Serving Flask app "main" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
 * Restarting with stat
Traceback (most recent call last):
  File "main.py", line 10, in 
    app.run(host='0.0.0.0', port=5000, debug=True)
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 943, in run
    run_simple(host, port, self, **options)
  File "/usr/local/lib/python3.6/dist-packages/werkzeug/serving.py", line 988, in run_simple
    run_with_reloader(inner, extra_files, reloader_interval, reloader_type)
  File "/usr/local/lib/python3.6/dist-packages/werkzeug/_reloader.py", line 332, in run_with_reloader
    sys.exit(reloader.restart_with_reloader())
  File "/usr/local/lib/python3.6/dist-packages/werkzeug/_reloader.py", line 176, in restart_with_reloader
    exit_code = subprocess.call(args, env=new_environ, close_fds=False)
  File "/usr/lib/python3.6/subprocess.py", line 267, in call
    with Popen(*popenargs, **kwargs) as p:
  File "/usr/lib/python3.6/subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "/usr/lib/python3.6/subprocess.py", line 1344, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
OSError: [Errno 8] Exec format error: '/root/code/main.py'

解决:

werkzeug 从 0.15.2 降级到 0.14.1

pip3 uninstall werkzeug
pip3 install werkzeug==0.14.1

参考链接:
- Flask CLI throws 'OSError: [Errno 8] Exec format error' when run through docker-compose
- 0.15.0 causes OSError: [Errno 8] Exec format error: in Docker for Windows #1482
- Update Python and Flask usage in Compose tutorial #8609

你可能感兴趣的:(【Bug】- Docker for Windows 上面执行 flask 报错 OSError: [Errno 8] Exec format error)