简体   繁体   中英

docker not found with docker:dind + google/cloud-sdk

I'm getting the error docker: command not found<\/code> while running the following CI script inside gitlab-ci. This error is happening during before_script<\/code> for the deploy phase.

services:
  - docker:dind

stages:
  - build
  - test
  - deploy

before_script:
  - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY

build:
  stage: build
  image: docker:latest
  script:
    - docker info
    - docker version
    - docker build --pull -t $SERVICE_NAME:$CI_COMMIT_REF_NAME .
    - docker image list
    - docker tag $SERVICE_NAME:$CI_COMMIT_REF_NAME $CI_REGISTRY_IMAGE/$SERVICE_NAME:$CI_COMMIT_REF_NAME
    - docker push $CI_REGISTRY_IMAGE/$SERVICE_NAME:$CI_COMMIT_REF_NAME

test:
  image: docker:latest
  stage: test
  script:
    - docker pull $CI_REGISTRY_IMAGE/$SERVICE_NAME:$CI_COMMIT_REF_NAME
    - docker image list
    - docker run $CI_REGISTRY_IMAGE/$SERVICE_NAME:$CI_COMMIT_REF_NAME npm test

deploy:
  image: google/cloud-sdk
  stage: deploy
  environment: Production
  script:
    - echo $DEPLOY_KEY_FILE_PRODUCTION > /tmp/GCLOUD_KEYFILE.json
    - gcloud auth activate-service-account --key-file /tmp/GCLOUD_KEYFILE.json
    - rm /tmp/GCLOUD_KEYFILE.json
    - gcloud info
    - gcloud components list
  only:
    - master

You probably misunderstood what services mean. From the doc ,

The services keyword defines just another docker image that is run during your job and is linked to the docker image that the image keyword defines.

What you need is a custom docker executor that uses dind image and preinstalled with gcloud sdk. You can build such an image with this Dockerfile :

FROM docker:latest

RUN apk add --no-cache \
 bash \
 build-base \
 curl \
 git \
 libffi-dev \
 openssh \
 openssl-dev \
 python \
 py-pip \
 python-dev

RUN pip install docker-compose fabric
RUN curl https://sdk.cloud.google.com | bash -s -- --disable-prompts

You forgot to inform the image tag at the top.

image: docker:latest

services:
- docker:dind
...

Works for me! :)

See: https://docs.gitlab.com/ce/ci/docker/using_docker_build.html

I think the only problem in OPs yaml is the before_script as mentioned by jolooket. But, as I ended up here several times after running into problems trying to use docker:dind and google/cloud-sdk , I will add the following:

Is possible to use docker from the google/cloud-sdk image. The problem is that docker in google/cloud-sdk tries to connect to the socket in /var/run/docker.sock as is presented in the logs:

$ docker build -t gcr.io/$GCP_PROJECT_ID/test:$CI_COMMIT_SHORT_SHA .
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

Anyways you can also checks in your logs of the service docker:dind that docker listens in a socket (not reachable from the job container) and a tcp port (reachable via the hostname docker ). So, you just need to use the tcp port in your docker commands, either by setting the env variable DOCKER_HOST or adding a -H tcp://docker:2375 as in

$ docker -H tcp://docker:2375 build -t gcr.io/$GCP_PROJECT_ID/test:$CI_COMMIT_SHORT_SHA .
Step 1/8 : FROM python:latest

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM