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.
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.
The docker images, they are stored inside the docker directory: /var/lib/docker/ images are stored there.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With