Using image we built in previuos stage in Gitlab-CI

Hello

We are using gitlab as git software now for CI i choose gitlab and decide to not use any thing else
so i have a question
my runner use docker as executor so I decide to have 3 stages

-build
-test
-deploy

the question is can i use exactly the same image in build in test stage or a way to push that image in our private registery and call it in test section ?

sorry for poor english

thanks

Currently, you can only use images from public projects as images for runners. We will eventually address that though.

So, I think you are looking for something like this…

Basically we are spinning up a docker-in-docker container.

I’ve had to occasionally put a sleep phase to give the docker-in-docker container a chance to start docker

`image: docker:git
services:

  • docker:dind
    stages:
  • build
  • test
  • release
  • deploy
    variables:
    CONTAINER_TEST_IMAGE: registry.example.com/my-group/my-project:$CI_BUILD_REF_NAME
    CONTAINER_RELEASE_IMAGE: registry.example.com/my-group/my-project:latest
    before_script:
    • docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN registry.example.com
      build:
      stage: build
      script:
      • docker build -t $CONTAINER_TEST_IMAGE .
      • docker push $CONTAINER_TEST_IMAGE
        test1:
        stage: test
        script:
      • docker run $CONTAINER_TEST_IMAGE /script/to/run/tests
        test2:
        stage: test
        script:
      • docker run $CONTAINER_TEST_IMAGE /script/to/run/another/test
        release-image:
        stage: release
        script:
      • docker pull $CONTAINER_TEST_IMAGE
      • docker tag $CONTAINER_TEST_IMAGE $CONTAINER_RELEASE_IMAGE
      • docker push $CONTAINER_RELEASE_IMAGE
        only:
      • master
        deploy:
        stage: deploy
        script:
      • ./deploy.sh
        only:
      • master`

May be a repost, my last reply didn’t seen to take

Take a look at this.

Some times I’ve had to add a sleep phase to let the docker-in-docker container to fully boot.’

`image: docker:git
services:

  • docker:dind
    stages:
  • build
  • test
  • release
  • deploy
    variables:
    CONTAINER_TEST_IMAGE: registry.example.com/my-group/my-project:$CI_BUILD_REF_NAME
    CONTAINER_RELEASE_IMAGE: registry.example.com/my-group/my-project:latest
    before_script:
    • docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN registry.example.com
      build:
      stage: build
      script:
      • docker build -t $CONTAINER_TEST_IMAGE .
      • docker push $CONTAINER_TEST_IMAGE
        test1:
        stage: test
        script:
      • docker run $CONTAINER_TEST_IMAGE /script/to/run/tests
        test2:
        stage: test
        script:
      • docker run $CONTAINER_TEST_IMAGE /script/to/run/another/test
        release-image:
        stage: release
        script:
      • docker pull $CONTAINER_TEST_IMAGE
      • docker tag $CONTAINER_TEST_IMAGE $CONTAINER_RELEASE_IMAGE
      • docker push $CONTAINER_RELEASE_IMAGE
        only:
      • master
        deploy:
        stage: deploy
        script:
      • ./deploy.sh
        only:
      • master`

`image: docker:git
services:

  • docker:dind
    stages:
  • build
  • test
  • release
  • deploy
    variables:
    CONTAINER_TEST_IMAGE: registry.example.com/my-group/my-project:$CI_BUILD_REF_NAME
    CONTAINER_RELEASE_IMAGE: registry.example.com/my-group/my-project:latest
    before_script:
    • docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN registry.example.com
      build:
      stage: build
      script:
      • docker build -t $CONTAINER_TEST_IMAGE .
      • docker push $CONTAINER_TEST_IMAGE
        test1:
        stage: test
        script:
      • docker run $CONTAINER_TEST_IMAGE /script/to/run/tests
        test2:
        stage: test
        script:
      • docker run $CONTAINER_TEST_IMAGE /script/to/run/another/test
        release-image:
        stage: release
        script:
      • docker pull $CONTAINER_TEST_IMAGE
      • docker tag $CONTAINER_TEST_IMAGE $CONTAINER_RELEASE_IMAGE
      • docker push $CONTAINER_RELEASE_IMAGE
        only:
      • master
        deploy:
        stage: deploy
        script:
      • ./deploy.sh
        only:
      • master`

I am miss understanding the question or the answer?

You can use private docker images, from a private registry.

in gitlab-ci.yml

image: registry.fqdn.com/group/imagename:tag

Hello @Conradjones

The question is each stages built a docker image :
Build
Test
Deploy

in first stage we use a public docker image execute several commands in it and then exit is it true ?
I dont want to exit the container I want to save that and using it as Test stage image .
Now im using SSH as executor and use a Docker file to build an image and do other works .

its not all about creating a Docker inside a Docker container we want to call Docker docker inside the container Commit changes and push it to our private registry

I hope this time I explained better

I’m starting to work on something similar to this too.

What I’m thinking is that you will only run the “docker build” command in the first build stage for your “dev” environment. As you promote the code via Merge Request to each new environment “Integration” or “QA” or “Staging” or “Production”, you can just simply re-tag the same docker image in the private Gitlab Registry with the relevant tag names for the environment you are targeting.
You will use the “docker tag” command to do that.

So at the end of your deploy stage, you can try to run that “docker tag” command. In the subsequent stages you won’t re-build the Docker Image, but just pull against the image you just tagged.

I hope that makes sense.