Docker image with python3, chromedriver, chrome & selenium
Try https://github.com/SeleniumHQ/docker-selenium.
It has python installed:
$ docker run selenium/standalone-chrome python3 --versionPython 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-chromeUSER rootRUN wget https://bootstrap.pypa.io/get-pip.pyRUN python3 get-pip.pyRUN 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-chromeUSER rootRUN apt-get update && apt-get install python3-distutils -yRUN wget https://bootstrap.pypa.io/get-pip.pyRUN python3 get-pip.pyRUN 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.0requests==2.18.4urllib3==1.22... (your list of packages)
Then create the Dockerfile in the same directory like this:
FROM joyzoursky/python-chromedriver:3.6-alpine3.7RUN mkdir packagesADD requirements.txt packagesRUN 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.