gpt4 book ai didi

python - 在没有docker的情况下运行正常时拒绝使用docker连接本地ip地址

转载 作者:行者123 更新时间:2023-12-05 04:26:50 25 4
gpt4 key购买 nike

<分区>

[已编辑]

我正在尝试对一个简单的 Flask 项目进行 docker 化。当我使用 docker 运行项目时,我一直收到此错误。另一方面,当我使用 python3 test-flask.py 正常运行项目时,该项目工作得很好

docker 文件

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

在终端和 Docker 容器日志中,我用 docker 运行它后,它完全没有显示错误

* 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

更多信息,我已经运行docker port查看,但是还是报错:

docker port <container-name>
8000/tcp -> 127.0.0.1:8000

对于文件 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)

但是当我尝试访问终端和 Docker 容器日志中显示的 Ip 地址时,我发现 127.0.0.1 拒绝连接,如下图所示。问题出在终端和 docker 日志中,没有任何错误消息,但我仍然无法访问它,谁能告诉我,如何解决我的问题?

Error at the local browser

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com