Docker image with python3, chromedriver, chrome &

2019-06-09 21:24发布

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:

  • python3
  • chromedriver
  • chrome
  • selenium

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.

2条回答
闹够了就滚
2楼-- · 2019-06-09 21:52

I've recently updated this image with selenium included versions:

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 (myself) so may have higher risk of stopping maintenance.

查看更多
叛逆
3楼-- · 2019-06-09 22:02

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.

查看更多
登录 后发表回答