Configuring Selenium Firefox Docker with Gunicorn Flask application
I choose Firefox 45.0.2 and Selenium 2.53.1 because I can trust this combination.
Let's discuss the Docker-file
part-by-part.
Setup Firefox
# Firefox Setup
RUN wget https://ftp.mozilla.org/pub/mozilla.org/firefox/releases/45.0.2/linux-x86_64/en-US/firefox-45.0.2.tar.bz2
RUN tar -xjvf firefox*.tar.bz2
RUN mv firefox /opt/firefox
RUN ln -sf /opt/firefox/firefox /usr/bin/firefox
Install Xvfb(X Virtual framebuffer)
# xvfb install
RUN apt-get install -y xvfb
Xvfb enables you to run graphical applications without a display.
# Environment Varaibles
ENV DISPLAY=:10
Install libasound2
RUN apt-get install -y libasound2
Install Gunicorn
RUN apt-get update -y
RUN apt-get install -y python3-pip build-essential python3-dev nginx
RUN pip install -U gunicorn
Expose 5056
#port to run the flask application
Have a look at Dockerfile now
Requirements.txt
selenium==2.53.1
Flask==1.0.2
Setup Flask api
Simple flask application to extract html with the GET request of the url.
Setup GUnicorn(Green Unicorn)
It is a Python WSGI(Web Server Gateway Interface) HTTP Server. To run the flask api, we have to create wsgi.py and import the app.
Now, we have to run the Xvfb in the background whenever the docker instance called along with the Gunicorn server. The best way to do both jobs is by using shell script with ENTRYPOINT
in docker.
ENTRYPOINT [“sh”, “start.sh”]
Make sure the Xvfb port
and environment variable $DISPLAY
are the same. Both set as :10
in this tutorial.
The Complete code of this project can be seen here.
Detailed steps of building the docker and running the Flask API with selenium webdriver are mentioned in the project.
Confirmation
After everything is set. Enter this http://localhost:5056/pageSource?url=https://httpbin.org/ip
in browser
Thanks for reading! If you like the concept, please don’t forget to endorse my skills on Linkedin. Please clap & share if you like.