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

使用Docker在Vercel部署Flask应用时,访问图片生成接口却下载app.py文件的问题求助

使用Docker在Vercel部署Flask应用时,访问图片生成接口却下载app.py文件的问题求助

我正在用Docker在Vercel上部署一个Flask应用,但当我访问生成图片的接口时,浏览器没有返回预期的图片,而是下载了app.py文件。

  • 我已经创建了Dockerfile和vercel.json配置文件。
  • 本地用flask run运行应用时一切正常,图片能正常生成。
  • 在Vercel部署完成后,访问接口(比如/generate_image?name=John)时,浏览器开始下载app.py文件,而不是显示图片。

Dockerfile内容:

# Use an official Python image
FROM python:3.9-slim

# Set working directory
WORKDIR /app

# Install system dependencies for Pillow (libjpeg, zlib)
RUN apt-get update && apt-get install -y libjpeg-dev zlib1g-dev

# Copy project files into the container
COPY . /app

# Copy the fonts directory into the Docker container
COPY fonts /app/fonts

# Install Python dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Expose the Flask port
EXPOSE 8000

# Install gunicorn (if not already in requirements.txt)
RUN pip install gunicorn

# Run the Flask app with gunicorn for production
CMD ["gunicorn", "-b", "0.0.0.0:8000", "app:app"]

vercel.json内容:

{
  "version": 2,
  "builds": [
    {
      "src": "Dockerfile",
      "use": "@vercel/docker"
    }
  ],
  "routes": [
    {
      "src": "/(.*)",
      "dest": "/app.py"
    }
  ],
  "functions": {
    "app.py": {
      "runtime": "python3.9",
      "includeFiles": "fonts/*"
    }
  },
  "build": {
    "env": {
      "LD_LIBRARY_PATH": "/usr/lib"
    }
  }
}

访问/generate_image?name=John时,本应生成并显示一张以名字首字母为内容的PNG图片,但实际却是浏览器下载app.py文件。

我需要修改Vercel配置或者Dockerfile来正确处理请求吗?是不是vercel.json里的路由配置有问题?

备注:内容来源于stack exchange,提问作者Abdelhakim Sahifa

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.14 09:49:31