0

.gitlab-ci.yml

workflow:
  rules:
    - if: $CI_COMMIT_BRANCH != "main" && $CI_PIPELINE_SOURCE != "merge_request_event" 
      when: never
    - when: always


variables:
  IMAGE_NAME: $CI_REGISTRY_IMAGE


stages:
  - test
  - build



run_unit_tests:
  image: node:16-alpine3.17
  stage: test
  tags:
    - txy
  before_script:
    - cd app
    - npm install
  script: 
    - npm test
  artifacts:
    when: always
    paths:
      - app/junit.xml
    reports:
      junit: app/junit.xml


build_image:
  stage: build
  tags: 
    - remoteone
  before_script:
    - echo "Docker registry url is $CI_REGISTRY"
    - echo "Docker registry username is $CI_REGISTRY_USER"
    - echo "Docker registry repo is $CI_REGISTRY_IMAGE"
  script:
    - docker build -t $IMAGE_NAME .


push_image:
  stage: build
  needs: 
    - build_image
  tags:
    - remoteone
  before_script:
    - echo "Docker registry url is $CI_REGISTRY"
    - echo "Docker registry username is $CI_REGISTRY_USER"
    - echo "Docker registry repo is $CI_REGISTRY_IMAGE"
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
  script: 
    - docker push $IMAGE_NAME


enter image description here

correspondence of the above variables

$CI_REGISTRY_IMAGE = docker build -t registry.gitlab.com/komorebi-cqd/mynodeapp-cicd-project .

$CI_REGISTRY = docker push registry.gitlab.com/komorebi-cqd/mynodeapp-cicd-project

Dockerfile

FROM node:16-alpine
RUN apk add docker-cli

WORKDIR /usr/src/app

COPY ./app/package*.json ./
RUN npm install
COPY ./app .

EXPOSE 3000
CMD [ "npm" ,"start"]

cicd pipelines build_image error

Running with gitlab-runner 15.5.1 (7178588d)
  on one-docker-runner fAyvKeBR
Preparing the "docker" executor
00:03
Using Docker executor with image alpine:3.14 ...
Pulling docker image alpine:3.14 ...
Using docker image sha256:dd53f409bf0bd55eac632f9e694fd190244fef5854a428bf3ae1e2b636577623 for alpine:3.14 with digest alpine@sha256:4c869a63e1b7c0722fed1e402a6466610327c3b83bdddb94bd94fb71da7f638a ...
Preparing environment
00:01
Running on runner-fayvkebr-project-41046722-concurrent-0 via VM-20-2-ubuntu...
Getting source from Git repository
00:02
Fetching changes with git depth set to 20...
Reinitialized existing Git repository in /builds/komorebi-cqd/mynodeapp-cicd-project/.git/
Checking out 023ee3af as main...
Removing app/junit.xml
Skipping Git submodules setup
Downloading artifacts
00:03
Downloading artifacts for run_unit_tests (3502740847)...
Downloading artifacts from coordinator... ok        id=3502740847 responseStatus=200 OK token=64_pVtH2
Executing "step_script" stage of the job script
00:01
Using docker image sha256:dd53f409bf0bd55eac632f9e694fd190244fef5854a428bf3ae1e2b636577623 for alpine:3.14 with digest alpine@sha256:4c869a63e1b7c0722fed1e402a6466610327c3b83bdddb94bd94fb71da7f638a ...
$ echo "Docker registry url is $CI_REGISTRY"
Docker registry url is registry.gitlab.com
$ echo "Docker registry username is $CI_REGISTRY_USER"
Docker registry username is gitlab-ci-token
$ echo "Docker registry repo is $CI_REGISTRY_IMAGE"
Docker registry repo is registry.gitlab.com/komorebi-cqd/mynodeapp-cicd-project
$ docker build -t $IMAGE_NAME .
/bin/sh: eval: line 137: docker: not found
Cleaning up project directory and file based variables
00:00
ERROR: Job failed: exit code 127

It prompts docker: not found,i have been searching the Internet for a long time, but there is no specific solution

I have a Docker installed on my server, and there is no problem running Docker PS. However, when I use the gitlab registry, I will be prompted that the Docker does not exist. I do not know where the problem is, and there is no corresponding solution online. I hope someone can tell me why the Docker is not found and how to solve it.

1 Answers1

1

GitLab CI uses Docker to run your pipelines (with the Docker executor) but it doesn't mean that Docker will be available from inside the CI containers.

You can see from the CI output that it uses the alpine:3.14 Docker image to run your commands, unfortunately this image has not Docker installed:

Using Docker executor with image alpine:3.14 ...

The GitLab documentation describes 3 ways to build Docker images with their CI using:

  • the Shell executor
  • Docker-in-Docker with the Docker or Kubernetes executor
  • Docker socket binding

I'd say to go with the Docker-in-Docker approach because the other 2 may result in concurrency issues: concurrent pipelines will use the same Docker daemon which could result in naming collision of Docker objects.


You can read more about my personal experience in 2020 about building Docker images in GitLab CI if that helps.

AymDev
  • 6,626
  • 4
  • 29
  • 52
  • I used your method, but it made another error,```error during connect: Post "http://docker:2375/v1.24/build?buildargs=%7B%7D&cachefrom=%5B%5D&cgroupparent=&cpuperiod=0&cpuquota=0&cpusetcpus=&cpusetmems=&cpushares=0&dockerfile=Dockerfile&labels=%7B%7D&memory=0&memswap=0&networkmode=default&rm=1&shmsize=0&t=registry.gitlab.com%2Fkomorebi-cqd%2Fmynodeapp-cicd-project&target=&ulimits=null&version=1": dial tcp: lookup docker on 183.60.83.19:53: no such host``` – qiaoda chen Dec 21 '22 at 14:47
  • I also tried to follow the Docker in Docker in the gitlab document, but the same error occurred – qiaoda chen Dec 21 '22 at 14:50
  • @qiaodachen this is another issue unrelated to your original question, I don't know what's wrong with your updated setup. Do you find solutions in [this other question](https://stackoverflow.com/questions/71657162/gitlab-runner-docker-login-not-working-error-during-connect-post-http-docker) ? If not, you can ask a new question on SO. – AymDev Dec 22 '22 at 08:52