【发布时间】:2021-09-28 07:47:58
【问题描述】:
我正在尝试从 docker 容器中执行 chrome 驱动程序,但没有成功,到目前为止我做了以下操作。
- 实施 Microsoft Azure Functions
- 实施 Dockerfile
- 成功运行 docker 容器
Python 文件
def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request. 11:38')
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
path_to_chrome=os.path.join(os.getcwd(),'/usr/local/bin/chromedriver')
driver = webdriver.Chrome(executable_path=path_to_chrome, chrome_options=chrome_options)
Dockerfile
FROM mcr.microsoft.com/azure-functions/python:3.0-python3.8
# 0. Install essential packages
RUN apt-get update \
&& apt-get install -y \
build-essential \
cmake \
git \
wget \
unzip \
&& rm -rf /var/lib/apt/lists/*
# 1. Install Chrome (root image is debian)
# See https://stackoverflow.com/questions/49132615/installing-chrome-in-docker-file
ARG CHROME_VERSION="google-chrome-stable"
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
&& echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list \
&& apt-get update -qqy \
&& apt-get -qqy install \
${CHROME_VERSION:-google-chrome-stable} \
&& rm /etc/apt/sources.list.d/google-chrome.list \
&& rm -rf /var/lib/apt/lists/* /var/cache/apt/*
# 2. Install Chrome driver used by Selenium
RUN LATEST=$(wget -q -O - http://chromedriver.storage.googleapis.com/LATEST_RELEASE) && \
wget http://chromedriver.storage.googleapis.com/$LATEST/chromedriver_linux64.zip && \
unzip chromedriver_linux64.zip && ln -s $PWD/chromedriver /usr/local/bin/chromedriver
ENV PATH="/usr/local/bin/chromedriver:${PATH}"
# 3. Install selenium in Python
RUN pip install -U selenium
# 4. Finally, copy python code to image
COPY . /home/site/wwwroot
# 5. Install other packages in requirements.txt
RUN cd /home/site/wwwroot && \
pip install -r requirements.txt
【问题讨论】:
标签: python docker selenium selenium-chromedriver azure-functions