#docker #selenium #firefox #geckodriver
#docker #селен #firefox #geckodriver
Вопрос:
Я протестировал подключение к Интернету wget https://www.google.com
, и оно работало изнутри docker. Но, когда я запускаю безголовый firefox с привязкой selenium python, selenium пропускает TimeoutException
:
>> docker run myselcontainer
Traceback (most recent call last):
File "run.py", line 24, in <module>
driver = webdriver.Firefox(service_log_path=os.devnull, options=options, capabilities=capabilities, firefox_profile=profile)
File "/usr/local/lib/python3.8/site-packages/selenium/webdriver/firefox/webdriver.py", line 170, in __init__
RemoteWebDriver.__init__(
File "/usr/local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/usr/local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/usr/local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: Connection refused (os error 111)
Но когда я запускаю тот же файл python со своего хоста, он работает нормально.
(Пожалуйста, не предлагайте мне использовать изображение docker-selenium. У меня есть причины не использовать их. За исключением изменения базового изображения, любые предложения или запросы приветствуются.)
Ниже приведен run.py:
from selenium import webdriver
import os
# Set proper profile
profile = webdriver.FirefoxProfile()
profile.set_preference("security.fileuri.strict_origin_policy", False) # disable Strict Origin Policy
profile.set_preference("dom.webdriver.enabled", False) # disable Strict Origin Policy
# Capabilities
capabilities = webdriver.DesiredCapabilities.FIREFOX
capabilities['marionette'] = True
# Options
options = webdriver.FirefoxOptions()
options.add_argument("--log-level=OFF")
# Using non Headless for debugging
options.headless = True
driver = webdriver.Firefox(service_log_path=os.devnull, options=options, capabilities=capabilities, firefox_profile=profile)
driver.set_window_size(1920, 1080)
driver.get('https://www.google.com')
print(driver.page_source)
driver.quit()
И ниже приведен файл Dockerfile:
FROM python:3.8-slim-buster
# Python optimization
## Keeps Python from generating .pyc files in the container
ENV PYTHONDONTWRITEBYTECODE 1
## Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED 1
# Locales
ENV LANG C.UTF-8
ENV LC_ALL C.UTF-8
ENV DEBIAN_FRONTEND=noninteractive
# Installation required for selenium
RUN apt-get update -y
amp;amp; apt-get install --no-install-recommends --no-install-suggests -y tzdata ca-certificates bzip2 curl wget libc-dev libxt6
amp;amp; apt-get install --no-install-recommends --no-install-suggests -y `apt-cache depends firefox-esr | awk '/Depends:/{print$2}'`
amp;amp; update-ca-certificates
# Cleanup unnecessary stuff
amp;amp; apt-get purge -y --auto-remove
-o APT::AutoRemove::RecommendsImportant=false
amp;amp; rm -rf /var/lib/apt/lists/* /tmp/*
# install geckodriver
RUN GECKODRIVER_VERSION=`curl https://github.com/mozilla/geckodriver/releases/latest | grep -Po 'v[0-9] .[0-9] .[0-9] '` amp;amp;
wget https://github.com/mozilla/geckodriver/releases/download/$GECKODRIVER_VERSION/geckodriver-$GECKODRIVER_VERSION-linux64.tar.gz amp;amp;
tar -zxf geckodriver-$GECKODRIVER_VERSION-linux64.tar.gz -C /usr/local/bin amp;amp;
chmod x /usr/local/bin/geckodriver amp;amp;
rm geckodriver-$GECKODRIVER_VERSION-linux64.tar.gz
# install firefox
RUN FIREFOX_SETUP=firefox-setup.tar.bz2 amp;amp;
wget -O $FIREFOX_SETUP "https://download.mozilla.org/?product=firefox-latestamp;os=linux64" amp;amp;
tar xjf $FIREFOX_SETUP -C /opt/ amp;amp;
ln -s /opt/firefox/firefox /usr/bin/firefox amp;amp;
rm $FIREFOX_SETUP
# Install pip requirements
RUN python -m pip install --upgrade pip amp;amp; python -m pip install --no-cache-dir selenium scrapy
ENV APP_HOME /usr/src/app
WORKDIR /$APP_HOME
COPY . $APP_HOME/
RUN export PYTHONPATH=$PYTHONPATH:$APP_HOME
# Switching to a non-root user, please refer to https://aka.ms/vscode-docker-python-user-rights
RUN useradd appuser amp;amp; chown -R appuser $APP_HOME
USER appuser
CMD [ "python3", "run.py" ]
Мои команды сборки и запуска контейнера :
docker build -t myselcontainer .
docker run myselcontainer