use image from gitlab Registry in CI

Viewed 7796

Could you tell me if I do it in correct way:

  • I have create Docker image with all stuff which is need for running my tests in gitlab CI
  • I push it to gitlab registry
  • I can see on gitlab page in section Registry my image - gitlablogin/projectname
  • I want to use this image for CI, so in .gitlab-ci.yml I add image: gitlablogin/projectname

Before I had had in .gitlab-ci.yml

same_task:
  stage: deploy
  image: python:3
  script:
    - python -V

Now I have:

pep8:
  stage: deploy
  image: gitlablogin/projectname
  script:
    - python -V

and after this change job failed:

Running with gitlab-runner 11.4.2 (cf91d5e1)
  on docker-auto-scale 72989761
Using Docker executor with image gitlablogin/projectname ...
Pulling docker image gitlablogin/projectname ...
ERROR: Job failed: Error response from daemon: pull access denied for gitlablogin/projectname, repository does not exist or may require 'docker login' (executor_docker.go:168:0s)

Is my usage of docker in context of gitlab CI and gitlab registry is correct? I also want to keep my docker file on same repo and build new image when samething change in Dockerfile, what will be the best way to do it?

2 Answers

Right now it is possible to use images from your gitlab registry without any special steps. Just build and push an image to your gitlab project container registry

docker build -t registry.gitlab.com/gitlabProject/projectName:build .
docker push registry.gitlab.com/gitlabProject/projectName:build 

and then just specify this image in your pipeline settings:

image: registry.gitlab.com/gitlabProject/projectName:build

Gitlab is able to pull this image using it's credentials:

Preparing the "docker+machine" executor
00:46
 Using Docker executor with image registry.gitlab.com/gitlabProject/projectName:build ...
 Authenticating with credentials from job payload (GitLab Registry)
 Pulling docker image registry.gitlab.com/gitlabProject/projectName:build ...
 Using docker image sha256:e7e0f4f5fa8cff8a93b1f37ffd7dd0505946648246aa921dd457c06a1607304b for registry.gitlab.com/gitlabProject/projectName:build ...

More: https://docs.gitlab.com/runner/configuration/advanced-configuration.html#support-for-gitlab-integrated-registry

Your approach isn't far from what you want to achieve. I believe what you are missing is this:

According to: https://docs.gitlab.com/ee/ci/docker/using_docker_images.html#what-is-an-image

in order to use the image you have built for your CI you will first need to add it to runner's config.toml file as a service.

Once you have done so you would be able to use the directive: image: my_image

However, there's another option which you could do: you could login to your Docker registry, pull and run the CI Docker image and then you can exec inside where you will be running your pipeline.

Related