简体   繁体   中英

Error: connect ECONNREFUSED 127.0.0.1:5000 or Error: socket hang up after GET request to docker container

docker build --tag house. and then: docker run -it -p 5000:5000 house Returns Error: connect ECONNREFUSED 127.0.0.1:5000 when I try to send a GET request via Postman to 127.0.0.1:5000

Also: docker build --tag house. and then: docker run --publish 5000:5000 house Returns Error: socket hang up when I try to send a GET request via Postman to 127.0.0.1:5000

I've also tried localhost:5000 and 0.0.0.0:5000. Is there anything I can do to get the project running?

The entire project repository can be cloned or forked from here (there is def some junk hanging out, but the core files, app.py, Dockerfile, model.py, model.pkl are all there): https://github.com/aliciachen10/wakecountyhousing_final

Below is the code if you want a preliminary look: DOCKERFILE

FROM ubuntu:18.04

RUN apt -y update &&\
    apt -y install python3 python3-pip

ENV PYTHON_VERSION 3.9.4

COPY . .
RUN python3 -m pip install -r python_requirements.txt

EXPOSE 5000

CMD [ "python3", "-u", "./app.py" ]

python_requirements.txt file:

pandas
numpy
flask
scikit-learn

app.py

import numpy as np
from flask import Flask, request, jsonify, render_template
import pickle

app = Flask(__name__,template_folder='templates')
model = pickle.load(open('model.pkl', 'rb'))

@app.route('/')
def home():
    return render_template('index.html')

@app.route('/predict',methods=['POST'])
def predict():

    int_features = [int(x) for x in request.form.values()]
    final_features = [np.array(int_features)]
    prediction = model.predict(final_features)

    output = np.round(prediction[0], 2)

    return render_template('index.html', prediction_text='Home price should be $ {}'.format(output))

@app.route('/results',methods=['POST'])
def results():

    data = request.get_json(force=True)
    prediction = model.predict([np.array(list(data.values()))])

    output = prediction[0]
    return jsonify(output)

if __name__ == "__main__":
    #app.run(debug=True, host='0.0.0.0')
    app.run(debug=True)

Thanks guys!

The problem is in the CMD command in Dockerfile.

From Docker docs:

"Now, all we have to do is to tell Docker what command we want to run when our image is executed inside a container. We do this using the CMD command. Note that we need to make the application externally visible (ie from outside the container) by specifying --host=0.0.0.0."

You need to provide the --host argument and change the command:

CMD ["python3", "-m", "flask", "run", "--host=0.0.0.0"]

Also, when I run the container, I receive a RuntimeError related to UTF-8. So you need to add the following ENV vars to Dockerfile.

ENV LC_ALL=C.UTF-8
ENV LANG=C.UTF-8 

Finally, Dockerfile looks like this:

FROM ubuntu:18.04
    
RUN apt -y update &&\
    apt -y install python3 python3-pip

ENV PYTHON_VERSION 3.9.4

COPY . .

RUN python3 -m pip install -r python_requirements.txt

EXPOSE 5000

ENV LC_ALL=C.UTF-8 
ENV LANG=C.UTF-8 
CMD [ "python3", "-m" , "flask", "run", "--host=0.0.0.0"]

Source: https://docs.docker.com/language/python/build-images/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM