GitLab CI with Docker - Time & Space Complexity
We want to understand how the time to run a GitLab CI pipeline using Docker changes as we add more jobs or steps.
How does the total execution time grow when the pipeline gets bigger?
Analyze the time complexity of the following GitLab CI snippet using Docker.
image: docker:latest
services:
- docker:dind
stages:
- build
- test
build_job:
stage: build
script:
- docker build -t myapp .
test_job:
stage: test
script:
- docker run myapp pytest
This pipeline builds a Docker image and then runs tests inside a container from that image.
Look for repeated steps or loops in the pipeline.
- Primary operation: Each job runs its script commands sequentially.
- How many times: The pipeline runs each job once per pipeline execution; no loops inside the snippet.
As we add more jobs or steps, the total time grows roughly by adding each job's time.
| Input Size (number of jobs) | Approx. Operations (job runs) |
|---|---|
| 2 | 2 jobs run sequentially |
| 10 | 10 jobs run sequentially |
| 100 | 100 jobs run sequentially |
Pattern observation: The total time grows linearly as more jobs are added.
Time Complexity: O(n)
This means the total pipeline time grows in direct proportion to the number of jobs.
[X] Wrong: "Adding more jobs will not increase total time because they run in parallel."
[OK] Correct: In this example, jobs run in stages sequentially, so total time adds up, not stays the same.
Understanding how pipeline steps add up helps you design efficient CI/CD workflows and explain your choices clearly.
What if we changed the pipeline to run all jobs in parallel? How would the time complexity change?
Practice
docker:dind service in a GitLab CI pipeline?Solution
Step 1: Understand the role of
Thedocker:dinddocker:dind(Docker-in-Docker) service allows Docker commands to run inside a container, which is essential for building and managing Docker images during CI jobs.Step 2: Identify the purpose in GitLab CI context
In GitLab CI, usingdocker:dindlets the pipeline build, run, and push Docker images isolated from the host system.Final Answer:
To run Docker commands inside the CI job container -> Option CQuick Check:
docker:dind = run Docker commands in CI [OK]
- Confusing dind with deployment step
- Thinking dind stores artifacts
- Assuming dind sends notifications
Solution
Step 1: Recall the correct service name for Docker-in-Docker
The official Docker-in-Docker service image isdocker:dind, which must be specified exactly to enable Docker commands in GitLab CI.Step 2: Compare options to find exact match
Only services: - docker:dind uses the correct service namedocker:dind. Other options are invalid or do not exist as official images.Final Answer:
services: - docker:dind -> Option AQuick Check:
Correct service name = docker:dind [OK]
- Using 'docker:latest' instead of 'docker:dind'
- Adding extra tags like 'build' or 'run'
- Omitting the services section
docker ps command inside the job?
job:
image: docker:latest
services:
- docker:dind
script:
- docker info
- docker ps
Solution
Step 1: Understand the environment of the job
The job usesdocker:dindservice, which runs a Docker daemon inside the CI environment. Thedocker pscommand connects to this daemon.Step 2: Predict the output of
Since the job just started and no containers were run inside the dind daemon,docker psdocker pswill list running containers inside the dind environment, which is likely empty.Final Answer:
Lists running containers inside the dind service (likely empty) -> Option BQuick Check:
docker ps inside dind = running containers (empty if none) [OK]
- Expecting error about Docker daemon
- Thinking it lists host machine containers
- Confusing docker info output with docker ps
build:
image: docker:latest
script:
- docker build -t myapp .
services:
- docker:dind
The job fails with error: Cannot connect to the Docker daemon. What is the most likely fix?Solution
Step 1: Identify cause of Docker daemon connection error
The error means the Docker client inside the job cannot reach the Docker daemon running in thedocker:dindservice container.Step 2: Fix connection by setting DOCKER_HOST variable
SettingDOCKER_HOST: tcp://docker:2375tells the Docker client to connect to the dind service over TCP, enabling communication.Final Answer:
Add variables: DOCKER_HOST: tcp://docker:2375 to the job -> Option AQuick Check:
Set DOCKER_HOST to connect client to dind daemon [OK]
- Changing image to docker:dind instead of using service
- Removing services section disables Docker daemon
- Adding docker login does not fix daemon connection
build_and_push:
image: docker:latest
services:
- docker:dind
variables:
DOCKER_HOST: tcp://docker:2375
DOCKER_TLS_CERTDIR: ""
script:
- docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
- docker build -t $CI_REGISTRY_IMAGE:latest .
- docker push $CI_REGISTRY_IMAGE:latest
Solution
Step 1: Check Docker-in-Docker setup
The job usesdocker:dindas a service and setsDOCKER_HOSTand disables TLS cert dir, which is required for Docker commands to connect properly.Step 2: Verify login and push steps
The job logs into the GitLab Container Registry using CI variables, then builds and pushes the image with correct tags.Final Answer:
Correct setup with variables and login before build and push -> Option DQuick Check:
Proper dind setup + login + build + push = Correct setup with variables and login before build and push [OK]
- Forgetting DOCKER_HOST causes connection errors
- Using docker:dind as image instead of service
- Skipping docker login before push
Ready to prove it?
Pass the test and this pattern moves unknown
