I'm trying to dockerize and run the web scrapper developed using the selenium library in python. I used Windows 10 for development. It ran well there. While running the same script as a docker image, I'm getting multiple issues. This is how I connect the driver in windows.
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
I didn't use options as I don't have any use cases. As I got root user error while running in docker I added the option and ran the code as below.
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
driver = webdriver.Chrome(options = chrome_options, service=Service(ChromeDriverManager().install()))
Still, it didn't start. So I configured it by hardcoding the driver path.
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
driver = webdriver.Chrome(executable_path=driverPath,options=option)
Even then it didn't get started as the display was not configured. So configured the headless argument and ran, but in the end, I got the below error.
**
Tkinter.TclError: no display name and no $DISPLAY environment variable
**
So I tried to start the display by the below code.
if platform.system() == 'Linux':
from pyvirtualdisplay import Display
display = Display(visible=0, size=(800, 800))
display.start()
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
driver = webdriver.Chrome(executable_path=driverPath,options=option)
But it is not running, it is frozen and not creating the driver session.
This is my Dockerfile
FROM python
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.list
RUN apt-get update && apt-get -y install google-chrome-stable
RUN apt-get install -yqq unzip
RUN wget -O /tmp/chromedriver.zip http://chromedriver.storage.googleapis.com/`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE`/chromedriver_linux64.zip
RUN unzip /tmp/chromedriver.zip chromedriver -d /usr/local/bin/
RUN apt-get install xvfb mesa-utils -y \
&& apt install freeglut3-dev -y
ENV DISPLAY=:99
RUN mkdir -p /app/drivers
ADD requirements.txt /app
ADD sample.py /app
COPY run.sh /app
COPY drivers /app/drivers
COPY csv /app/csv
WORKDIR /app
RUN pip3 install -r requirements.txt
CMD ./run.sh
run.sh
#!/bin/sh
#Xvfb :99 -screen 0 640x480x8 -nolisten tcp &
python3 ./sample.py
requirements.txt
selenium==4.3.0
webdriver-manager==3.8.2
chromedriver-py==103.0.5060.53
pyvirtualdisplay==3.0
What are the mistakes I made in the code? And how to run the selenium python app with display in docker? Thank you.