3

docker: command not found在 gitlab-ci 中运行以下 CI 脚本时出现错误。此错误发生在before_script部署阶段。

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

我有点困惑,因为我将 docker-in-docker ( docker:dind) 作为服务运行,所以 docker 命令应该可用于所有阶段(如果我理解正确的话),但显然不是。

是因为与 google/cloud-sdk 的交互吗?

4

3 回答 3

5

你可能误解了什么services意思。从文档中,

services 关键字仅定义了在您的作业期间运行的另一个 docker 映像,并链接到 image 关键字定义的 docker 映像。

您需要的是一个使用 dind 映像并预装 gcloud sdk 的自定义 docker 执行器。您可以使用以下方法构建这样的图像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
于 2017-04-23T18:07:06.273 回答
0

这个问题是在大约 5 年前提出的,我不确定到那时该图像是否在没有 docker 二进制文件的情况下发布,除了标准位置不可用之外google/cloud-sdk,我想不出任何其他错误。docker: command not found无论如何,今天 2022google/cloud-sdk带有 docker,它可以与 docker 服务交互,由于我在尝试使用docker:dindand遇到问题后多次来到这里google/cloud-sdk,我将添加以下内容:

可以使用google/cloud-sdk镜像中的 docker,无需为您的 Gitlab CI 创建自定义镜像。问题是 docker ingoogle/cloud-sdk尝试连接到/var/run/docker.sock日志中显示的套接字:

$ 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?

无论如何,您还可以检查docker:dinddocker 在套接字(无法从作业容器访问)和 tcp 端口(可通过主机名访问)中侦听的服务日志docker。因此,您只需在 docker 命令中使用 tcp 端口,方法是设置 env 变量或DOCKER_HOST添加-H tcp://docker:2375

$ docker -H tcp://docker:2375 build -t gcr.io/$GCP_PROJECT_ID/test:$CI_COMMIT_SHORT_SHA .
Step 1/8 : FROM python:latest
于 2022-02-06T16:57:58.943 回答
-3

你忘了通知image顶部的标签。

image: docker:latest

services:
- docker:dind
...

为我工作!:)

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

于 2017-04-28T02:58:28.380 回答