You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker部署Flask应用连接失败求助(Ubuntu 18.04环境)

Hey there, let's troubleshoot your Flask-Docker connection issue step by step—looks like a couple of small but critical mistakes are causing the problem.

1. The Root Cause: Flask Startup Code Isn't Executing

First, take a close look at your app.py entry point:

if __name__ == '__main_':
    app.run(debug=True, host='0.0.0.0', port=5000)

You've got a typo here: __main_ is missing an underscore—it should be __main__. This means when your container runs python app.py, the Flask server startup code never triggers. The container starts, does nothing, and immediately exits. That's why docker ps might briefly show it as running, but docker exec fails (it's already stopped) and your browser can't connect.

2. Secondary Issue: Requirements File Path Mismatch

In your Dockerfile, you're copying requirements.txt to a non-working directory:

COPY ./requirements.txt /pynlg_root/requirements.txt

But your working directory is /flask_app_dir, and you run pip install -r requirements.txt from there. Pip will look for the file in /flask_app_dir and fail to find it (even though you manually install Flask later, this is a messy setup that could break other dependencies). Fix this by copying the file to your working directory:

COPY ./requirements.txt .

3. Fix & Verify Steps

Step 1: Correct the app.py Typo

Update the entry line to:

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=5000)

Step 2: Adjust the Dockerfile

Fix the requirements file path, and reorder steps to leverage Docker's build cache (for faster future builds):

FROM python:3.7 as alias_one
RUN apt-get clean && apt-get update
RUN apt-get install -y \
software-properties-common \
python3.7 \
python3-pip \
wget \
apt-transport-https \
build-essential \
ca-certificates \
bash \
perl \
publicsuffix
RUN mkdir /local/
WORKDIR /local/
(... install libs with wget, run installation shells etc. ...)

# second image:
FROM python:3.7
WORKDIR /flask_app_dir

# Copy requirements first to cache dependency installs
COPY ./requirements.txt .
ARG SPACY_VERSION=2.3.4
RUN pip install -r requirements.txt
RUN pip install spacy==${SPACY_VERSION}
RUN python -m spacy download fr_core_news_lg
RUN pip install six

# Copy application code
ADD . /flask_app_dir
COPY ./dir1 /flask_app_dir/dir1
COPY ./__init__.py /flask_app_dir/__init__.py
COPY ./config.py /flask_app_dir/config.py
COPY ./app.py /flask_app_dir/app.py
COPY --from=alias_one /local/ /local/

ENV PATH /local/bin:/local/cmd:$PATH

ENTRYPOINT ["python"]
CMD ["app.py"]

Step 3: Rebuild & Test

Run these commands again:

sudo docker build -t flask-image:latest .
sudo docker run -d -p 5000:5000 --name flask_try flask-image:latest

Check if the container stays running with sudo docker ps, then look for Flask's startup log with sudo docker logs flask_try—you should see a line like:

Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)

Now accessing 127.0.0.1:5000 in your browser should work as expected.

内容的提问来源于stack exchange,提问作者Catalina Chircu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:05:05