DeerFreak
DeerFreak

Reputation: 115

Is there a way to use container registry images in dockerfiles?

I am currently trying to build docker images in Gitlab CI. Here I am trying to use an image from the Gitlab container registry to build the image.

FROM <CI_REGISTRY>/<CI_PROJECT_ROOT_NAMESPACE>/<CI_PROJECT_NAME>/<image_name>

RUN echo 'hello world' 

executed with

test:
 stage: build
 image: docker
 services: 
   - docker:dind
 before_script:
   - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
 script:
   - docker build --pull -t $CI_REGISTRY_IMAGE/test:latest .
   - docker push $CI_REGISTRY_IMAGE/test:latest

is not able to build the image, as the manifest is unknown.

What is the best way of doing this? Pulling the image in the job before building the image does feel not right to me.

Thank you

Upvotes: 1

Views: 884

Answers (1)

Romain TAILLANDIER
Romain TAILLANDIER

Reputation: 1985

I think you might read this : https://docs.docker.com/engine/reference/builder/#understand-how-arg-and-from-interact

then your docker file may start like :

ARG CI_REGISTRY_IMAGE_FULL_NAME
FROM ${CI_REGISTRY_IMAGE_FULL_NAME}

then your call to docker build may look like :

script:
   - > docker build --pull 
       --build-arg CI_REGISTRY_IMAGE_FULL_NAME=$CI_REGISTRY_IMAGE_FULL_NAME
       -t $CI_REGISTRY_IMAGE/test:latest .

Do not confuse CI_REGISTRY_IMAGE_FULL_NAME the name of the base image, with $CI_REGISTRY_IMAGE/test:latest which will be the new image name

Upvotes: 2

Related Questions