由于“没有此类文件或目录”而导致gitlab CI失败

时间:2020-01-09 19:49:30

标签: docker gitlab dockerfile gitlab-ci raspbian

我正在尝试让我的.gitlab-ci.yml文件使用Gitlab容器注册表中的映像。我已成功将Dockerfile上传到注册表,并且可以从本地计算机上的注册表中提取图像并建立一个容器。但是,将图像用于我的.gitlab-ci.yml文件时,出现以下错误:

Authenticating with credentials from job payload (GitLab Registry)
standard_init_linux.go:190: exec user process caused "no such file or directory"

我已经看到了很多有关Windows EOL字符的讨论,但是我正在Raspbian上运行,我不认为这是问题所在。但是,我对此很陌生,无法弄清楚问题出在哪里。感谢您的帮助。

.gitlab-ci.yml文件:

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

stages:
    - test-version

test:
    stage: test-version
    image: registry.gitlab.com/my/project/test:latest
    script:
        - python --version 

test.Dockerfile(在注册表中为registry.gitlab.com/my/project/test:latest

ARG base_img="python:3.6"                                                                                                                                                                                                                    
FROM ${base_img}

# Install Python packages
RUN pip install --upgrade pip

编辑: 还要注意的另一件事是,如果我将.gitlab-ci.yml文件中的图像更改为仅python:3.6,则它可以正常运行。只有当我尝试在注册表中链接我的图像时。

1 个答案:

答案 0 :(得分:1)

正如您在评论中所确认的那样,gitlab.com/my/project是一个私有存储库,因此不能直接将docker pullimage:属性与registry.gitlab.com/my/project/test:latest一起使用。

但是,您应该能够使用.gitlab-ci.yml并手动运行image: docker:latest命令(包括docker)来适应docker login

这依赖于所谓的Docker-in-Docker (dind) approach,它是supported by GitLab CI

这里是.gitlab-ci.yml的通用模板,它依赖于此思想:

stages:
  - test-version

test:
  stage: test-version
  image: docker:latest
  services:
    - docker:dind
  variables:
    # GIT_STRATEGY: none  # uncomment if "git clone" is unneeded
    IMAGE: "registry.gitlab.com/my/project/test:latest"
  before_script:
    # - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" "$CI_REGISTRY"
    # or better
    - echo "$CI_REGISTRY_PASSWORD" | docker login -u "$CI_REGISTRY_USER" --password-stdin "$CI_REGISTRY"

  script:
    - docker pull "$IMAGE"
    - |
      docker run --rm -v "$PWD:/build" -w /build "$IMAGE" /bin/bash -c "
        export PS4='+ \e[33;1m(\$0 @ line \$LINENO) \$\e[0m '  # optional
        set -ex  # mandatory
        ## TODO insert your multi-line shell script here ##
        echo \"One comment\"  # quotes must be escaped here
        : A better comment
        python --version
        echo $PWD  # interpolated outside the container
        echo \$PWD  # interpolated inside the container
        ## (cont'd) ##
      " "$CI_JOB_NAME"
    - echo done

这会导致更多样板,但这是通用的,因此您只需替换IMAGE定义并用您自己的Bash脚本替换TODO区域,只需确保满足两个条件即可:

  • 如果您的shell代码中包含一些双引号,则需要转义它们,因为整个代码都被docker run … ""包围(最后一个变量"$CI_JOB_NAME"是一个细节,是可选的,只允许覆盖Bash变量$0中引用的PS4变量
  • 如果您的外壳程序代码包含局部变量,则需要对它们进行转义(请参见上面的\ $ PWD),否则这些变量将在运行docker run … "$IMAGE" /bin/sh -c "…"命令本身之前被解析。