I am new to docker world and I've a requirement for an AWS codebuild project where I need to use selenium/hub image and Dockerfile to run some UI tests. So far what I've done is:
Create docker-compose.yaml file to pull selenium/hub and selenium/node-chrome Create buildspec.yaml file with docker-compose up --build -d and other commands A Dockerfile to build the image FROM python:latest and install some dependencies A small python file to open google.ca using webdriver.Remote
When I build the project in AWS using Code pipeline, I am able to successfully get past initially where it spins up selenium/grid but when I try to run the command "docker run" it errors out as "connection refused"
> [Container] 2022/10/06 19:09:16 Running command docker run
> mygridimage:ver1
> ============================= test session starts ============================== platform
linux -- Python 3.10.7, pytest-7.1.2, pluggy-1.0.0 -- /usr/local/bin/python cachedir:
> .pytest_cache metadata: {'Python': '3.10.7', 'Platform':
> 'Linux-4.14.281-212.502.amzn2.x86_64-x86_64-with-glibc2.31',
> 'Packages': {'pytest': '7.1.2', 'py': '1.11.0', 'pluggy': '1.0.0'},
> 'Plugins': {'html': '3.1.1', 'metadata': '2.0.2'}} rootdir: /app
> plugins: html-3.1.1, metadata-2.0.2 collecting ... collected 0 items /
> 1 error
>
> ==================================== ERRORS ====================================
> ___________________________ ERROR collecting test.py ___________________________ /usr/local/lib/python3.10/site-packages/urllib3/connection.py:174: in
> _new_conn
> conn = connection.create_connection( /usr/local/lib/python3.10/site-packages/urllib3/util/connection.py:95:
> in create_connection
> raise err /usr/local/lib/python3.10/site-packages/urllib3/util/connection.py:85:
> in create_connection
> sock.connect(sa) E ConnectionRefusedError: [Errno 111] Connection refused
These are my files.
buildspec.yaml
--------------
version: 0.2
phases:
pre_build:
commands:
- docker-compose up --build -d
- docker build -t mygridimage:ver1 .
- docker images
- docker ps -a
- docker run mygridimage:ver1
build:
commands:
reports:
pytest_reports:
files:
base-directory: /reports
file-format: JUNITXML
artifacts:
files:
- "**/*"
Dockerfile
----------
FROM python:latest
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
ADD . /app
# Install any needed packages specified in requirements.txt
#RUN pip install --trusted-host pypi.python.org -r requirements.txt
RUN pip3 install -U -r requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 80
# Run app.py when the container launches
ENTRYPOINT ["python", "-m", "pytest", "-v", "-s", "test.py"]
docker-compose.yaml
-------------------
version: '3.8'
services:
selenium-hub:
image: selenium/hub
container_name: selenium-hub
ports:
- '4444:4444'
chrome:
image: selenium/node-chrome
volumes:
- '/dev/shm:/dev/shm'
depends_on:
- selenium-hub
environment:
- HUB_HOST=selenium-hub
- HUB_PORT=4444
requirements.txt
----------------
selenium==4.1.3
pytest==7.1.2
pytest-html==3.1.1
test.py
-------
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
driver = webdriver.Remote(command_executor='http://localhost:4444/wd/hub',
desired_capabilities=DesiredCapabilities.CHROME)
URL = "https://www.google.ca"
driver.get(URL)
title = driver.title
print('Title------------>', title)
I am not sure what mistake I am doing here... The error happens in this line from buildspec.yaml
docker run mygridimage:ver1
Thanks for your help.