Bird
0
0

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?

hard📝 Workflow Q15 of 15
Docker - in CI/CD
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?
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
ANo docker login command before push
BMissing DOCKER_HOST variable, so Docker commands fail
CUses docker:dind as image instead of service
DCorrect setup with variables and login before build and push
Step-by-Step Solution
Solution:
  1. Step 1: Check Docker-in-Docker setup

    The job uses docker:dind as a service and sets DOCKER_HOST and disables TLS cert dir, which is required for Docker commands to connect properly.
  2. 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.
  3. Final Answer:

    Correct setup with variables and login before build and push -> Option D
  4. Quick Check:

    Proper dind setup + login + build + push = Correct setup with variables and login before build and push [OK]
Quick Trick: Always login before push and set DOCKER_HOST for dind [OK]
Common Mistakes:
  • Forgetting DOCKER_HOST causes connection errors
  • Using docker:dind as image instead of service
  • Skipping docker login before push

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Docker Quizzes