joyzoursky / docker-python-chromedriver

Dockerfile for running Python Selenium in headless Chrome (Python 2.7 / 3.6 / 3.7 / 3.8 / Alpine based Python / Chromedriver / Selenium / Xvfb included in different versions)
https://hub.docker.com/r/joyzoursky/python-chromedriver/
MIT License
638 stars 196 forks source link
chromedriver docker dockerfile headless-chrome python selenium xvfb

docker-python-chromedriver (Deprecated)

Python with Chromedriver, for running automated tests

Quick Try

$ git clone https://github.com/joyzoursky/docker-python-chromedriver.git
$ cd docker-python-chromedriver
$ docker run -it -w /usr/workspace -v $(pwd):/usr/workspace joyzoursky/python-chromedriver:latest bash
/usr/workspace# pip install selenium
/usr/workspace# python test_script.py

Warning: Current versions only support for building and running on amd64 (aka x86-64) machines, for arm64 (e.g. Apple M1 chip) machines, sevaral issues are found to be blocking (ref https://github.com/joyzoursky/docker-python-chromedriver/issues/31 https://github.com/joyzoursky/docker-python-chromedriver/issues/30).

Image includes

Versions

Python 3 (Debian-based)

Python 3 (Alpine-based)

Python 2 (Deprecated)

Versions with Xvfb (Deprecated)

Usage

For the following Debian-based images:

Run the followings in your terminal:

$ cd [your working directory]
$ docker run -it -w /usr/workspace -v $(pwd):/usr/workspace joyzoursky/python-chromedriver:[version] bash

For the following Alpine-based images:

Run the followings in your terminal:

$ cd [your working directory]
$ docker run -it -w /usr/workspace -v $(pwd):/usr/workspace joyzoursky/python-chromedriver:[version] sh

This will create a container from the image. Then you could starting running the commands in the container as in your working directory. The followings are some examples to run a selenium test.

Examples to run selenium test in the container

You can download a selenium test example from here to quick start.

For the following images with selenium pre-installed:

You may run:

# python test_script.py

For the following images that do not have selenium pre-installed:

You may run:

# pip install selenium
# python test_script.py

For 3.6-xvfb or 2.7-xvfb, you may run:

# pip install selenium
# xvfb-run --server-args="-screen 0 1024x768x24" python test_script.py

For 3.6-xvfb-selenium or 2.7-xvfb-selenium, you may run:

# xvfb-run --server-args="-screen 0 1024x768x24" python test_script.py

Then you should see the test result like this:

test_case_1 (__main__.TestTemplate)
Find and click top-left logo button ... ok
test_case_2 (__main__.TestTemplate)
Find and click top-right Start your project button ... ok

----------------------------------------------------------------------
Ran 2 tests in 11.852s

OK

Contributing

To make changes upon the existing Dockerfiles, you are suggested to edit upon the Dockerfile.template files, and generate the new Dockerfiles for different versions by running render.sh.

How to edit and generate new versions of Dockerfiles

  1. cd to the folder that you want to edit, e.g. py-debian
  2. Make changes upon Dockerfile.template or Dockerfile-selenium.template
  3. Run bash render.sh from command-line

Note: The render.sh is written for Mac OS; If you are using Windows or other OS, some commands may need to be optimised before running.

Thanks a lot for helping on improving this project!