Can't run Selenium inside Docker

Question:

Please help me to run Selenium inside Docker. Below is my code which is not working. I am adding error message as well.

Dockerfile:

FROM python:3.8-slim

# Install the Selenium Python bindings
RUN pip install selenium

# Download the ChromeDriver executable and add it to the PATH
RUN wget -O /tmp/chromedriver.zip https://chromedriver.storage.googleapis.com/2.46/chromedriver_linux64.zip 
    && unzip /tmp/chromedriver.zip -d /usr/local/bin 
    && rm /tmp/chromedriver.zip
ENV PATH="/usr/local/bin:${PATH}"

docker-compose:

version: '3'
services:
  selenium:
    image: selenium/standalone-chrome
    ports:
      - "4444:4444"
      - "5900:5900"
  webapp:
    build: .
    volumes:
      - .:/app
    command: python3 test.py
    depends_on:
      - selenium

test.py code snippet:

from selenium import webdriver

driver = webdriver.Remote(
    command_executor='http://selenium:4444/wd/hub',
    desired_capabilities={'browserName': 'chrome'}
)

driver.get('https://www.google.com')
driver.save_screenshot('search_results.png')
driver.quit()

Command to run:

docker-compose up

Error stacktrace:

ERROR [3/3] RUN wget -O /tmp/chromedriver.zip https://chromedriver.storage.googleapis.com/2.46/chromedriver_linux64.zip     0.8s 
------
 > [3/3] RUN wget -O /tmp/chromedriver.zip https://chromedriver.storage.googleapis.com/2.46/chromedriver_linux64.zip     && unzip /tm
p/chromedriver.zip -d /usr/local/bin     && rm /tmp/chromedriver.zip:
#0 0.736 /bin/sh: 1: wget: not found
------
failed to solve: executor failed running [/bin/sh -c wget -O /tmp/chromedriver.zip https://chromedriver.storage.googleapis.com/2.46/c
hromedriver_linux64.zip     && unzip /tmp/chromedriver.zip -d /usr/local/bin     && rm /tmp/chromedriver.zip]: exit code: 127 

Additional notes:
I am tring to run it on my local machine and my machine is Windows 10.

Please note that all files are in one main directory. What is the wrong here? Please help me to fix it.

UPDATE for Dockerfile:

FROM python:3.8-slim

# Install the Selenium Python bindings
RUN pip install selenium

RUN apt update && apt install -y wget

# Download the ChromeDriver executable and add it to the PATH
RUN wget -O /tmp/chromedriver.zip https://chromedriver.storage.googleapis.com/2.46/chromedriver_linux64.zip 
    && unzip /tmp/chromedriver.zip -d /usr/local/bin 
    && rm /tmp/chromedriver.zip
ENV PATH="/usr/local/bin:${PATH}"

New error message:

#0 2.034 2022-12-17 08:19:54 (6.27 MB/s) - ‘/tmp/chromedriver.zip’ saved [5404417/5404417]
#0 2.034
#0 2.037 /bin/sh: 1: unzip: not found
------
failed to solve: executor failed running [/bin/sh -c wget -O /tmp/chromedriver.zip https://chromedriver.storage.googleapis.com/2.46/c
hromedriver_linux64.zip     && unzip /tmp/chromedriver.zip -d /usr/local/bin     && rm /tmp/chromedriver.zip]: exit code: 127 
Asked By: Micky

||

Answers:

You should add just after RUN pip install selenium :

apt update && apt install -y wget zip
Answered By: Philippe