📖 Scenario: You are working on a project where you want to automate building a Docker image using GitLab CI. This will help you save time and avoid manual steps every time you update your code.Imagine you have a simple app and you want GitLab to build its Docker image automatically whenever you push changes.
🎯 Goal: Build a GitLab CI pipeline that uses Docker to build an image from a Dockerfile. You will create the pipeline configuration step-by-step, adding the Docker build command and finally printing the image build result.
📋 What You'll Learn
Create a basic GitLab CI YAML file named .gitlab-ci.yml
Add a job called build_image that uses the docker:latest image
Configure the job to build a Docker image named myapp:latest from the Dockerfile
Print the Docker image details after building
💡 Why This Matters
🌍 Real World
Automating Docker image builds in GitLab CI saves time and reduces errors by removing manual steps.
💼 Career
DevOps engineers and developers use GitLab CI with Docker to create reliable, repeatable build pipelines.
Progress0 / 4 steps
1
Create the GitLab CI YAML file with a build job
Create a file named .gitlab-ci.yml and add a job called build_image that uses the docker:latest image.
Docker
Hint
Start with the job name, then specify the image under it with proper indentation.
2
Add the script to build the Docker image
In the build_image job, add a script section with the command docker build -t myapp:latest . to build the Docker image.
Docker
Hint
Use a list under script: with a dash and the docker build command.
3
Add Docker service for Docker-in-Docker support
Add a services section to the build_image job with docker:dind to enable Docker-in-Docker during the build.
Docker
Hint
Services are listed like scripts, with a dash and the service name.
4
Print the Docker image details after building
Add a command in the script section to print the details of the built image using docker images myapp:latest.
Docker
Hint
Add the print command as another item in the script list.
Practice
(1/5)
1. What is the main purpose of using docker:dind service in a GitLab CI pipeline?
easy
A. To send notifications after the pipeline finishes
B. To deploy the application to production automatically
C. To run Docker commands inside the CI job container
D. To store build artifacts permanently
Solution
Step 1: Understand the role of docker:dind
The docker: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, using docker:dind lets 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 C
Quick Check:
docker:dind = run Docker commands in CI [OK]
Hint: Remember: dind means Docker runs inside Docker [OK]
Common Mistakes:
Confusing dind with deployment step
Thinking dind stores artifacts
Assuming dind sends notifications
2. Which of the following is the correct way to specify the Docker service in a GitLab CI job to enable Docker commands?
easy
A. services:
- docker:dind
B. services:
- docker:latest
C. services:
- docker:build
D. services:
- docker:run
Solution
Step 1: Recall the correct service name for Docker-in-Docker
The official Docker-in-Docker service image is docker: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 name docker:dind. Other options are invalid or do not exist as official images.
Final Answer:
services:
- docker:dind -> Option A
Quick Check:
Correct service name = docker:dind [OK]
Hint: Use exact service name 'docker:dind' for Docker commands [OK]
Common Mistakes:
Using 'docker:latest' instead of 'docker:dind'
Adding extra tags like 'build' or 'run'
Omitting the services section
3. Given this GitLab CI job snippet, what will be the output of the docker ps command inside the job?
B. Lists running containers inside the dind service (likely empty)
C. Lists containers on the host machine
D. Outputs the Docker version only
Solution
Step 1: Understand the environment of the job
The job uses docker:dind service, which runs a Docker daemon inside the CI environment. The docker ps command connects to this daemon.
Step 2: Predict the output of docker ps
Since the job just started and no containers were run inside the dind daemon, docker ps will list running containers inside the dind environment, which is likely empty.
Final Answer:
Lists running containers inside the dind service (likely empty) -> Option B
The job fails with error: Cannot connect to the Docker daemon. What is the most likely fix?
medium
A. Add variables:
DOCKER_HOST: tcp://docker:2375 to the job
B. Change image to docker:dind
C. Remove the services section
D. Add docker login before build
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 the docker:dind service container.
Step 2: Fix connection by setting DOCKER_HOST variable
Setting DOCKER_HOST: tcp://docker:2375 tells 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 A
Quick Check:
Set DOCKER_HOST to connect client to dind daemon [OK]
Hint: Set DOCKER_HOST to tcp://docker:2375 to fix connection [OK]
Common Mistakes:
Changing image to docker:dind instead of using service
Removing services section disables Docker daemon
Adding docker login does not fix daemon connection
5. You want to build a Docker image and push it to GitLab Container Registry in a GitLab CI job. Which snippet correctly sets up the job with Docker-in-Docker and pushes the image?