使用 gitlab 时 docker Push 由于 "unauthorized: authentication required"失败

标签 docker gitlab docker-compose gitlab-ci-runner

尝试使用 gitlab-runner 推送到 Gitlab 注册表时遇到以下错误:

unauthorized: authentication required 
ERROR: Build failed: exit status 1

尽管:

$ docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN git.COMPANY.com
Login Succeeded

从本地环境进行构建和推送效果很好,这表明问题与运行程序运行的主机(gitlab-ci3)有关,或者可能与正在使用的用户有关:

$ echo $USER
gitlab-runner

在组中:

docker:x:999:gitlab-runner
gitlab-runner:x:998:

我已经尝试过 docker unauthorized: authentication required - upon push with successful login 但没有成功。 也许是因为 gitlab-runner 没有 root config.json 的权限?:

$ cat /root/.docker/config.json
 cat: /root/.docker/config.json: Permission denied

除了解决这个问题之外,如果您能为我提供如何更好地调试此错误以供将来使用,这将非常有帮助。

我使用的是 GitLab 企业版 8.13.1-ee、Docker 1.12.3、gitlab-ci-multi-runner 1.7.1

Gitlab 输出:

Running with gitlab-ci-multi-runner 1.7.1 (f896af7)
Using Shell executor...
Running on gitlab-ci3...
Fetching changes...
HEAD is now at cfe0a35 gitlab-ci.yml - testing
Checking out cfe0a356 as master...
$ docker info
Containers: 0
 Running: 0
 Paused: 0
 Stopped: 0
Images: 39
Server Version: 1.12.3
Storage Driver: aufs
 Root Dir: /var/lib/docker/aufs
 Backing Filesystem: extfs
 Dirs: 30
 Dirperm1 Supported: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: host null overlay bridge
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Security Options: apparmor
Kernel Version: 3.13.0-96-generic
Operating System: Ubuntu 14.04.5 LTS
OSType: linux
Architecture: x86_64
CPUs: 4
Total Memory: 7.612 GiB
Name: gitlab-ci3
ID: 6QWV:RQFD:4RWJ:D4CF:QN2M:MHKK:TABD:JD3F:3W7R:MCNA:4NHO:26VA
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
WARNING: No swap limit support
Insecure Registries:
 127.0.0.0/8
$ docker-compose rm --all --force
The TESTS variable is not set. Defaulting to a blank string.
--all flag is obsolete. This is now the default behavior of `docker-compose rm`
No stopped containers
$ docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN git.COMPANY.com
Login Succeeded
$ docker build --pull -t $CONTAINER_IMAGE .
Sending build context to Docker daemon 557.1 kB
.
.
.
Sending build context to Docker daemon 18.05 MB


Step 1 : FROM ruby:latest
latest: Pulling from library/ruby
Digest: sha256:21a22bcc774f25c99bf5f27f16f8b4666f4ccbcb457e06f7a7e06a4728ea640b
Status: Image is up to date for ruby:latest
 ---> 45766fabe805
Step 2 : RUN apt-get update && curl -sL https://deb.nodesource.com/setup_5.x | bash - && apt-get install -y git nodejs && rm -rf /var/lib/apt/lists/*
 ---> Using cache
.
.
.
Step 23 : 
 ---> Using cache
 ---> b36460381a03
Successfully built b36460381a03
$ docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN git.COMPANY.com
Login Succeeded
$ echo $USER
gitlab-runner
$ cat ~/.docker/config.json
{
  "auths": {
    "git.COMPANY.com": {
      "auth": "EDW..."
    }
  }

 $ cat /root/.docker/config.json
 cat: /root/.docker/config.json: Permission denied

$ docker push $CONTAINER_IMAGE
The push refers to a repository [git.COMPANY.com:4567/ui/PROJECT]
fc4a22a92ee5: Preparing
.
.
.
149636c85012: Waiting
f96222d75c55: Waiting
unauthorized: authentication required
ERROR: Build failed: exit status 1

.gitlab-ci.yml:

before_script:
  - docker info
stages:
  - build
  - test
variables:
  CONTAINER_IMAGE: git.COMPANY.com:4567/ui/PROJECT:$CI_BUILD_REF_NAME
build:
  tags:
   - spec_work
  stage: build
  script:
      - docker-compose rm --all --force
      - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN git.COMPANY.com
      - docker build --pull -t $CONTAINER_IMAGE .
      - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN git.COMPANY.com
      - echo $USER
      - cat ~/.docker/config.json
      - docker push $CONTAINER_IMAGE
test:
  stage: test
  tags:
    - spec_work
  script:
    - docker-compose rm --all --force
    - docker-compose down
    - docker-compose build
    - docker-compose up --abort-on-container-exit --force-recreate
    - (exit `docker-compose ps | grep 'test' | grep -Po 'Exit (\d+)' | cut -d " " -f 2`)

Docker 配置.json:

root@gitlab-ci3 (Ubuntu 14.04) ➜  ~ cat .docker/config.json
{
  "auths": {
    "https://git.COMPANY.com": {
      "auth": "EDW..."
    }
  }
}

还尝试过:

{
  "auths": {
    "https://git.COMPANY.com/ui/PROJECT": {
      "auth": "EDW..."
    }
  }
}

最佳答案

来自Docker-in-Docker with Gitlab Shared runner for building and pushing docker images to registry

基于:

- docker version
- docker build -t $CI_REGISTRY_IMAGE:latest .
- docker tag $CI_REGISTRY_IMAGE:latest $CI_REGISTRY_IMAGE:$CI_BUILD_TAG
- docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY
- docker push $CI_REGISTRY_IMAGE:$CI_BUILD_TAG

我用过:

- docker version
- docker build -t $CI_REGISTRY_IMAGE:latest .
- docker tag $CI_REGISTRY_IMAGE:latest $CI_REGISTRY_IMAGE:$CI_BUILD_REF_NAME
- docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY
- docker push $CI_REGISTRY_IMAGE:$CI_BUILD_REF_NAME

在 .gitlab-ci.ym 的构建部分。

现在推送工作正常了。

至于为什么它有效,我不知道。 如果有人知道为什么此更改解决了此问题,请发表评论,我确实想知道原因。

谢谢!

关于使用 gitlab 时 docker Push 由于 "unauthorized: authentication required"失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40338565/

相关文章:

mysql - 使用 Docker 部署 laravel 项目时出现 mysql 错误

git - 使用 GitLab CI 在一个存储库中运行多个 Maven 项目

azure - 将.net Core应用程序发布到Azure Docker(容器注册表)

gitlab 上的 Sonarqube 质量徽章

git - 使用 SSH 的 Aptana 和 GitLab 版本控制

python - docker项目中mysql配置的不同主机值产生不同的错误

bash - 如何强制 Git for Windows 的 bash-shell 不将路径字符串转换为 Windows 路径?

java - Docker、Dockerfile 和在我开始下一个服务之前使用等待

docker - 在 Bitnami Docker 容器中设置用户 UID

.net - 无法访问我的 .NET 容器