DevOps / GitLab CI Basics Interview Questions
How do you use the 'image' keyword to specify a Docker environment for a job?
The image keyword specifies the Docker image that the runner uses as the execution environment for the job. This ensures each job runs in a consistent, isolated environment with all required tools pre-installed.
# Set a default image for all jobs default: image: ubuntu:24.04 # Override image for a specific job build-node: stage: build image: node:20-alpine # uses Node.js 20 script: - node --version - npm ci - npm run build build-python: stage: build image: python:3.12-slim # different image for Python job script: - pip install -r requirements.txt - python -m pytest # Image with a registry prefix deploy-job: image: registry.gitlab.com/myorg/myimage:latest script: - ./deploy.sh # Image with a specific SHA (for reproducibility) test-job: image: name: node:20-alpine entrypoint: [""] # override the entrypoint if needed script: - npm test
Without a Docker executor: the image keyword is only used by runners with a Docker, Kubernetes, or similar container-based executor. Runners using the Shell executor ignore the image keyword and run jobs directly on the host machine.
Default image: if you don't specify an image, GitLab.com runners use a default image (often a Ruby image). Always specify an explicit image for consistent, predictable environments.
More Related questions...