DevOps / GitLab CI Basics Interview Questions
What are 'services' in GitLab CI/CD and how do you use them?
Services are Docker containers that run alongside the main job container, providing supporting infrastructure like databases, message brokers, or a Docker daemon. They share a network namespace with the job container.
# Run tests with a PostgreSQL database service: test-with-db: stage: test image: python:3.12 services: - postgres:16-alpine # starts a Postgres container variables: POSTGRES_DB: testdb POSTGRES_USER: postgres POSTGRES_PASSWORD: secret DATABASE_URL: "postgresql://postgres:secret@postgres/testdb" script: - pip install -r requirements.txt - python -m pytest # Multiple services: integration-test: image: node:20 services: - redis:7-alpine - postgres:16-alpine - name: rabbitmq:3-management alias: rabbit # access as "rabbit" hostname script: - npm test # Service with alias and entrypoint override: custom-service: services: - name: my-registry/my-service:latest alias: api-mock entrypoint: ["/usr/bin/api-mock"] command: ["--port", "8080"]
Services are accessible from the main job container using the service's image name (or alias) as the hostname. For example, a postgres:16 service is reached at hostname postgres. Set an alias when you need a custom hostname.
More Related questions...