如何解决执行我的 docker 容器时出现 ValueError
就业之路,我一直在研究docker。我从 dockerfile
构建了容器,当我运行容器消息时得到 ValueError
,说我写错了网址,但我不知道如何修复。
我从日志中收到此消息:
Traceback (most recent call last):
File "/echo/docker_test.py",line 6,in <module>
def hello():
File "/usr/local/lib/python3.9/site-packages/flask/app.py",line 1315,in decorator
self.add_url_rule(rule,endpoint,f,**options)
File "/usr/local/lib/python3.9/site-packages/flask/app.py",line 98,in wrapper_func
return f(self,*args,**kwargs)
File "/usr/local/lib/python3.9/site-packages/flask/app.py",line 1275,in add_url_rule
rule = self.url_rule_class(rule,methods=methods,**options)
File "/usr/local/lib/python3.9/site-packages/werkzeug/routing.py",line 666,in __init__
raise ValueError("urls must start with a leading slash")
ValueError: urls must start with a leading slash
这是我的 docker_test.py:
from flask import Flask,request
app=Flask(__name__)
@app.route('/hi/')
def hi():
mth=request.method
print("hello world")
return "hello world"
if __name__ == '__main__':
app.run(host='127.0.0.1',port=5000)
'''
Dockerfile:
FROM python:3.9
RUN pip install flask
RUN mkdir /echo
COPY docker_test.py /echo
CMD ["python","/echo/docker_test.py"]
和我的开发环境: 码头工人:20.10.2 蟒蛇:3.9
解决方法
问题
您发布了不同的代码,然后您将其构建到 docker 中:
在您遇到的错误中:
def hello():
但你的代码看起来像:
@app.route('/hi/')
def hi():
所以可能在你有这样的事情之前:
@app.route('hi/')
def hello():
并且错误是由于缺少路由 url 开头的 shash 引起的:
@app.route('hi/')
应该
@app.route('/hi/')
解决方案
再次构建docker镜像:
build docker . -t python_app:latest
docker run -p 5000:5000 python_app:latest
那么一切都应该没问题。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。