docker - 基于图像的 Lambda 不适用于 Selenium Webdriver
问题描述
我想知道为什么我的项目可以在本地运行,但不能在我的帐户中作为基于图像的 lambda 函数运行?
以下命令我在本地执行:
docker build -t lambda-repository .
docker run -p 9001:8080 lambda-repository:latest
在另一个终端:
curl -XPOST "http://localhost:9001/2015-03-31/functions/function/invocations" -d '{}'
这导致:
"Google"%
我的 AWS 账户会发生什么:
- 我创建了一个 ecr 存储库
- 我把我的形象推到它上面
- 我使用图像创建一个 lambda 函数
- 调用 lambda 会导致以下结果
错误:
START RequestId: 2a8b9cf9-f1f0-48cb-8a5d-eb86c2dddcc0 Version: $LATEST
[ERROR] WebDriverException: Message: unknown error: Chrome failed to start: crashed.
(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.)
Traceback (most recent call last):
File "/var/task/app.py", line 14, in handler
driver = webdriver.Chrome(options=options)
File "/var/lang/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py", line 76, in __init__
RemoteWebDriver.__init__(
File "/var/lang/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/var/lang/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/var/lang/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/var/lang/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
END RequestId: 2a8b9cf9-f1f0-48cb-8a5d-eb86c2dddcc0
REPORT RequestId: 2a8b9cf9-f1f0-48cb-8a5d-eb86c2dddcc0 Duration: 2736.59 ms Billed Duration: 3752 ms Memory Size: 2048 MB Max Memory Used: 168 MB Init Duration: 1014.78 ms
Dockerfile:
FROM public.ecr.aws/lambda/python:3.8
RUN pwd
WORKDIR /tmp
RUN yum -y update
RUN yum -y install wget unzip
RUN yum -y install GConf2 libX11
RUN yum -y install curl
COPY download-chromedriver.sh ./
RUN /bin/bash -c "source ./download-chromedriver.sh"
RUN unzip chromedriver_linux64.zip
RUN mv chromedriver /usr/bin/chromedriver
RUN chromedriver -version
RUN curl https://intoli.com/install-google-chrome.sh | bash
RUN mv /usr/bin/google-chrome-stable /usr/bin/google-chrome
RUN google-chrome -version && which google-chrome
WORKDIR /var/task
COPY app.py ./
COPY requirements.txt ./
RUN pip3 install -r requirements.txt
CMD ["app.handler"]
下载-chromedriver.sh:
#!/usr/bin/env bash
CHROME_DRIVER_VERSION=`curl -sS https://chromedriver.storage.googleapis.com/LATEST_RELEASE`
wget -N https://chromedriver.storage.googleapis.com/$CHROME_DRIVER_VERSION/chromedriver_linux64.zip
应用程序.py:
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
def handler(event, context):
options = Options()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--disable-gpu')
options.add_argument("window-size=1280,1024")
url = 'http://www.google.com'
driver = webdriver.Chrome(options=options)
driver.get(url)
text = driver.title
return text
要求.txt:
selenium==3.141.0
urllib3==1.26.2
解决方案
这不是解决方案
我在 lambda zip 中使用了这种方式。但也许会为您提供更多提示。
当您运行时chromedriver -version
(我认为应该是chromedriver --verseion
)意味着在本地运行。
所以你应该chromedriver --version
在 lambda_function.handler 中输入。
喜欢os.system('<chromedriver_path>/chromedriver --version')
。
你需要设置executable_path
.
我不确定你是否也需要options.binary_location
。
def handler(event, context):
os.system('/usr/bin/chromedriver --version')
options = Options()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--disable-gpu')
options.add_argument("window-size=1280,1024")
options.binary_location = "/usr/bin/google-chrome"
url = 'http://www.google.com'
driver = webdriver.Chrome(executable_path="/usr/bin/chromedriver", options=options)
driver.get(url)
text = driver.title
return text
在您成功使用 chrome88 启动 Python3.8 后,我希望分享如何工作。
推荐阅读
- python - Python Pandas:输出到excel“.xls”电子表格,只需覆盖选定的工作表
- three.js - 纹理几何在照明更新时丢失图像
- python - Python/Pydantic - 使用带有 json 对象的列表
- intellij-idea - 有没有办法以独立于大小写(然而,各自)的方式选择和编辑下一个事件?
- java - 如何在java中的变量中使用大数进行算术运算而不会出现异常
- r - R cron 脚本不会自动刷新 httr-oauth 令牌
- reactjs - 为什么 React 组件不是每次都重新渲染?
- jquery - 有没有办法在外部点击时关闭 UIkit 切换?
- javascript - 在不同的域中设置 localStorage
- javascript - Javascript:在 Chrome 和 Firefox 中解析 unicode 字符串会产生不同的结果