Course contentsShow
← Cloud and Infrastructure
Lesson 1744 of 2,98936. GitLab CI/CDFree lesson

Image and Services Configuration

Specifying Docker images for jobs and configuring service containers for testing.

Image and Services Configuration

What you'll learn: How to specify which Docker images your GitLab CI jobs run in and how to attach service containers for testing dependencies like databases.


The Core Concept

Every GitLab CI job needs an execution environment. The image directive lets you specify which Docker container your job runs inside—think of it as choosing the operating system and pre-installed tools for that job. The services directive adds supplementary containers (like databases or cache servers) that run alongside your main job, accessible via network.

Why This Matters

Different jobs need different tools. Your build job might need Node.js, while your integration tests need both Node.js and a PostgreSQL database. Rather than installing everything manually in scripts, you declare ready-made Docker images.

Specifying an Image

test_job:
  image: node:18
  script:
    - npm test

Here, node:18 tells GitLab to run this job inside an official Node.js 18 container.

Adding Services for Testing

integration_test:
  image: python:3.9
  services:
    - postgres:14
  script:
    - python run_tests.py

The postgres:14 service starts a PostgreSQL container. Your test script can connect to it at hostname postgres (GitLab handles networking automatically).

Job-Level vs Global

You can define image globally for all jobs or override it per-job:

image: alpine:latest

job_a:
  script:
    - echo "Uses alpine"

job_b:
  image: ubuntu:22.04
  script:
    - echo "Uses ubuntu instead"

Key Takeaway: Use image to specify the Docker container environment for jobs, and services to add helper containers like databases—enabling isolated, reproducible test environments without manual setup.