Gitlab CI:找不到docker

时间:2019-01-16 09:23:30

标签: docker continuous-integration gitlab gitlab-ci gitlab-ci-runner

我正在尝试使用gitlab ci功能来构建我的项目。根据{{​​3}}问题,我已正确完成了所有工作。这是我的.gitlab-ci.yml。

.gitlab-ci.yml

image: docker:latest

services:
  - docker:dind

stages:
  - build
  - release
  - deploy

variables:
  DOCKER_DRIVER: overlay
  MAVEN_CLI_OPTS: "-s .m2/settings.xml --batch-mode"

cache:
  paths:
    - .m2/repository/
    - auth/target/

before_script:
  - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY

build:
  image: maven:latest
  stage: build
  script:
      - mvn $MAVEN_CLI_OPTS package

auth-release:
  stage: release
  when: on_success
  script:
    - docker build -f auth/Dockerfile -t "$CI_REGISTRY/$CI_PROJECT_PATH/auth" ./auth
    - docker push "$CI_REGISTRY/$CI_PROJECT_PATH/auth"

但是控制台会打印出错误:

控制台输出

Running with gitlab-runner 11.7.0-rc1 (6e20bd76)
  on docker-auto-scale ed2dce3a
Using Docker executor with image maven:latest ...
Starting service docker:dind ...
Pulling docker image docker:dind ...
Using docker image sha256:067814781fbba30c17a86aa6159516660b6c210485ac9ac346d5101d6e4b70f0 for docker:dind ...
Waiting for services to be up and running...
Pulling docker image maven:latest ...
Using docker image sha256:3bc97dc2e7ba13887f5a850968419ad0e83fc482acd1cf0d9606882b665e39f3 for maven:latest ...
Running on runner-ed2dce3a-project-10090201-concurrent-0 via runner-ed2dce3a-srm-1547629674-aa23c547...
Cloning repository...
Checking out 10c9202f as master...
Skipping Git submodules setup
Checking cache for default...
FATAL: file does not exist                         
Failed to extract cache
$ docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
/bin/bash: line 76: docker: command not found
ERROR: Job failed: exit code 1

我在做什么错了?

1 个答案:

答案 0 :(得分:1)

您正在混淆您的构建阶段。

根据您的日志,您在build阶段失败,该阶段是使用maven:latest图像执行的,因为它是在yaml中写入的。

但是您也有自己的before_script

before_script:
  - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY

因此,此脚本尝试在maven图像上执行并失败,因为maven图像没有内有docker。可能您只想在before_script阶段执行auth-release-在那里可以工作,因为auth-release使用docker:latest映像,而映像又包含docker可执行文件

您可能对docker:dind服务感到困惑,认为它会为您提供所有图像中的docker CLI-无法正常工作。 docker:dind服务为您提供了docker守护程序,但是您仍然需要在所有映像中安装docker CLI才能与服务进行交互。