Install Selenium on Docker

I am gone to show how to run Selenium server and Selenium PHP client driver especially the Facebook version.
For more information about that driver you can get from here.

NOTE: If you don't want to through whole process and you have installed docker, just pull it from my docker hub repo at: https://registry.hub.docker.com/u/gzekiii/selenium-with-php-facebook/

For example:
 docker run -it -h selenium --name="selenium" -p=80:80 gzekiii/selenium-with-php-facebook /bin/sh -c "if [ -f /run.sh ]; then bash /run.sh; fi; exec /bin/bash"


Install Docker


Update your repository and then install Docker.
apt-get update && apt-get install docker.io -y

Create Docker container based on latest Ubuntu image


I'll use the lastest Ubuntu version (14.04.2)
docker run -i -t --name="selenium" -p=80:80 ubuntu:14.04.2 /bin/sh -c "if [ -f /run.sh ]; then bash /run.sh; fi; exec /bin/bash"
NOTE: Be careful with "-p=80:80" part of command below, it will be public accessible.
Now, you are in container and if you pay attention you will see that your hostname is different than previous one.

Install and Configure Selenium server


Update your repository and then install all the necessary packets.
apt-get update && apt-get install vim default-jre xvfb firefox nginx php5-fpm php5-cli php5-curl git -y

Create directory "selenium" in "/usr/lib/".
mkdir /usr/lib/selenium

Move to new directory you just have created.
cd /usr/lib/selenium

Download the last version of Selenium server. In my case it's 2.45.0.
curl --remote-name http://selenium-release.storage.googleapis.com/2.45/selenium-server-standalone-2.45.0.jar

Run Xvfb instance.
 
Xvfb :1 -ac -screen 0 1024x768x8 > /tmp/xvfb_1.log 2>&1 &

When X client start it will look at DISPLAY for X Server. So set right value for environment variables DISPALY.
export DISPLAY=localhost:1.0

Start Selenium server with nohup to prevent termination.
NOTE: the part "-Djava.security.egd=file:/dev/./urandom" is optional but if you have trouble with entropy you must use it.
For more details visit GitHub SeleniumHQ/docker-selenium
nohup java -jar selenium-server-standalone-2.45.0.jar -Djava.security.egd=file:/dev/./urandom > /tmp/selenium.log &
You must see message "nohup: ignoring input and redirecting stderr to stdout" just strike "enter".

Install and Configure Selenium client


Open up /etc/nginx/sites-available/default file.
vi /etc/nginx/sites-available/default

Find the line.
index index.html index.htm;

Replace it with.
index index.php index.html index.htm;

In the same file find lines.
#location ~ \.php$ {
#	fastcgi_split_path_info ^(.+\.php)(/.+)$;
#	# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
#
#	# With php5-cgi alone:
#	fastcgi_pass 127.0.0.1:9000;
#	# With php5-fpm:
#	fastcgi_pass unix:/var/run/php5-fpm.sock;
#	fastcgi_index index.php;
#	include fastcgi_params;
#}

Replace them with.
location ~ \.php$ {
	try_files $uri =404;

	fastcgi_pass unix:/var/run/php5-fpm.sock;
	fastcgi_index index.php;
	fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
	include fastcgi_params; 
}

Move to default nginx public directory.
cd /usr/share/nginx/html/

In current version there are two files, we don't need them, let's delete them.
rm -r *

Download Composer.
curl -sS https://getcomposer.org/installer | php

Create composer.json file.
echo '{
  "require": {
    "facebook/webdriver": "dev-master"
  }
}
' > composer.json

Install "facebook/webdriver" using Composer.
php composer.phar install

Create and open up file with index.php name.
vi index.php

Then put inside this PHP code.
<?php
require "./vendor/autoload.php";

$host = "http://localhost:4444/wd/hub";
$driver = RemoteWebDriver::create($host, DesiredCapabilities::firefox());
$driver->get("http://google.com");

echo $driver->getTitle();
$driver->close();

That script will open http://google.com then it will take the page's title and close the driver.
Yes, it doesn't make so much, but enough to test what we've done.

Create and open up file with run.sh name in /.
vi /run.sh

Then put inside code below and save.
rm /tmp/* -r -f
rm -rf /tmp/.* 2> /dev/null
Xvfb :1 -ac -screen 0 1024x768x8 > /tmp/xvfb_1.log 2>&1 &
export DISPLAY=localhost:1.0
nohup java -jar /usr/lib/selenium/selenium-server-standalone-2.45.0.jar -Djava.security.egd=file:/dev/./urandom > /tmp/selenium.log &
service php5-fpm start
service nginx start

Exit from this container.
exit

Well, that all you container is ready, you can start it.
docker start selenium

And you can access it from you machine as well as public.