Home > other >  Refused to connect local ip address by using docker while running ok without docker
Refused to connect local ip address by using docker while running ok without docker

Time:07-11

[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+cpu-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

CodePudding user response:

The problem is that 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/

  • Related