Firefox(headless)+selenium cannot access internet from docker container

泄露秘密 提交于 2021-02-11 14:02:30

问题


I have tested the internet connection by wget https://www.google.com and it worked from inside the docker. But, when I run a headless firefox with selenium python binding, selenium throughs the 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)

But when I run the same python file from my host, it runs completely fine.

(Kindly do not suggest me to use docker-selenium image. I have reasons to not to use them. Except changing the base image, any suggestion or query is welcome.)

Below is the 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()

And below is the 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 \
    && apt-get install --no-install-recommends --no-install-suggests -y tzdata ca-certificates bzip2 curl wget libc-dev libxt6 \
    && apt-get install --no-install-recommends --no-install-suggests -y `apt-cache depends firefox-esr | awk '/Depends:/{print$2}'` \
    && update-ca-certificates \
    # Cleanup unnecessary stuff
    && apt-get purge -y --auto-remove \
                  -o APT::AutoRemove::RecommendsImportant=false \
    && 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]+'` && \
    wget https://github.com/mozilla/geckodriver/releases/download/$GECKODRIVER_VERSION/geckodriver-$GECKODRIVER_VERSION-linux64.tar.gz && \
    tar -zxf geckodriver-$GECKODRIVER_VERSION-linux64.tar.gz -C /usr/local/bin && \
    chmod +x /usr/local/bin/geckodriver && \
    rm geckodriver-$GECKODRIVER_VERSION-linux64.tar.gz
# install firefox
RUN FIREFOX_SETUP=firefox-setup.tar.bz2 && \
    wget -O $FIREFOX_SETUP "https://download.mozilla.org/?product=firefox-latest&os=linux64" && \
    tar xjf $FIREFOX_SETUP -C /opt/ && \
    ln -s /opt/firefox/firefox /usr/bin/firefox && \
    rm $FIREFOX_SETUP

# Install pip requirements
RUN python -m pip install --upgrade pip && 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 && chown -R appuser $APP_HOME
USER appuser

CMD [ "python3", "run.py" ]

My container build and run commands are :

docker build -t myselcontainer .
docker run myselcontainer

来源:https://stackoverflow.com/questions/64118544/firefoxheadlessselenium-cannot-access-internet-from-docker-container

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!