Docker

How to Use Docker For Your Software, Automation Framework and Tools

Since some day I have been playing with docker to make it part of our CI. I would like to put my thought here and benefit of using docker.

So first of all, let’s understand what is Docker:

Docker is open source platform which help to package, distribute and run your application.Docker provider efficient and easy way to encapsulate all your dependencies e.g. java application, tomcat, database and make the image which then can be run on any required infrastructure. Docker image can be share on public or across organization to use without any dependency from ‘docker registory’.

So basically, Docker provide tools which can be use to create docker container. So Docker container is kind of light weighted virtual machine with considerable smaller memory and disk space.

There are lot of article over the net which explain Docker really well.

Let me come to the point where It could be really beneficial on Test Automation and Tools:

Let’s imagine, you have Selenium test automation framework or any api test framework which build using different tools like, python/java/ruby/scala/javascript, selenium/watir/robotium/appium, git/svn and whatever technology you know about which you are using in your framework. I guess, you would have faced the problem when someone else want to use your framework or tests without investing too much time and figuring out how to use your framework or run the tests, specially QA and Developers. Things get more complicated when you want to make your Test Automation framework part of the “Continuous Integration” and you need to figure out how to deploy and run test on Jenkins/Bamboo/BuildBot or whatever tool you use. Docker comes to rescue and it can make this challenge go away.

We discuss about the problem which Docker solve but what actually it will solve:

Let say you have docker image of your framework(by the way, that’s really easy to create) and some other team member(QA or developer) want to use it, you will just ask them to download your docker image and they can straightly go and run your test without understanding or figuring out any setup installation of your framework or tests. It’s really great way to setup test framework or tests on your team members system or jenkins.

Let me show you how you do that step by step:

  1. First create a Dockerfile like => touch Dockerfile
  2. Now put following  contains on your Docker file

(You don’t need to use each line from below contain, it could be used as reference and for knowledge)

Before going ahead, let’s get familiar with some of the keyword used in Dockerfile:

  • RUN => Run the command at build time
  • CMD => Run the command at Run time
  • ADD => add local directory in Container
  • ARG => Provide command line argument while building the Dockerfile
  • FROM => Get the parent image
  • USER => User to set
  • ENV => Set the environment
  • PORT => Port to use

Here we are using Ubuntu image with specific versions for Chrome and Python. This part will download ubuntu:14.04 image in the container and will set the ENV values for python and chrome driver.

FROM ubuntu:14.04
MAINTAINER Sunil Kapil <snlkapil@gmail.com>
ARG SSH_KEY_LOC
ENV CHROME_DRIVER_VERSION 2.14
ENV PYTHON_VERSION 2.7.10
ENV PYTHON_PIP_VERSION 7.0.3
ENV LANG C.UTF-8
ENV DISPLAY :99
ENV UID 1000
ENV GID 1000

Now we are making user as root. and installing packages: git and curl

USER root
#================================================
# Add dedicated user
#================================================
RUN apt-get -y install sudo
# Install software 
RUN apt-get update
RUN apt-get install -y git
RUN apt-get install -y curl

You can skip this part if you don’t want to clone your repo into the container. There are different way to get your repo inside container. I will explain what other way to avoid git if you don’t want to do this.

#========================
# GIT Configuration
#========================
RUN mkdir -p /opt/git/.ssh
RUN mkdir -p /root/.ssh
RUN chmod -R 700 /root/.ssh
ADD ${SSH_KEY_LOC} /opt/git/.ssh/id_rsa
RUN chmod -R 700 /opt/git/.ssh
RUN echo "\n\
Host github.com\n\ 
 HostName github.com\n\ 
 Port 7999\n\
 User git\n\
 IdentityFile /opt/git/.ssh/id_rsa\n\
 StrictHostKeyChecking no\n\
 UserKnownHostsFile=/dev/null\n\
" >> /root/.ssh/config

Here we are customizing source for apt-get(linux command to install any package)

#================================================
# Customize sources for apt-get
#================================================
RUN echo "deb http://archive.ubuntu.com/ubuntu trusty main universe\n" > /etc/apt/sources.list \
 && echo "deb http://archive.ubuntu.com/ubuntu trusty-updates main universe\n" >> /etc/apt/sources.list
RUN apt-get update -y \
 && apt-get -y install build-essential wget unzip curl xvfb xz-utils zlib1g-dev libssl-dev

Install Google Chrome browser

#===============
# Google Chrome
#===============
RUN wget -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
 && echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list \
 && apt-get update -y \
 && apt-get -y install google-chrome-stable \
 && rm /etc/apt/sources.list.d/google-chrome.list \
 && rm -rf /var/lib/apt/lists/*

Install Chrome Driver (you only need if your tests using selenium webdriver) neither you can skip this part.

#==================
# Chrome webdriver
#==================
RUN wget --no-verbose -O /tmp/chromedriver_linux64.zip http://chromedriver.storage.googleapis.com/$CHROME_DRIVER_VERSION/chromedriver_linux64.zip \
 && rm -rf /opt/selenium/chromedriver \
 && unzip /tmp/chromedriver_linux64.zip -d /opt/selenium \
 && rm /tmp/chromedriver_linux64.zip \
 && mv /opt/selenium/chromedriver /opt/selenium/chromedriver-$CHROME_DRIVER_VERSION \
 && chmod 755 /opt/selenium/chromedriver-$CHROME_DRIVER_VERSION \
 && ln -fs /opt/selenium/chromedriver-$CHROME_DRIVER_VERSION /usr/bin/chromedriver

Install firefox update and driver

#=========
# Firefox
#=========
RUN apt-get update -qqy \
 && apt-get -qqy --no-install-recommends install \
 firefox \
 && rm -rf /var/lib/apt/lists/*

If you are using python on your project this is way to do it, if you are using java/ruby/scala/javascript or any other language, you can google it and you will find the way to install on linux

#==================
# Python
#==================
RUN apt-get purge -y python.*
RUN gpg --keyserver ha.pool.sks-keyservers.net --recv-keys C01E1CAD5EA2C4F0B8E3571504C367C218ADD4FF
RUN set -x \
 && mkdir -p /usr/src/python \
 && curl -SL "https://www.python.org/ftp/python/$PYTHON_VERSION/Python-$PYTHON_VERSION.tar.xz" -o python.tar.xz \
 && curl -SL "https://www.python.org/ftp/python/$PYTHON_VERSION/Python-$PYTHON_VERSION.tar.xz.asc" -o python.tar.xz.asc \
 && gpg --verify python.tar.xz.asc \
 && tar -xJC /usr/src/python --strip-components=1 -f python.tar.xz \
 && rm python.tar.xz* \
 && cd /usr/src/python \
 && ./configure --enable-shared --enable-unicode=ucs4 \
 && make -j$(nproc) \
 && make install \
 && ldconfig \
 && curl -SL 'https://bootstrap.pypa.io/get-pip.py' | python2 \
 && pip install --upgrade pip==$PYTHON_PIP_VERSION \
 && find /usr/local \
 \( -type d -a -name test -o -name tests \) \
 -o \( -type f -a -name '*.pyc' -o -name '*.pyo' \) \
 -exec rm -rf '{}' + \
 && rm -rf /usr/src/python

Here we are using pip to install selenium client and unittest, you can install anything using pip, just put those pip command here.

RUN pip install -I selenium==2.45.0 unittest-xml-reporting==1.12.0

Here we are install VNC, it’s required if your tests are Web based and using selenium/watir to run on GUI mode.

#=====
# VNC
#=====
RUN apt-get update -y \
 && apt-get -y install \
 x11vnc \
 && rm -rf /var/lib/apt/lists/* \
 && mkdir -p ~/.vnc \
 && x11vnc -storepasswd secret ~/.vnc/passwd

Fluxbox is window manger, this is also optional, depend upon your need.

#=========
# fluxbox
# A fast, lightweight and responsive window manager
#=========
RUN apt-get update -qqy \
 && apt-get -qqy install \
 fluxbox \
 && rm -rf /var/lib/apt/lists/*

To run webdriver tests on headless mode, installing Xvfb, you can install phantomjs  as well.

#==================
# Xvfb + init scripts
#==================
RUN apt-get update -y 
RUN apt-get install -y unzip xvfb
ENV DISPLAY :99

Some other settings which I have put on comment section.

#Install google chome
RUN chmod a+x /usr/bin/google-chrome
#=================
# Locale settings
#=================
ENV LANGUAGE en_US.UTF-8
ENV LANG en_US.UTF-8
RUN locale-gen en_US.UTF-8 \
 && dpkg-reconfigure --frontend noninteractive locales \
 && apt-get update -qqy \
 && apt-get -qqy --no-install-recommends install \
 language-pack-en \
 && rm -rf /var/lib/apt/lists/*
#=======
# Fonts
#=======
RUN apt-get update -qqy \
 && apt-get -qqy --no-install-recommends install \
 fonts-ipafont-gothic \
 xfonts-100dpi \
 xfonts-75dpi \
 xfonts-cyrillic \
 xfonts-scalable \
 && rm -rf /var/lib/apt/lists/*
#=========
#Clean up
#=========
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* ~/.config/google-chrome

Here we are cloning the git repo on container.

#check out your git repo
RUN git clone <your github repo>
RUN git clone <your github repo>

If  you don’t want to clone, you can directly use below command. before adding below line make sure you have cloned git repo in your current directory (Dockerfile location)

#You can also use below command if you don't want to clone it
#ADD <absolute name your local directory name> /opt/git/

Here you can run your tests inside the container by adding this line or better to run outside of container by providing command like this way. This will run at the run time.

docker run <image_name> python <test_name> or docker run <image_name> <your_test_command>

I have this Dockerfile here for use: https://github.com/skapil/dockerexample

I hope this will help to start with docker. Please put your comment below for any question or new information, so we can learn together.

Advertisements

2 thoughts on “How to Use Docker For Your Software, Automation Framework and Tools

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s