Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker container run locally - didn't send any data

Tags:

python

docker

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.

like image 696
RoDo Avatar asked Apr 13 '20 19:04

RoDo


People also ask

Do Docker containers run locally?

Docker containers can run on a developer's local laptop, on physical or virtual machines in a data center, on cloud providers, or in a mixture of environments.

Where are Docker files stored locally?

The docker images, they are stored inside the docker directory: /var/lib/docker/ images are stored there.

Do I lose my data when the Docker container exits?

Do I lose my data when the container exits? 🔗 Not at all! Any data that your application writes to disk gets preserved in its container until you explicitly delete the container.


Video Answer


1 Answers

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
like image 64
akazuko Avatar answered Nov 14 '22 23:11

akazuko