cURL From Docker Container

  cloud, curl, docker, flask, python

I am newbie in Docker but I wanted to deploy a simple app on the cloud so I created this simple flask app like the below:

import os
from flask import Flask, jsonify, request
from flask_restful import Api, Resource, reqparse
import json


app = Flask(__name__)
api = Api(app)

parser = reqparse.RequestParser()
parser.add_argument('list', type=list)


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

    return jsonify('Hello there!!')

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

    ABC = parser.parse_args()
    data_decoded = request.data.decode("utf-8") 

    #convert to json
    data_json = json.loads(data_decoded)
    print(data_json)


    if 'name' in data_json:
        name = data_json['name']
    else:
        name is None


    if isinstance(name,str) and name is not None:

        output = "Hi, "+name+'!! Have a good day!!'

    else:

        output = "Whoever you are, I wish you a good day!! "

    output = json.dumps(output)

    return jsonify(output)

if __name__ == '__main__':
    port = int(os.environ.get("PORT", 5001))
    app.run(host='0.0.0.0', port = port, debug=True)

    # local
    # app.run(host='127.0.0.1', port = port, debug=True)

My Dockerfile below is below:

FROM tiangolo/uwsgi-nginx-flask:python3.8

# copy over our requirements.txt file
COPY requirements.txt /tmp/

# upgrade pip and install required python packages
RUN pip install -U pip
RUN pip install -r /tmp/requirements.txt

# copy over our app code
COPY . /app

ENV LISTEN_PORT 5001

EXPOSE 5001

And then I build a docker image:

docker build -t my_image .

And next run it:

docker run -d --name my_container -p 5001:5001 my_image

When I tried do curl like below:

curl localhost:5001

I got the expected output like below:

Hello World from Flask in a uWSGI Nginx Docker container with Python 3.8 

which is like what I expected

But then when I tried to curl the second function like below:

curl -v --insecure -XPOST -H "Content-type: application/json" -d '{"name":"my name"}' 'http://localhost:5001/call' 

I did not get the result that I wanted instead I got:

Note: Unnecessary use of -X or --request, POST is already inferred.
*   Trying 127.0.0.1:5001...
* Connected to localhost (127.0.0.1) port 5001 (#0)
> POST /call HTTP/1.1
> Host: localhost:5001
> User-Agent: curl/7.69.1
> Accept: */*
> Content-type: application/json
> Content-Length: 18
> 
* upload completely sent off: 18 out of 18 bytes
* Mark bundle as not supporting multiuse
< HTTP/1.1 404 NOT FOUND
< Server: nginx/1.17.10
< Date: Thu, 09 Sep 2021 04:37:17 GMT
< Content-Type: text/html; charset=utf-8
< Content-Length: 232
< Connection: keep-alive
< 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>404 Not Found</title>
<h1>Not Found</h1>
<p>The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.</p>
* Connection #0 to host localhost left intact

Is there anyone who knows why is that and how can do the right curl to get the desired output? Is this the right way to deploy the flask app on the cloud?

Please advise

Source: Docker Questions

LEAVE A COMMENT