Flask App not working when running on docker container - python

Im new to docker and Flask and I'm getting an issue when I try to run the app. The browser says the site (172.17.0.2:5000) can't be reached.
For anyone wondering, the Dockerfiles:
FROM ubuntu
RUN apt-get update && apt-get install -y python3 python3-pip
RUN pip3 install flask
RUN mkdir -p /opt/MyApp-test
COPY . /opt/MyApp-test
WORKDIR /opt/MyApp-test
EXPOSE 5000
ENTRYPOINT python3 main.py
CMD flask run --host 0.0.0.0
The main.py:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def index():
return 'IT WORKED! I AM RUNNING FROM A DOCKER CONTAINER!!!'
if __name__ == '__main__':
app.run()
And when I run the container, I get:
(base) daniellombardi#Daniels-MacBook-Pro MyApp-test % docker run 2625
* Serving Flask app "main" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
I try to go to http://127.0.0.1:5000/, unable to connect, then I inspect the container to get its IP address, and it says 172.0.1 instead of 127.0.0.1:5000, but also unable to connect. And the app works when I run it on my computer.

You should just bind the exposed port inside of your container to a port in docker host when calling docker run, thus docker run should be called this way:
docker run 2625 -p 5000:5000

Related

Docker image for Flask applicatno [duplicate]

This question already has answers here:
How can I change the host and port that the flask command uses?
(6 answers)
Closed last month.
when a run a simple flask application with a specified port(5888) from the console it is running well and is provided with a
URL: * Debugger PIN: 125-876-281
* Running on http://127.0.0.1:5888/ (Press CTRL+C to quit)
for the same application when I build a docker image and run using the comman : docker run -p 192.168.0.152:5888:5888 dock_test is is giving the
URL : *** Running on all addresses (0.0.0.0)
* Running on http://127.0.0.1:5000
* Running on http://172.17.0.3:5000** with the different port(5000).
The main python file: app.py
from flask import Flask
app = Flask(__name__)
#app.route("/")
def index():
return "running"
if __name__ == "__main__":
app.run(debug=True,port=5888)
docker file: Dockerfile
FROM ubuntu
RUN apt update
RUN apt install python3-pip -y
RUN pip3 install Flask
COPY requirements.txt requirements.txt
ENV LISTEN_PORT=5888
EXPOSE 5888
RUN pip3 install -r requirements.txt
WORKDIR C:\Users\heman\PycharmProjects\dock_test\main.py
COPY . .
ENV FLASK_APP=main.py
CMD ["python3","-m","flask","run","--host=0.0.0.0"]
Step-1: To build the image I've run : docker build -t dock_test .(it created the docker image well)
step-2: To run : docker run -d -p 5888:5888 dock_test
output: * Running on all addresses (0.0.0.0)
* Running on http://127.0.0.1:5000
* Running on http://172.17.0.3:5000
why the port is changing to 5000 instead of 5888?
I think you are missing the port number in the Dockerfile.
In your docker file, you are calling your application as a module (by using -m). When you run a module, it's first imported internally and then runs the function inside it. You can find more details here: Execution of Python code with -m option or not
So basically, the if "____main___" part is skipped when you call using the -m command. Thus, the flask app is defaulting to the default port 5000. If you specify it in the Dockerfile, as shown below, it should work.
CMD ["python3","-m", "flask", "run","--host=0.0.0.0", "--port=5888"]

Refused to connect local ip address by using docker while running ok without docker [duplicate]

This question already has answers here:
Deploying a minimal flask app in docker - server connection issues
(8 answers)
Closed 7 months ago.
[Editted]
I am trying to dockerize a simple flask project. I kept getting this error when I run the project using docker. On the other hand the project works just fine when I run it normally using python3 test-flask.py
Docker File
FROM python:3.9.6-slim
# https://www.lifewithpython.com/2021/05/python-docker-env-vars.html
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
PYTHONUTF8=1 \
PIP_DISABLE_PIP_VERSION_CHECK=on
RUN apt-get update \
&& apt-get install --no-install-recommends -y \
curl \
wget \
unzip \
jq \
ffmpeg \
libsndfile1-dev \
&& rm -rf /var/lib/apt/lists/*
RUN wget "https://download.pytorch.org/whl/cpu/torch-1.10.2%2Bcpu-cp39-cp39-linux_x86_64.whl" \
&& pip install torch-1.10.2+cpu-cp39-cp39-linux_x86_64.whl
RUN mkdir app
WORKDIR /app
COPY README.md .
COPY requirements.txt .
COPY setup.py .
COPY src ./src
RUN pip install .
# COPY main.py .
COPY test_flask.py .
CMD [ "python", "test_flask.py", "run", "--host", "0.0.0.0"]
docker-compose.yml
version: '3.9'
services:
asr-cpu-local:
build:
context: ./
dockerfile: ./Dockerfile
volumes:
- ./data:/data
- ./models:/models
ports:
- 127.0.0.1:8000:8000
In the Terminal and Docker Container log, after I run it with docker, it shows no error at all
* Serving Flask app 'test_flask' (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://127.0.0.1:8000 (Press CTRL+C to quit)
* Restarting with stat
For additional information, I have run docker port to check, but the error still occured:
docker port <container-name>
8000/tcp -> 127.0.0.1:8000
For the File test_flask.py:
from flask import Flask
app = Flask(__name__)
import librosa
from src.jrasr.predictors import Wav2vec2bert2bertPredictor
MODEL_CONFIG = {
"name": "Wav2vec2bert2bertPredictor",
"version": "0.0.1",
"models": [
{
"name": "wav2vec2",
"dir": "/models/wav2vec2-base-csj-kana-v0.0.1"
},
{
"name": "bert2bert",
"dir": "/models/bert2bert-csj-kana-v0.0.1"
}
]
}
#app.before_first_request
def load_model_to_app():
# Load the model
app.predictor = Wav2vec2bert2bertPredictor(MODEL_CONFIG)
#app.route('/')
def index():
return "This is Home Page"
#app.route('/data/<audio_path>')
def predict(audio_path):
model = app.predictor
audio_path = "/data/"+audio_path
data, _ = librosa.load(audio_path, sr=16000, mono=True, duration=10)
words = model.predict(data)
result = "".join(words)
return result
if __name__ == "__main__":
app.run(host="127.0.0.1", port=8000, debug=True)
But when I try to access the Ip address as shown in the Terminal and Docker Container Log, I got that 127.0.0.1 refuse to connect such as in the image shown below. The problem is in the terminal and docker log, there isn't any Error message, but still I can't access it, can anyone tell me, how to solve my problems?
Error at the local browser
127.0.0.1 is the localhost interface and when running inside the container only the container has access to it. To be accessible outside the container it should be the special IP 0.0.0.0 to bind all interfaces.
Solution 1
Change the host to be 0.0.0.0 instead of 127.0.0.1 in the test_flask.py. Something like the following snippet.
WARNING: As 0.0.0.0 binds all interfaces to the outside world it could be a security risk if running it locally instead of in a container. That said, the Solution 2 is the recommended one (With the 127.0.0.1 in the Python source as it is). So, when running it directly locally it binds to the localhost and when running it in the container it binds the outside world of the container itself.
...
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000, debug=True)
Solution 2
On the other hand, to run it passing the host as an argument the Dockerfile should be changed to the following:
...
ENV FLASK_APP=test_flask
CMD ["flask", "run", "--host=0.0.0.0"]
Test it with the browser connecting to http://localhost:8000/ or http://127.0.0.1:8000/

Container Connection Refused

I am new to Docker.
Here is a very simple application written in DashPlotly:
FROM python:3.8-slim
COPY . /app
WORKDIR /app
RUN pip3 install --no-cache -r requirements.txt
EXPOSE 8050
CMD ["python3","./app.py","--host","0.0.0.0"]
#docker build -t test .
#docker run -p 8051:8050 -it test
Connecting to the PostgreSQL database...
Running on http://127.0.0.1:8050/
Debugger PIN: 479-458-364
* Serving Flask app "app" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
But when a point the browser to 127.0.0.1:8051, it says connection reset...
The solution is to force the DashPlotly server to run on 0.0.0.0
app.run_server(host='0.0.0.0',debug=False, port=8050)

Can not connect to docker container(python server) with Dockerfile

I am studying the docker.
I using docker and using Dockerfile to run a python server.
This my python name app.py:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello():
return "hello docker"
if __name__ == '__main__':
app.run()
app.run(host="0.0.0.0", port=5000)
I am running in the VirtualBox and use centos7.
If I don't add
host="0.0.0.0", port=5000
I can not connect to http://192.168.1.6:5000/ (This is my virtual IP)
but now when I using Dockerfile and run this command, I can't connect to the server.
This is my Dockerfile :
FROM python:2.7
LABEL maintainer ="me <me#gmail.com>"
RUN pip install flask
COPY app.py /app/
WORKDIR /app
EXPOSE 5000
CMD ["python","app.py"]
And I inspect the container
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
It is succeed. But I cant connect to http://192.168.1.6:5000/
I also set the firewall to open port 5000 and reload.
Why can I connect to my VM?
EXPOSE doesn't actually publish the port. You should run with the -p option in order to publish and map the port:
docker run --detach -p 5000:5000 <image>

Can't acces swagger server into docker container

I have a swagger server api in python that I can run on my pc and easily access to the user interface via web. I'm now trying to run this API into a docker container and place it into a remote server. After the doing the 'docker run' command int the remote server all seems to be working fine but when I try to connect I got a ERR_CONNECTION_REFUSED response. The funny thing is that if I enter into the container the swagger server is working and answer my requests.
Here is my Dockerfile:
FROM python:3
MAINTAINER Me
ADD . /myprojectdir
WORKDIR /myprojectdir
RUN pip install -r requirements.txt
RUN ["/bin/bash", "-c", "chmod 777 {start.sh,stop.sh,restart.sh,test.sh}"]
Here are my commands to build/run:
sudo docker build -t mycontainer .
sudo docker run -d -p 33788:80 mycontainer ./start.sh
Here is the start.sh script:
#!/bin/bash
echo $'\r' >> log/server_log_`date +%Y%m`.dat
python3 -m swagger_server >> log/server_log_`date +%Y%m`.dat 2>&1
And the main.py of the swagger server:
#!/usr/bin/env python3
import connexion
from .encoder import JSONEncoder
if __name__ == '__main__':
app = connexion.App(__name__, specification_dir='./swagger/')
app.app.json_encoder = JSONEncoder
app.add_api('swagger.yaml', arguments={'title': 'A title'})
app.run(port=80, threaded=True, debug=False)
Does anyone know why I can't acces to 'myremoteserver:33788/myservice/ui' and what to change for solving it.
Thanks in advance
I finally managed to find out the solution. It's needed to tell the flask server of connexion to run on 0.0.0.0 so that not only local connections are allowed and to change in the swagger.yaml the url with the name of the server where the docker container is located
app.run(port=80, threaded=True, debug=False, host='0.0.0.0')

Categories