Running your php/js tests with docker (for GitLab CI and others)

Grégoire Willmann
May 2, 2018 · 3 min read

GitLab has a wonderful CI (Continuous Integration) system. Not to use it when pushing your code onto their repos would be a shame ;)

Let me guide you in the marvellous land of automated unit and functional tests.

In this story, we will be using a very common symfony setup:

  • Backend: symfony 3.4
  • Assets (js and css) are compiled with webpack and webpack encore (see symfony flex)
  • Yarn as a javascript package manager
  • phpunit for php tests
  • mocha, puppeteer and chrome for headless javascript tests

Here is what we are going to cover:

  1. Dockerfile file, used to create your custom docker image where your tests are going to be running
  2. .gitlab-ci.yml file, used to run your tests on Gitlab

1. Dockerfile

Docker is a program which helps you create sort of virtual machines thanks to a file named Dockerfile.

Isn’t it just great? Thanks to this file and two simple commands, you are going to be able to run your tests effortlessly on any host capable of running a docker instance.

The philosophy here is to take an existing docker image and extend it (or customise it) with our dependencies so that we can run our tests.

Take a look at my Dockerfile (view it on GitHub) and edit it to your needs:

Extract of my Dockerfile

See how we start from a regular php:7.1 image and work our way to a customised one?

Once edited, run the following command to build your image (you might need to create a account on Docker Hub first to get your account name):

docker build -t [YOUR ACCOUNT NAME]/[YOUR IMAGE NAME]:latest .

Once built, push your image to your Docker Hub account with:


This will make your custom image available for your tests in GitLab!

Great! Now that we have our custom docker image built and pushed to Docker Hub, let’s take a look at our .gitlab-ci.yml file.

2. .gitlab-ci.yml

For detailed information on this file, please refer to the official documentation.

Basically when this file is present in your code, GitLab detects it and automatically runs your tests!

Here it what mine looks like (view it on GitHub):

Extract of my .gitlab-ci.yml file

The main things to note here:

  • line 1: we use our previously created custom image
  • line 32: we tell GitLab to start our phpunit tests
  • line 36: we tell GitLab to start our javascript tests in parallel of our phpunit tests
  • line 38–40: we start a fake screen in order to be able to execute javascript

Wrapping up

Things can be greatly improved according to me, for example:

  • For our custom image, we extend a php:7.1 image which results in a rather large docker image (~1.1Gb). Starting from an alpine docker image would result in a smaller image but I haven’t managed to install all the dependencies to make chromium work for the javascript tests…
  • During tests, the `composer install` task takes a long time to execute. Maybe one could find a way to speed this up?

Thanks for reading,

Comments and improvements to my code are more than welcomed!

Grégoire Willmann

Written by

Head of infrastructure at Kinoba | Ruby On Rails expert 👾 | Developing innovative web platforms for our tribe (

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade