RoDo
RoDo

Reputation: 73

Docker container run locally - didn't send any data

I am trying to run a basic flask app inside a docker container. The docker build works fine but when i try to test locally i get

127.0.0.1 didn't send any data error.

Dockerfile

FROM tiangolo/uwsgi-nginx-flask:python3.6-alpine3.7 

ENV LISTEN_PORT=5000
EXPOSE 5000

RUN pip install --upgrade pip

WORKDIR /app
ADD . /app

CMD ["python3","main.py","--host=0.0.0.0"]

main.py

import flask
from flask import Flask, request
import os

app = Flask(__name__)

@app.route('/')

def this_works():
  return "This works..."

if  __name__ == '__main__':
    app.run(debug=True)

The command to run the container i am using is :

docker run -it --name dockertestapp1 --rm -p 5000:5000 dockertestapp1

Also command to build is :

docker build --tag dockertestapp1 .

Could someone help please.

Upvotes: 7

Views: 11621

Answers (3)

akazuko
akazuko

Reputation: 1394

The issue is that you are passing --host parameter while not using the flask binary to bring up the application. Thus, you need to just take the parameter out of CMD in Dockerfile to your code. Working setup:

Dockerfile:

FROM tiangolo/uwsgi-nginx-flask:python3.6-alpine3.7 

ENV LISTEN_PORT=5000
EXPOSE 5000

RUN pip install --upgrade pip

WORKDIR /app
ADD . /app

CMD ["python3","main.py"]

and main.py

import flask
from flask import Flask, request
import os

app = Flask(__name__)

@app.route('/')

def this_works():
  return "This works..."

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

The way you are building the image and bringing up the container is correct. I am adding the steps again for the answer to be whole:

# build the image
docker build --tag dockertestapp1 .

# run the container
docker run -it --name dockertestapp1 --rm -p 5000:5000 dockertestapp1

Upvotes: 9

Kyle Willmon
Kyle Willmon

Reputation: 719

The way you're using it, flask is only listening on 127.0.0.1 within the container. So the only way that you can reach it is from within the same container.

For the docker image you're using, you shouldn't need to override the default command. Remove this line from your Dockerfile and it should work:

CMD ["python3","main.py","--host=0.0.0.0"]

Upvotes: 0

Kartik Sikka
Kartik Sikka

Reputation: 107

Well... Every Docker Containter has it's own memory space & network boundaries.To make a port available to Client Machine ( Your OS ) , you have to do 2 steps :

  1. Expose the port
  2. Publish the port

Exposing the Port

    You generally do it by putting EXPOSE statement inside you docker file which you had already done

        Now you need to do step 2

Publishing the Port

Use Below Command to do that ( which maps the port of you host machine ( Docker Conainer ) to the client machine .

docker run -p client_port:host_port imageName

In your case , it would be :

docker run -p 5000:5000 imageName

Replace imageName with your Docker Image Name

Then you can do :

localhost:5000

Upvotes: 0

Related Questions