Skip to main content
In this guide, you’ll learn how to configure Docker-based services within a GitLab CI/CD pipeline. Services are Docker containers that provide network-accessible dependencies (e.g., databases, caches) so your jobs can execute tests against them.

Understanding GitLab CI Services

GitLab CI services let your job containers connect to supporting containers on the same network. Common use cases include databases (PostgreSQL, MongoDB), message queues (RabbitMQ), or custom APIs.

What Services Are—and What They Aren’t

You can use any Docker image—public or private—as a service. However, services are not a way to install CLI tools for your job container. For example:
To run language-specific commands, always specify the language image under image:, not under services:.
Use services for networked dependencies only. Put build tools and language runtimes in the image: section of your job.

Defining & Connecting to Services

A minimal service definition looks like this:
By default, each service is reachable via two hostnames:
  • Replace / with __ (double underscore): tutum__wordpress
  • Replace / with - (single dash): tutum-wordpress
Tags (the part after :) are dropped. Because underscores aren’t valid under RFC 1123, it’s best to assign a custom alias:

Service Configuration Options

The image shows a GitLab documentation page detailing available settings for services, including columns for setting names, requirements, GitLab version, and descriptions. The sidebar contains navigation links for various GitLab features.

Example: Entrypoint, Command & Variables

Setting Pull Policies

Control when the Runner pulls service images:

CI/CD Jobs: Unit Testing & Code Coverage with MongoDB Service

The example below shows two jobs—unit_testing and code_coverage—both using a MongoDB service from siddharth67/mongo-db:non-prod. We set an alias mongo and always pull the image on self-managed runners.

How It Works

  1. Service Startup
    The GitLab Runner pulls and starts the MongoDB container (siddharth67/mongo-db:non-prod), then waits for it to become healthy.
  2. Job Container Launch
    The Runner pulls the Node.js image and initializes the job container.
  3. Networking
    Both containers share a Docker network. The job container connects to MongoDB via mongo:27017.
  4. Caching & Artifacts
    Node modules and test reports are cached/restored and archived, speeding up subsequent pipelines.

Sample Job Logs

References

Watch Video