Dockerize your Ruby Cucumber project

Seçil Kaplan
hepsiburadatech
2 min readApr 3, 2019

--

Me and my colleague are writing tests with Ruby on a Windows environment and run them on Linux Selenium Grid. We are using TeamCity as a continuous integration (CI) tool on Windows servers. Our Ruby project was 2.0.0 but we had to use newer gem dependencies so we were forced to upgrade Ruby to 2.5.3.

Also, since my team has new projects and infrastructure we needed to adapt our tests accordingly. Installing Ruby and dependencies in every CI server/agent is frustrating. The solution was using Docker which performs operating-system-level virtualization.

Dockerizing means running your project in a virtual container which includes every dependency you need. Setting up your environment will be just building your docker image. For this, you will need Docker in your local environment.

https://docs.docker.com/install/

The “magical” Dockerfile

The first thing is to write the Dockerfile and put it in your project’s main folder. It is the file that creates your image. You can sign up for the docker hub and run a search to find a base image.

https://hub.docker.com/

My Dockerfile was the one below. I preferred to use alpine as a base image because it is lighter.

Dockerfile to build Ruby Cucumber project

Build an image

After creating the Dockerfile you can build your image with the command:

docker build -t cucumber:1.0 .

cucumber is the name of the image and 1.0 is the tag version. The comma at the end of the command represents the location of your Dockerfile which was in the main directory in my case.

After the successful execution of this command, your image will be ready. You can see it with calling the docker images command.

Run container & scenarios

Below command creates a container which is built from the image we just created above and starts tests within.

docker run -v C:/workspace/CucumberTests:/cucumber cucumber:1.0 cucumber features/*.feature

The -v parameter represents a very nice feature of Docker. It enables you to share a volume between your container and the host you build your container. Why did I need this? Because failing scenarios creates screenshots and HTML reports and I don’t want to lose them since container will be dead after my features complete test run. After ‘-v’ follows the cucumber command to run features.

Let me give you an important detail here. My example does not cover the case of opening the browser in the container. Where to open browser depends on your env.rb configuration. You can open the browser in the container or in a remote machine such as selenium grid which is what I did here. If you wish not to use selenium grid but open the browser in the container then you must add necessary steps to your Dockerfile to install a browser in the image.

Happy dockerizing 🐳

--

--