Issue running chrome driver in docker image

Viewed 3207

I have written some scripts to extract some data from website using selenium. Although my code runs well outside of docker container, it fails when I run it after building an image. I searched through the google and looked over the internet to find the similar issue but could not find something similar. Here I have posted my docker file,

# syntax=docker/dockerfile:1

FROM python:latest

WORKDIR /Users/ufomammut/Documents/eplrestapi
COPY requirements.txt requirements.txt

RUN pip3 install -r requirements.txt
RUN curl https://chromedriver.storage.googleapis.com/90.0.4430.24/chromedriver_linux64.zip -O
RUN unzip chromedriver_linux64.zip 

COPY . .


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

The error message I receive when I run the docker image that I built,

 Traceback (most recent call last):
  File "/Users/ufomammut/Documents/eplrestapi/epl.py", line 172, in <module>
    browser = webdriver.Chrome("./chromedriver",options = chr_options)
  File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
    self.service.start()
  File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/common/service.py", line 98, in start
    self.assert_process_still_running()
  File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/common/service.py", line 109, in assert_process_still_running
    raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: Service ./chromedriver unexpectedly exited. Status code was: 255

In my code I have provided the path to the chromedriver as follows:

browser = webdriver.Chrome("./chromedriver",options = chr_options)

Right now I used linux arm64 python base image and was able to curl and unzip the chromedriver as reflected in my dockerfile above.I am no longer receiving the format error but I am receiving this error message as I posted above where it says the chromedriver unexpectedly exited.

5 Answers

Looks like you have been using chromedriver binary for MacOS instead of its linux version.

To make it a 'workable' piece of code, you can try linux binary and perform volume mapping when you try to run your docker image. Use the linux binary for chromedriver and put it in a seperate directory in your system which needs to be mapped. And when you run your image try using:

docker run -v <directory_path_to_chromedriver>:<docker_image_directory_path> <image>

Your final piece of code will be updated with your new binary file path.

browser = webdriver.Chrome("<docker_image_directory_path>/chromedriver",options = chr_options)

But for permanent fix, curl and unzip the linux binary while you build the docker image.

You appear to have copied a chromedriver binary built for MacOS into a Debian machine. Based on how you've tagged this question with macos and the image python:3.9.5-slim-buster you are using is amd64 based on Debian.

I suggest you continue persisting with trying to curl the Linux 64 bit chromedriver into your machine via your Dockerfile.

To test it in your host OS you can simply change your current chromedriver binary to chromedriver.bakup. Download the linux version to chromedriver and rebuild your docker machine and it will copy the linux version into the new image.

After going through the comments, I believe you're a little confused with Docker containers and how they interact with your system.

A Docker container runs it's own independent, isolated and optimised version of an operating system determined by the FROM layer, and leverages your system's kernel.

Now, you must use Chrome Webdriver binaries compatible with linux arm64, rightly pointed out by @Shubham and @lgflorentino. It seems that you've already done so.

Your next steps should be to check permissions on the Chrome Webdriver executables, and make sure that your container has setup all the environment paths and variables correctly.

You can do so by entering your container with the command docker exec -it /bin/bash or /bin/sh and then manually execute your bins. This will give you a clearer picture.

Check your environment variables as well! Also, an improved Dockerfile with reduced number of layers can be as well.

FROM python:latest
#Use a deterministic Python image, mention a version instead of 'latest'
WORKDIR /Users/ufomammut/Documents/eplrestapi
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt && \
    curl https://chromedriver.storage.googleapis.com/90.0.4430.24/chromedriver_linux64.zip -O && \
    unzip chromedriver_linux64.zip 

COPY . .
#Try adding an ENV layer to make sure your paths are setup properly. (After debugging your container)
CMD [ "python3", "epl.py"]

Since you have tagged with macOs and ARM64, I believe that your development machine has Apple Silicon or M1 chip. The docker engine on the Apple Silicon machine will by default pulls ARM64 based images and your chrome driver is of linux64. This means your chrome driver can't run inside the host.

You can either use arm64 chrome driver or build docker image form amd64 platform.

docker build -t your-username/multiarch-example:manifest-amd64 --build-arg ARCH=amd64

Try adding the --platform=linux/amd64 to the FROM statement. I used this for Debian:

FROM --platform=linux/amd64 python:3.9

Related