55

Gitlab provides a .gitlab-ci.yml template for building and publishing images to its own registry (click "new file" in one of your project, select .gitlab-ci.yml and docker). The file looks like this and it works out of the box :)

# This file is a template, and might need editing before it works on your project.
# Official docker image.
image: docker:latest

services:
  - docker:dind

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

build-master:
  stage: build
  script:
    - docker build --pull -t "$CI_REGISTRY_IMAGE" .
    - docker push "$CI_REGISTRY_IMAGE"
  only:
    - master

build:
  stage: build
  script:
    - docker build --pull -t "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG" .
    - docker push "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG"
  except:
    - master

But by default, this will publish to gitlab's registry. How can we publish to docker hub instead?

1
  • 3
    Never specify ":latest" as your image in a CI script. Your build is not reproducible anymore Jul 16, 2019 at 20:14

2 Answers 2

82

No need to change that .gitlab-ci.yml at all, we only need to add/replace the environment variables in project's pipeline settings.

1. Find the desired registry url

Using hub.docker.com won't work, you'll get the following error:

Error response from daemon: login attempt to https://hub.docker.com/v2/ failed with status: 404 Not Found

Default docker hub registry url can be found like this:

docker info | grep Registry
Registry: https://index.docker.io/v1/

index.docker.io is what I was looking for.

2. Set the environment variables in gitlab settings

I wanted to publish gableroux/unity3d images using gitlab-ci, here's what I used in Gitlab's project > Settings > CI/CD > Variables

CI_REGISTRY_USER=gableroux
CI_REGISTRY_PASSWORD=********
CI_REGISTRY=docker.io
CI_REGISTRY_IMAGE=index.docker.io/gableroux/unity3d

CI_REGISTRY_IMAGE is important to set.
It defaults to registry.gitlab.com/<username>/<project>
regsitry url needs to be updated so use index.docker.io/<username>/<project>

Since docker hub is the default registry when using docker, you can also use <username>/<project> instead. I personally prefer when it's verbose so I kept the full registry url.

This answer should also cover other registries, just update environment variables accordingly. 🙌

3
  • Could be great to explain where we can do these action more precisely than just " add/replace the environment variables in project's pipeline settings."
    – Arnaud F.
    Mar 16, 2021 at 17:20
  • 1
    @ArnaudF. I just updated the answer with your suggestion. There are many ways to set the environment variables in a gitlab-ci pipeline. You can do it in the project's CI/CD settings page, but it can also be done using the variables key in your .gitlab-ci.yml. It is preferable to keep secrets in the settings to prevent to keep them hidden.
    – GabLeRoux
    Mar 16, 2021 at 18:21
  • I am getting "The push refers to repository [docker.io/user/repo]" still by changing index in registry image Feb 6 at 15:36
17

To expand on the GabLeRoux's answer,

I had issues on the pushing stage of the GitLab CI build:

denied: requested access to the resource is denied

By changing my CI_REGISTRY to docker.io (remove the index.) I was able to successfully push.

1
  • 1
    To state what should be obvious (eventually); the original answer by GabLeRoux has been updated and now "index." prefixes the value in CI_REGISTRY_IMAGE so that it follows the pattern: index.docker.io/<username>/<project>
    – rainabba
    May 28, 2020 at 14:20

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.