A free CI (using Docker-Compose) for your Gitlab repositories?
Tags: docker, gitlab, testing, dockercompose
Published: 2018-10-03 23:18:07

---
title: A free CI (using Docker-Compose) for your Gitlab repositories?
published: true
description: 
tags: docker,gitlab,testing,dockercompose
cover_image: https://thepracticaldev.s3.amazonaws.com/i/8swxzr418o4ipkkqs9mn.jpg
canonical_url: https://www.einenlum.com/articles/free-ci-gitlab
---
*This article was originally posted on my [blog](https://www.einenlum.com/articles/free-ci-gitlab).*

If you're like me, you use Github for public repositories and for private repositories owned by organizations, but for your own private repositories, you use Gitlab. Indeed, Gitlab offers free unlimited private repositories and does the job.

After having pushed a lot of commits, you decide that it could be really nice to automate your tests every time a Merge Request (yes, Merge Request. We're talking Gitlab language here) is created.

Then you think that you could use CircleCI as you do for your Github repositories, [especially since you know how to use docker-compose on it](https://dev.to/einenlum/testing-your-app-with-docker-compose-on-circleci-1ll). You log into your CircleCI account, go to the integration page, and realize [Gitlab integration is missing](https://circleci.com/integrations/). Github is there, as well as Bitbucket, but no Gitlab in the area. Shit. [You can still upvote the idea of a Gitlab integration on CircleCI's website](https://circleci.com/ideas/?idea=CCI-I-248), but until they decide to change things, you're stuck.

You decide to list all the free CI services that can work with Gitlab and match your expectations. And then you realize that Gitlab offers a CI service. [After having read all the not clear at all advertised features on the website](https://about.gitlab.com/features/gitlab-ci-cd/) (**to Gitlab’s commercials: you have a nice product, we just don’t realize how cool is your offer since everything is so fuzzy on your website. It’s a shame**) you start to think that maybe they offer a free CI even for people using free private repositories on the Community Edition. It seems it is limited to 2000 build minutes per month, which is frankly a very generous deal. Our question today is more precise: is it possible to use docker-compose to build the whole stack and run your tests in the CI? Yup.

First, the (very bad…) documentation says you need to enable some shared runners, but in my case it was already done by default, so I guess you don’t need to do it either. Next.

Now we just need to create a `.gitlab-ci.yml` file in the root directory of our project. [This is quite documented for the general use](https://docs.gitlab.com/ce/ci/yaml/), but there is no track of docker-compose in the documentation. It seems we just need to install it as a python package ([thank you Stack Overflow!](https://stackoverflow.com/questions/39868369/run-docker-compose-build-in-gitlab-ci-yml/42697808#42697808)) and then we can use it. We also need to use some obscure image:docker and docker:dind services. Well, if it works, why not.

```yaml
image: docker

services:
  - docker:dind

before_script:
  - apk add --no-cache py-pip
  - pip install docker-compose
  - docker-compose build
  - docker-compose up -d
  # here we can install our dependencies (composer, yarn…)

tests:
  script:
    # launch our tests
    - docker-compose exec php phpspec run --format pretty
    - docker-compose exec php phpunit
    - docker-compose exec php behat
```

Quite easy actually. I just had some issue (that I don’t have locally or on CircleCI) with the interactive TTY while executing `docker-compose exec`. Maybe because the version used by pip is [the one adding a regression](https://github.com/docker/compose/issues/5696#issue-299103105)? I dunno. Anyway, you can fix the issue by adding a `-T` tag to your commands.

```yaml
image: docker

services:
  - docker:dind

before_script:
  - apk add --no-cache py-pip
  - pip install docker-compose
  - docker-compose build
  - docker-compose up -d
  - docker-compose exec -T php composer install

tests:
  script:
    - docker-compose exec -T php phpspec run --format pretty
    - docker-compose exec -T php phpunit
    - docker-compose exec -T php behat
```

This should do the trick. Hope it helped. Happy testing.

**_Update_**: Actually, maybe I spoke too fast…
**I have an issue with the network. Containers cannot communicate with each others… Maybe I can play with the network options (network_mode?).
If a docker expert is here, I would love some help :)**

*Image by [Paradasos](https://www.flickr.com/photos/paradasos/7234825030/) (CC by/nc)*