我正在关注testdriven.io上的微服务课程。在本课程中,我需要使用Docker部署Flask应用程序。要运行该应用程序,我使用Flask-cli,它在启动时给我一个错误:
No such file or directory: '/usr/src/app/manage.py': '/usr/src/app/manage.py'
要部署容器,我使用Docker-compose。我在Windows 10计算机上运行该命令,并使用标准的Python映像(python:3.7.2-alpine
)。我已经尝试更改文件的权限,但这无济于事。
这是我的撰写文件的样子:
version: '3.7'
services:
users:
build:
context: ./services/users
dockerfile: Dockerfile-dev
volumes:
- './services/users:/usr/src/app'
ports:
- 6001:6000
environment:
- FLASK_APP=project/__init__.py
- FLASK_ENV=development
- COMPOSE_CONVERT_WINDOWS_PATHS=1
我的docker文件:
# 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
# i tried to give it the right rights.. but it didn't help
RUN chmod +x /usr/src/app/manage.py
# run server
CMD python manage.py run -h 0.0.0.0
还有我的manage.py
文件,它将启动该应用程序:
#!/usr/bin/python3
# services/users/manage.py
from flask.cli import FlaskGroup
from project import app
cli = FlaskGroup(app)
if __name__ == '__main__':
cli()
所以我希望这将启动应用程序并使它在端口6001上可用,但是却给了我以下错误:
Successfully tagged testdriven-app_users:latest
Recreating testdriven-app_users_1 ... done
Attaching to testdriven-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://0.0.0.0:5000/ (Press CTRL+C to quit)
users_1 | * Restarting with stat
users_1 | Traceback (most recent call last):
users_1 | File "manage.py", line 11, 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 557, 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 771, 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 988, 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 | FileNotFoundError: [Errno 2] No such file or directory: '/usr/src/app/manage.py': '/usr/src/app/manage.py'
testdriven-app_users_1 exited with code 1
答案 0 :(得分:1)
执行脚本时,内核会寻找shebang(#!
)中指定的解释器。提取解释器的位置,然后启动解释器(例如python3
),执行脚本本身。如果找不到解释器二进制文件,则执行失败,并显示No such file or directory
(ENOENT
)错误(根据man 2 execve
):
ERRORS
<...>
ENOENT The file filename or a script or ELF interpreter does not exist,
or a shared library needed for the file or interpreter cannot be found.
您已经在shebang中指定了#!/usr/bin/python3
,但是在您使用的基本映像(python:3.7.2-alpine
中)python3
二进制文件位于不同的位置:
$ docker run -it python:3.7.2-alpine sh
/ # which python3
/usr/local/bin/python3
因此,即使您的应用程序在主机上本地运行(如果是Windows,它甚至可能不在乎shebang),当您将其移至Docker容器时,它也会停止工作。
为避免这种情况,脚本中的爆炸通常不引用解释器本身(#!/usr/bin/python3
),而是引用/usr/bin/env
二进制文件,该二进制文件查找并执行解释器:
#!/usr/bin/env python3
当您通过python3
运行env
时,后者将通过PATH
执行搜索,因此它使您无需依赖python3
二进制文件的特定位置。
最后,您的脚本的正确版本如下:
#!/usr/bin/env python3
# services/users/manage.py
from flask.cli import FlaskGroup
from project import app
cli = FlaskGroup(app)
if __name__ == '__main__':
cli()
答案 1 :(得分:0)
在request.txt中添加werkzeug == 0.14.1可以修复Windows上出现的相同错误。 它似乎是一个werkzeug错误。