Selenium Grid Server works on port 4444 and I want another container for Flask API that would use Selenium Chrome Driver to scrape websites.
Flask API works outside of the Docker container but when I build Docker container it gives urllib3 Exceptions:
urllib3.exceptions.MaxRetryError
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='127.0.0.1', port=4444): Max retries exceeded with url: /wd/hub/session (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fd542587760>: Failed to establish a new connection: [Errno 111] Connection refused'))
Selenium Chrome Container:
docker run -d -p 4444:4444 -p 5900:5900 -v /dev/shm:/dev/shm selenium/standalone-chrome-debug:3.141.59-yttrium
Flask API:
from selenium import webdriver
from selenium.webdriver import DesiredCapabilities
from flask import Flask
app = Flask(__name__)
@app.route('/')
def title():
driver = webdriver.Remote("http://127.0.0.1:4444/wd/hub", DesiredCapabilities.CHROME)
driver.get("http://www.python.org")
driver.close()
return driver.title
if __name__ == '__main__':
app.run(host="0.0.0.0", port=3000, debug=True, use_reloader=True)
Dockerfile
FROM ubuntu:latest
RUN apt-get update && apt-get upgrade -y && apt-get clean
RUN apt-get install -y curl python3.8 python3.8-dev python3.8-distutils
RUN curl -s https://bootstrap.pypa.io/get-pip.py -o get-pip.py && \
python3 get-pip.py --force-reinstall && \
rm get-pip.py
RUN pip3 install -U selenium
RUN apt-get update -y
WORKDIR /app
COPY . /app
RUN pip3 --no-cache-dir install -r requirements.txt
EXPOSE 3000
ENTRYPOINT ["python3"]
CMD ["app.py"]