2017-09-11 90 views
0

我最近學習了Docker的基礎知識以及如何創建和運行圖像。我正在嘗試創建一個python腳本的圖像,這個腳本會將一些數據網頁抓取並上傳到服務器。我使用Selenium,Chromium和Windows chromedriver。我試圖在我的Windows機器上構建映像,並能夠將它部署到一系列Linux/Windows服務器上。目前,我只是在同一臺Windows機器上構建和運行,直到我運行它,但我仍然收到相同的錯誤,即使腳本直接在機器上直接運行。OSError:在Docker容器中運行python時的[Errno 8]

這是錯誤:

Traceback (most recent call last): 
    File "my-app.py", line 796, in <module> 
    startScraper(); 
    File "my-app.py", line 92, in startScraper 
    browser = webdriver.Chrome(chrome_options = options, executable_path = path_to_chromedriver); 
    File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 62, in __init__ 
    self.service.start() 
    File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 74, in start 
    stdout=self.log_file, stderr=self.log_file) 
    File "/usr/local/lib/python3.6/subprocess.py", line 707, in __init__ 
    restore_signals, start_new_session) 
    File "/usr/local/lib/python3.6/subprocess.py", line 1326, in _execute_child 
    raise child_exception_type(errno_num, err_msg) 
OSError: [Errno 8] Exec format error 

這似乎是有關鉻的選擇,但即使我刪除所有的「附加參數」選項,錯誤仍然存​​在,但這裏的選項:

options = webdriver.ChromeOptions(); 
options.binary_location = './chrome-win32/chrome.exe'; 
options.add_argument('headless') 
options.add_argument('window-size=1400x1300') 
options.add_argument('--mute-audio') 
options.add_argument('--disable-web-security'); 
options.add_argument('--allow-running-insecure-content'); 
options.add_argument('--ignore-certificate-errors') 
options.add_argument('--ignore-ssl-errors') 
prefs = {"profile.managed_default_content_settings.images":2} 
options.add_experimental_option("prefs", prefs); 
path_to_chromedriver = './chromedriver.exe'; 

有什麼,我失蹤,能夠在容器中運行此刮刀?謝謝!

編輯:我忘了加上Dockerfile我如何建立/運行映像:

Dockerfile:

FROM python:3.6.0 

WORKDIR /my-app 

ADD . /my-app 

RUN pip install -r requirements.txt 

ENV NAME Scraper 

CMD ["python", "My_App.py"] 

編譯/運行圖像: - 碼頭工人建立-t MYAPP - 泊塢窗運行myapp

也許有一些選擇我不知道我錯過了嗎?

回答

0

你正試圖在linux容器內部運行一個exe文件,這是行不通的。您需要安裝Chrome和chromedriver您Dockerfile內,更新代碼以使用正確的路徑

FROM python:3.6.0 
RUN apt update && apt install -y chromedriver 
WORKDIR /my-app 
ADD . /my-app 
RUN pip install -r requirements.txt 

ENV NAME Scraper 

CMD ["python", "My_App.py"] 

更改您的代碼

options = webdriver.ChromeOptions(); 
options.add_argument('headless') 
options.add_argument('window-size=1400x1300') 
options.add_argument('--mute-audio') 
options.add_argument('--disable-web-security'); 
options.add_argument('--allow-running-insecure-content'); 
options.add_argument('--ignore-certificate-errors') 
options.add_argument('--ignore-ssl-errors') 
prefs = {"profile.managed_default_content_settings.images":2} 
options.add_experimental_option("prefs", prefs); 
path_to_chromedriver = '/usr/lib/chromium/chromedriver';