unauthorized: authentication required on docker push to a different repo
I'm creating two docker images via gitlab-ci from one repository upon pushing them to GitLabs private container registry. The .gitlab-ci.yml
is below.
myproject
is the group name. backend
and docs
are GitLab projects within this group. Both have the container registry enabled.
build_image
works OK but build_apidoc_image
fails when pushing the image for the apidoc container to the registry with:
docker push myregistry.com/myproject/docs:latest
The push refers to a repository [myregistry.com/myproject/docs]
630816f32edb: Preparing
530d5553aec8: Preparing
c3f640b7175a: Preparing
378f94990a4e: Preparing
77b53f4affd3: Preparing
4b0bab9ff599: Preparing
d1c800db26c7: Preparing
42755cf4ee95: Preparing
4b0bab9ff599: Waiting
d1c800db26c7: Waiting
42755cf4ee95: Waiting
unauthorized: authentication required
ERROR: Build failed: exit code 1
The strange thing is that docker login
succeeds:
$ docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN myregistry.com
Login Succeeded
$ docker build -t myregistry.com/myproject/docs -f apidoc/Dockerfile .
Sending build context to Docker daemon 485.4 kB
This gitlab build is triggered from the backend
repository.
When building and pushing manually to docs
it works.
Here's the .gitlab-ci.yml
stages:
- build
build_image:
stage: build
image: docker:git
services:
- docker:dind
script:
- docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN myregistry.com
- docker build -t myregistry.com/myproject/backend .
- docker push myregistry.com/myproject/backend:latest
only:
- master
build_apidoc_image:
stage: build
image: docker:git
services:
- docker:dind
script:
- docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN myregistry.com
- docker build -t myregistry.com/myproject/docs -f apidoc/Dockerfile .
- docker push myregistry.com/myproject/docs:latest