How to publish docker images to docker hub from gitlab-ci

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. 🙌


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.