From GitLab CI to Docker Hub

With all the noise around those topics I would have imagined this one had been covered thousands of time, yet I did not find a single complete resource on this subject which I found to be a basic building block: pushing docker images from GitLab CI to the Docker Hub registry.

There’s actually an opened issue on Docker GitHub‘s that’s sitting there for 3 years, and it really feels more like a political / strategic / commercial issue than a technical one. Point being, there’s no straightforward integration between GitLab.com and Docker Hub.

I really don’t like the method that’s been used by all the people in the same situation but I just can’t find any other one: you basically have to create variables within GitLab.com‘s CI settings with your Docker Hub login and password. This looks like this :

CI / CD Variables

One more catch, the $CI_REGISTRY is the value found in docker info|grep -i registry:, here being https://index.docker.io/v1/. Thanks to this post for the hint.

Once the credentials and endpoint set, you can write a .gitlab-ci.yml file that looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
image: docker:latest

services:
- docker:dind

stages:
- build

variables:
IMAGE_TAG: imil/myimage:$CI_COMMIT_TAG

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

build:
stage: build
script:
- docker build -t $IMAGE_TAG .
- docker push $IMAGE_TAG

I didn’t find GitLab documentation impressive on how to use the CI in their very own website, here are some pointers :

About this $IMAGE_TAG variable, you could replace myimage with $CI_PROJECT_NAME if the project has the same name on Docker Hub and GitLab (which is a good idea).
The $CI_COMMIT_TAG is set when an actual git tag is pushed, so this is triggered this way:

1
2
3
$ git commit -m "things changed" foo .gitlab-ci.yml
$ git tag 0.27
$ git push tags 0.27

This will summon the CI / CD pipeline with a $CI_COMMIT_TAG value of 0.27 , hopefully build the docker image and push it to Docker Hub.

Do you have another solution? Please tell me in the comments!