flaskdockerfileflask-cli

Dockerfile cannot run manage.py on Windows 10


Here is my 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"]

This is my manage.py file:

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()

When I ran "docker-compose up", everything is good but this:

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

It seems the the last step (CMD) from the Dockerfile failed. In my local machine, the flask application can be run without any problem. I have no idea why it failed in the "flask.cli" module. It seems it didn't take the "run" argument?

Please help!


Solution

  • The problem you are facing is actually contained in the last line of your stacktrace. I'll try to first shed some light into the error, then give you a solution:

    Meaning of error:

    Possible solution:

    Add a shebang at the beginning of the file, possibly using the full path of your python executable.

    Other related questions:

    OSError: [Errno 8] Exec format error

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