简体   繁体   中英

Docker image with python3, chromedriver, chrome & selenium

My objective is to scrape the web with Selenium driven by Python from a docker container .

I've looked around for and not found a docker image with all of the following installed:

Is anyone able to link me to a docker image with all of these installed and working together?

Perhaps building my own isn't as difficult as I think, but it's alluded me thus far.

Any and all advice appreciated.

Try https://github.com/SeleniumHQ/docker-selenium .

It has python installed:

$ docker run selenium/standalone-chrome python3 --version
Python 3.5.2

The instructions indicate you start it with

docker run -d -p 4444:4444 --shm-size=2g selenium/standalone-chrome

Edit:

To allow selenium to run through python it appears you need to install the packages. Create this Dockerfile :

FROM selenium/standalone-chrome

USER root
RUN wget https://bootstrap.pypa.io/get-pip.py
RUN python3 get-pip.py
RUN python3 -m pip install selenium

Then you could run it with

docker build . -t selenium-chrome && \
    docker run -it selenium-chrome python3

The advantage compared to the plain python docker image is that you won't need to install the chromedriver itself since it comes from selenium/standalone-chrome .

I like Harald's solution. However, as of 2021, my environment needed some modifications.

Docker version 20.10.5, build 55c4c88

I changed the Dockerfile as follows.

FROM selenium/standalone-chrome

USER root
RUN apt-get update && apt-get install python3-distutils -y
RUN wget https://bootstrap.pypa.io/get-pip.py
RUN python3 get-pip.py
RUN python3 -m pip install selenium

https://hub.docker.com/r/joyzoursky/python-chromedriver/

It uses python3 as base image and install chromedriver, chrome and selenium (as a pip package) to build. I used the alpine based python3 version for myself, as the image size is smaller.

$ cd [your working directory]
$ docker run -it -v $(pwd):/usr/workspace joyzoursky/python-chromedriver:3.6-alpine3.7-selenium sh
/ # cd /usr/workspace

See if the images suit your case, as you could pip install selenium with other packages together by a requirements.txt file to build your own image, or take reference from the Dockerfiles of this.


If you want to pip install more packages apart from selenium, you could build your own image as this example:

First, in your working directory, you may have a requirements.txt storing the package versions you want to install:

selenium==3.8.0
requests==2.18.4
urllib3==1.22
... (your list of packages)

Then create the Dockerfile in the same directory like this:

FROM joyzoursky/python-chromedriver:3.6-alpine3.7

RUN mkdir packages
ADD requirements.txt packages
RUN pip install -r packages/requirements.txt

Then build the image:

docker build -t yourimage .

This differs with the selenium official one as selenium is installed as a pip package to a python base image. Yet it is hosted by individual so may have higher risk of stopping maintenance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM