Google Chrome 在 Docker 容器内:DevToolsActivePort 文件不存在

Google Chrome inside Docker container: DevToolsActivePort file doesn't exist

我有一个安装 Google Chrome 和 chromedriver

的 Dockerfile
FROM python:3.9 AS base

ENV PATH "/opt/venv/bin:$PATH"
ENV PYTHONUNBUFFERED True
ENV PYTHONDONTWRITEBYTECODE True

FROM base as builder
RUN python -m venv /opt/venv
COPY requirements requirements
RUN pip install requests
RUN pip install -r requirements/common.txt

FROM base
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list'
RUN apt-get -y update
RUN apt-get install -y google-chrome-stable

RUN apt-get install -yqq unzip
RUN wget -qO /tmp/chromedriver.zip http://chromedriver.storage.googleapis.com/$(curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE)/chromedriver_linux64.zip
RUN unzip /tmp/chromedriver.zip chromedriver -d /usr/local/bin/

ENV DISPLAY=:1

WORKDIR /app
COPY --from=builder /opt/venv /opt/venv
COPY . .

ARG PORT=5000
ENV PORT $PORT
EXPOSE $PORT

CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 --timeout 0 api:app

它一直有效,直到今天我收到错误:

(unknown error: DevToolsActivePort file doesn't exist) (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

是某个新版本的 chromedriver 还是 google 它自己坏了?

可以是什么?

当 DISPLAY 为 :0 时,您必须添加选项 headless

options.add_argument("--headless")