This is a simple example of how to use Docker, Docker-compose and Selenium together to run a simple Python script.
In this case, we can use three different ways to run the script:
- Native
- Dockerfile
- Docker-compose with Simulated Web Driver
To run the script natively, we can use the next commands.
-
You need to install Chrome or Firefox.
-
Start the workspace.
virtualenv venv source venv/bin/activate
-
Edit the file .env to the workspace.
As example, can be
source/.env
ENVIRONMENT=local #ENVIRONMENT=docker #ENVIRONMENT=remote PATH_ASSETS=assets #BROWSER=chrome #EXTENSIONS_CHROME=["metamask"] BROWSER=firefox EXTENSIONS_FIREFOX=["metamask", "captcha"] REMOTE_URL=http://selenium-hub:4444/wd/hub #REMOTE_URL=http://0.0.0.0:4444/wd/hub #REMOTE_URL=http://localhost:4444/wd/hub METAMASK_AUTH=["SECRET", "A B C D E F G H I J K L"]
-
Inside to the folder
source
cd source
-
Install the dependencies.
python -m pip install --upgrade pip pip install -r requirements.txt
-
Execute the script.
python main.py
-
Stop the workspace.
deactivate
-
Edit the file .env to the workspace.
As example, can be
source/.env
#ENVIRONMENT=local ENVIRONMENT=docker #ENVIRONMENT=remote PATH_ASSETS=assets BROWSER=chrome EXTENSIONS_CHROME=["metamask", "captcha"] #BROWSER=firefox #EXTENSIONS_FIREFOX=["metamask"] REMOTE_URL=http://selenium-hub:4444/wd/hub #REMOTE_URL=http://0.0.0.0:4444/wd/hub #REMOTE_URL=http://localhost:4444/wd/hub METAMASK_AUTH=["SECRET", "A B C D E F G H I J K L"]
-
Build the Docker image.
docker build -f docker/single/Dockerfile -t selenium-python-docker .
-
Run the Docker image.
docker run -it --rm -v $(pwd)/source:/usr/src/app -w /usr/src/app --env-file=.env selenium-python-docker
-
Edit the file .env to the workspace.
As example, can be
source/.env
#ENVIRONMENT=local #ENVIRONMENT=docker ENVIRONMENT=remote PATH_ASSETS=assets BROWSER=chrome EXTENSIONS_CHROME=["metamask"] #BROWSER=firefox #EXTENSIONS_FIREFOX=["metamask"] REMOTE_URL=http://selenium-hub:4444/wd/hub #REMOTE_URL=http://0.0.0.0:4444/wd/hub #REMOTE_URL=http://localhost:4444/wd/hub METAMASK_AUTH=["SECRET", "A B C D E F G H I J K L"]
-
Build the Docker image.
docker-compose build
-
Up the Docker image.
docker-compose up
-
Execute the script in docker service.
docker-compose exec -it app bash
Or execute the script in the container.
docker exec -it $(docker ps | grep app | awk '{print $1}') bash # python main.py
-
In the localhost:4000, you can see the result in session, note: the password of session vnc is
secret
-
Stop the Docker image.
docker-compose down