What is the correct way of changing container names in the gitlab registry

Viewed 214

i am trying to build multiple docker images and push them to my gitlab container registry.

This works just fine, when i build and push them like this:

stages:
 - build

docker-build-template:
  # Use the official docker image.
  image: docker:latest
  stage: build
  services:
    - docker:dind
  before_script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
  variables:
    OS_IDENTIFIER: "ubuntu-1604"
  script:
    - cp docker/base/ubuntu-1604.dockerfile Dockerfile
    - docker build --pull -t "${OS_IDENTIFIER}-base${tag}" .
    - echo $CI_REGISTRY_IMAGE
    - docker push $CI_REGISTRY_IMAGE

But when I try changing the docker push command according to Gitlabs Container Registry Naming Convention the pipeline fails every time, as it tries to access a registry, which does not exist. Here is a list of commands i have tried and all failed.

- docker push ${CI_REGISTRY_IMAGE}/${OS_IDENTIFIER}-base${tag}
- docker push ${CI_REGISTRY}/$[CI_PROJECT_ROOT_NAMESPACE}/${CI_PROJECT_NAME}
- docker push ${CI_REGISTRY}/$[CI_PROJECT_NAMESPACE}/${CI_PROJECT_NAME}
- docker push ${CI_REGISTRY}/<my-group>/<my-project>/<image>:latest
- docker push <registry.example.com>/<mynamespace>/<myproject>/<image>:latest

All of the mentioned above (I've tried many more combinations) yield this error:

$ docker push <one-of-the-above>:latest
An image does not exist locally with the tag: <one-of-the-above>
The push refers to repository [<one-of-the-above>]

The push command seems to refer only to a entire repository and not as I expected when using the naming convention. How do I solve this issue? What am I doing wrong?

Thank you in advance

1 Answers

The image ID you build your image with must match the image ID you push. Currently you are trying to push the ID you don't have. Accordingly, you need to tag your image with full id when building, so something along these lines should work:

- docker build --pull -t ${CI_REGISTRY}/<my-group>/<my-project>/<image>:latest .
- docker push ${CI_REGISTRY}/<my-group>/<my-project>/<image>:latest

Again, the key here is that the ID on build must match exactly the ID on push. Naming also needs to be compatible with your registry permissions - but you should be able to figure this one out.

Related