dockergoogle-cloud-platformgitlab-ci-runnerdocker-machinegoogle-container-registry

How do I use docker commands in docker container?


I am using .gitlab-ci.yml file as below. I want to perform docker pull & push to google container registry

image:
  name: google/cloud-sdk:latest
  entrypoint: [ "" ]

variables:
  UNLEASH_IMAGE_NAME: "unleash"

stages:
  - print

print:
  stage: print
  script:
    - gcloud config set auth/impersonate_service_account $GCP_BUILD_SA
    - gcloud config set project $GCP_PROJECT_BUILD_ID
    - gcloud auth configure-docker
    - docker pull unleashorg/unleash-proxy:latest
    - docker tag unleashorg/unleash-proxy:latest gcr.io/$GCP_PROJECT_BUILD_ID/UNLEASH_IMAGE_NAME:$CI_COMMIT_SHORT_SHA
    - docker images

I am getting below error in my pipeline when I run docker images just to test whether its working: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

  1. I have tried entering the container & running the docker images command but same error.
  2. I tried command service docker start & systemctl but these tools/commands are not there in container.

How do I resolve this issue. I just want to docker pull & push in this image.


Solution

  • To publish a Docker image to Container Registry with Gitlab and use Docker in Docker, it's better using Kaniko, example for publish :

    build:
      stage: build
      image:
        name: gcr.io/kaniko-project/executor:v1.9.0-debug
        entrypoint: [""]
      script:
        - /kaniko/executor
          --context "${CI_PROJECT_DIR}"
          --dockerfile "${CI_PROJECT_DIR}/Dockerfile"
          --destination "${CI_REGISTRY_IMAGE}:${CI_COMMIT_TAG}"
      rules:
        - if: $CI_COMMIT_TAG
    

    If you want to pull, then publish with a need to have gcloud cli, you can create your own Docker image from kaniko and install gcloud sdk inside. Then you can publish this custom image to Gitlab repo, GCP Container registry or GCP Artifact registry, then use it from your Gitlab script :

    build:
      stage: build
      image:
        name: gcr.io/custom-kaniko/v1.0.0
        entrypoint: [""]
      script:
        - # tip your gcloud commands
        - # Adapt the Kaniko to fit to your need
        - /kaniko/executor
          --context "${CI_PROJECT_DIR}"
          --dockerfile "${CI_PROJECT_DIR}/Dockerfile"
          --destination "${CI_REGISTRY_IMAGE}:${CI_COMMIT_TAG}"
      rules:
        - if: $CI_COMMIT_TAG