Dockerfile无法在Windows 10上运行manage.py

时间:2019-06-25 06:42:25

标签: flask dockerfile flask-cli

这是我的 Dockerfile

# base image
FROM python:3.7.2-alpine

# set working directory
WORKDIR /usr/src/app

# add and install requirements
COPY ./requirements.txt /usr/src/app/requirements.txt
RUN pip install -r requirements.txt

# add app
COPY . /usr/src/app

# run server
CMD ["python", "manage.py", "run"]

这是我的 manage.py 文件:

from flask.cli import FlaskGroup
from project import app

cli = FlaskGroup(app)

# run and manage the app from the command line
if __name__ == '__main__':
    cli()

当我运行“ docker-compose up”时,一切都很好,但是:

Starting app_users_1 ... done
Attaching to app_users_1
users_1  |  * Serving Flask app "project/__init__.py" (lazy loading)
users_1  |  * Environment: development
users_1  |  * Debug mode: on
users_1  |  * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
users_1  |  * Restarting with stat
users_1  | Traceback (most recent call last):
users_1  |   File "manage.py", line 8, in <module>
users_1  |     cli()
users_1  |   File "/usr/local/lib/python3.7/site-packages/click/core.py", line 764, in __call__
users_1  |     return self.main(*args, **kwargs)
users_1  |   File "/usr/local/lib/python3.7/site-packages/flask/cli.py", line 569, in main
users_1  |     return super(FlaskGroup, self).main(*args, **kwargs)
users_1  |   File "/usr/local/lib/python3.7/site-packages/click/core.py", line 717, in main
users_1  |     rv = self.invoke(ctx)
users_1  |   File "/usr/local/lib/python3.7/site-packages/click/core.py", line 1137, in invoke
users_1  |     return _process_result(sub_ctx.command.invoke(sub_ctx))
users_1  |   File "/usr/local/lib/python3.7/site-packages/click/core.py", line 956, in invoke
users_1  |     return ctx.invoke(self.callback, **ctx.params)
users_1  |   File "/usr/local/lib/python3.7/site-packages/click/core.py", line 555, in invoke
users_1  |     return callback(*args, **kwargs)
users_1  |   File "/usr/local/lib/python3.7/site-packages/click/decorators.py", line 64, in new_func
users_1  |     return ctx.invoke(f, obj, *args, **kwargs)
users_1  |   File "/usr/local/lib/python3.7/site-packages/click/core.py", line 555, in invoke
users_1  |     return callback(*args, **kwargs)
users_1  |   File "/usr/local/lib/python3.7/site-packages/flask/cli.py", line 783, in run_command
users_1  |     threaded=with_threads, ssl_context=cert)
users_1  |   File "/usr/local/lib/python3.7/site-packages/werkzeug/serving.py", line 1007, in run_simple
users_1  |     run_with_reloader(inner, extra_files, reloader_interval, reloader_type)
users_1  |   File "/usr/local/lib/python3.7/site-packages/werkzeug/_reloader.py", line 332, in run_with_reloader
users_1  |     sys.exit(reloader.restart_with_reloader())
users_1  |   File "/usr/local/lib/python3.7/site-packages/werkzeug/_reloader.py", line 176, in restart_with_reloader
users_1  |     exit_code = subprocess.call(args, env=new_environ, close_fds=False)
users_1  |   File "/usr/local/lib/python3.7/subprocess.py", line 323, in call
users_1  |     with Popen(*popenargs, **kwargs) as p:
users_1  |   File "/usr/local/lib/python3.7/subprocess.py", line 775, in __init__
users_1  |     restore_signals, start_new_session)
users_1  |   File "/usr/local/lib/python3.7/subprocess.py", line 1522, in _execute_child
users_1  |     raise child_exception_type(errno_num, err_msg, err_filename)
users_1  | OSError: [Errno 8] Exec format error: '/usr/src/app/manage.py'
app_users_1 exited with code 1

似乎Dockerfile的最后一步(CMD)失败了。在我的本地计算机上,flask应用程序可以正常运行。我不知道为什么它在“ flask.cli”模块中失败。似乎没有采用“运行”参数?

请帮助!

1 个答案:

答案 0 :(得分:0)

您面临的问题实际上包含在您的堆栈跟踪的最后一行中。我将尝试首先弄清错误,然后为您提供解决方案:

错误含义

  • OSError:发生了与您的操作系统(即Windows)有关的错误。
  • Exec format error: '/usr/src/app/manage.py':具体来说,系统无法执行manage.py文件,这很可能是由于文件顶部缺少shebang造成的。

可能的解决方案

在文件的开头添加一个shebang,可能使用python可执行文件的完整路径。

其他相关问题

OSError: [Errno 8] Exec format error

Why not os.path.join use os.path.sep or os.sep?