Docker image with python3, chromedriver, chrome & selenium

一曲冷凌霜 提交于 2019-12-06 23:45:30

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'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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!