Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
381 views
in Technique[技术] by (71.8m points)

python - ValueError on executing my docker container

the way for employment, I've been studying docker. I built container from dockerfile, and got ValueError when I run a container message says I wrote wrong urls, but I have no idea how could I fix.

I got this message from log:

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

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

and my development environment: docker : 20.10.2 python : 3.9


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Issue

You posted different code then you build into docker:

In the error you have:

def hello():

But your code looks like:

@app.route('/hi/')
def hi():

So probably before you had sth like this:

@app.route('hi/')
def hello():

And error is caused by missing shash on the beginning of the route url:

@app.route('hi/')

Should be

@app.route('/hi/')

Solution

Build docker image again:

build docker . -t python_app:latest

docker run -p 5000:5000 python_app:latest

Then everything should be fine.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.6k users

...