gitlabdockerfile

Gitlab image giving docker not found


I am trying to create a baseline image (Stage A) and then use this image in subsequent stages (say Stage B). The image is getting created fine but the next stage is not able to use the same image. I am getting

docker not found error

Here is my .gitlab.ci file:

stages:
    - build-base
    - build-addons 


build:
    stage: build-base
    image: docker:20.10.3
    # tags:
    # #    - docker
    services: 
       - docker:dind
    script:
      - date=$(date '+%Y%m%d')
      - echo $date
        - echo $CI_REGISTRY_IMAGE
        - docker build --progress=plain -t $CI_REGISTRY_IMAGE:latest  -f test.Dockerfile .
        - docker push $CI_REGISTRY_IMAGE:latest

build_terraform:  
    image: $CI_REGISTRY_IMAGE:latest
    stage: build-addons
    timeout: 1 hour
    # services: 
    #    - docker:dind
    script:
        - echo $CI_REGISTRY_IMAGE
        - docker info 
        - docker build 
          --pull 
          -t $CI_REGISTRY_IMAGE:date -f addons.Dockerfile . 
Dockerfile

# Use the Red Hat Enterprise Linux 8 base image
FROM registry.access.redhat.com/ubi8/ubi

# Install necessary packages and dependencies
RUN yum install -y yum-utils device-mapper-persistent-data lvm2 && \
    yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo && \
    yum install -y docker-ce docker-ce-cli containerd.io && \
    yum clean all

Solution

  • It seems that there are two extra indents in your gitlab-ci.yml file (after first echo in build job), and because of that, echo is executing for the next 3 lines, and it exits successfully, without pushing the image. Therefore, in the next step as the docker is not installed, it will give docker not found error.

    Besides that, I have added docker login to the gitlab-ci.yml file also:

    stages:
        - build-base
        - build-addons 
    
    
    build:
        stage: build-base
        image: docker:20.10.3
        # tags:
        # #    - docker
        services: 
           - docker:dind
        script:
          - date=$(date '+%Y%m%d')
          - echo $date
          - echo $CI_REGISTRY_IMAGE
          - echo "$CI_REGISTRY_PASSWORD" | docker login $CI_REGISTRY -u $CI_REGISTRY_USER --password-stdin
          - docker build --progress=plain -t $CI_REGISTRY_IMAGE:latest  -f test.Dockerfile .
          - docker push $CI_REGISTRY_IMAGE:latest
    
    build_terraform:  
        image: $CI_REGISTRY_IMAGE:latest
        stage: build-addons
        timeout: 1 hour
        # services: 
        #    - docker:dind
        script:
            - echo $CI_REGISTRY_IMAGE
            - echo "$CI_REGISTRY_PASSWORD" | docker login $CI_REGISTRY -u $CI_REGISTRY_USER --password-stdin
            - docker info 
            - docker build 
              --pull 
              -t $CI_REGISTRY_IMAGE:date -f addons.Dockerfile . 
    

    You may have the issue of ERROR: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running? in the second job. Either your Dockerfile should not override the important stuff of docker:dind (like entrypoint), or you should share the docker socket of the gitlab runner.