本地Docker容器路由正常,部署至CloudRun后抛堆栈错误求助
问题:Docker镜像部署至Google CloudRun后Selenium Chrome驱动报错
我开发了一个基于Python的网页爬取应用,使用Selenium WebDriver搭配Chrome/ChromeDriver实现功能。该应用的路由在本地Docker容器中运行正常(返回200状态码及预期数据),但将同一Docker镜像部署至Google CloudRun后,调用相同路由时出现如下堆栈跟踪错误:
"Message: Stacktrace: #0 0x2a53f7fc886a <unknown> #1 0x2a53f7c96e50 <unknown> #2 0x2a53f7ce6644 <unknown> #3 0x2a53f7ce6931 <unknown> #4 0x2a53f7d2c534 <unknown> #5 0x2a53f7d0b4bd <unknown> #6 0x2a53f7d299c6 <unknown>..."
我的Dockerfile内容:
# Use the Python image from Docker Hub FROM python:3.12.4-slim # Set the working directory WORKDIR /app # Install system dependencies for Chrome RUN apt-get update && \ apt-get install -y \ wget \ unzip \ gcc \ g++ \ make \ libnss3 \ libgdk-pixbuf2.0-0 \ libatk-bridge2.0-0 \ libatk1.0-0 \ libcups2 \ libxkbcommon0 \ libx11-xcb1 \ libxcomposite1 \ libxdamage1 \ libxrandr2 \ libxshmfence1 \ libglib2.0-0 \ libpango-1.0-0 \ libpangocairo-1.0-0 \ libfontconfig1 \ libnss3 \ fonts-liberation \ libasound2 \ libdrm2 \ libgbm1 \ libgtk-3-0 \ libvulkan1 \ libxfixes3 \ xdg-utils \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* # Install Google Chrome RUN wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb RUN dpkg -i google-chrome-stable_current_amd64.deb; apt-get -fy install # Copy dependencies into the container and install COPY requirements.txt . RUN pip install -r requirements.txt # Copy the rest of the application code into the container COPY . . # Expose the port EXPOSE 8000 # Run the app (with Uvicorn) CMD ["uvicorn", "app.router:app", "--host", "0.0.0.0", "--port", "8000", "--reload"]
环境版本说明:
- Chrome版本:128.0.6613.119
- requirements.txt包含:
chromedriver-binary>=128.0.0,<129.0.0
路由调用的核心函数:
from selenium import webdriver import chromedriver_binary @staticmethod async def search_info(company_name: str): options = webdriver.ChromeOptions() options.add_argument('--headless') options.add_argument('--no-sandbox') options.add_argument('--disable-dev-shm-usage') options.add_argument('--remote-debugging-port=9222') driver = webdriver.Chrome(options=options) final_result = [] try: keywords = ["Talent Acquisition", "Recruiter", "HR", "CEO"] for keyword in keywords: result = searchInfoService._search_info( driver, company_name, keyword ) if result: final_result.extend(result[:1]) return final_result except Exception as e: print(f"An error occurred: {e}") raise finally: driver.quit()
已尝试的解决步骤:
| 步骤 | 本地(Docker)结果 | 部署后结果 |
|---|---|---|
使用ChromeDriverManager安装兼容的chromedriver:driver = webdriver.Chrome(service=ChromeDriverManager().install() options=options) | ❌ RUN文件报错 | - |
将webdriver_manager更新至4.0.2 | ✅ 正常运行 | ❌ 503:服务不可用 |
| 增加部署容器的内存 | - | ❌ 500:堆栈跟踪错误 |
| 重新配置Dockerfile以安装兼容版本的Chrome和ChromeDriver | ✅ 正常运行 | ❌ 500:堆栈跟踪错误 |
当前版本:简化Chrome安装,添加chromedriver-binary作为依赖(参考相关文档),将Selenium更新至4.24.0,移除driver中的service参数(依据Selenium文档):driver = webdriver.Chrome(options=options) | ✅ 正常运行 | ❌ 500:堆栈跟踪错误 |
我现在完全没头绪,希望能得到解决思路,代码如有疏漏请见谅!
内容的提问来源于stack exchange,提问作者Zoe Laventhol
相关产品推荐
相关产品推荐

