0

I have the following logic in my GitLab-ci.yml:

stages:
  - build
  - deploy

variables:
    IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG

make_patch:
  image: node:10.19
  stage: build
  script:
    - npm install
    - make
    - make source-package
  cache:
    key: ${CI_COMMIT_REF_SLUG}
    paths:
    - node_modules/
  artifacts:
    when:
    paths:
      - test.tar.bz2
    expire_in: 2 days

  test_feature:
    stage: deploy
    image: dockerhub_image:123
    script:
      - apt-get install bzip2
      - apt-get install curl -y
      - ls -lah
      - curl -kL https://mygitlabserver/namespace/project/-/jobs/artifacts/master/download?job=make_patch
      - tar -xvjf test.tar.bz2
      - docker login -u $CI_DEPLOY_USER -p $CI_DEPLOY_PASSWORD $CI_REGISTRY
      - docker build --no-cache -t $IMAGE_TAG .
      - docker push $IMAGE_TAG

So I'm trying to test changes in my repo by building a nodejs image; if it compiles, then I save a zip file containing all the web files needed to be used in a potential production system. Then I download an existing image from public docker hub (cus this is actually an open source app we are modifying). I want to update the web folder there, and then try to create a docker container out of it and save it to my project repository. As you can see from the output below, it's currently failing because as @marvin pointed out, the dockerhub_image:123 in doesn't have the docker cli.

523$ docker login -u $CI_DEPLOY_USER -p $CI_DEPLOY_PASSWORD $CI_REGISTRY
524/bin/bash: line 107: docker: command not found
525[cmd] sh exited 1
526[cont-finish.d] executing container finish scripts...
527[cont-finish.d] done.
528[s6-finish] waiting for services.
529[s6-finish] sending all processes the TERM signal.
530[s6-finish] sending all processes the KILL signal and exiting.
532ERROR: Job failed: exit code 1

I have defined a Deploy token in my project that looks like this:

Name: webtoken
Username: webtoken-1
Created: Aug 6, 2020
Expires: Never
Scopes
read_repository, read_registry, write_registry, read_package_registry, write_package_registry

I found this post: Gitlab CI - docker: command not found

But I'm having a hard time understanding where I would include the reference to the docker image in my case. Or could I just add a services clause within this "test_feature" job?

services:
  - docker:dind

Sorry for the remedial questions. I'm just new to all things docker and GitLab. I tried changing the test_feature job like so, but it still fails with the same error:

  test_feature:
    stage: deploy
    image: dockerhub_image:123
    services:
      - docker:dind
    script:
      - apt-get install bzip2
      - apt-get install curl -y
      - ls -lah
      - curl -kL https://mygitlabserver/namespace/project/-/jobs/artifacts/master/download?job=make_patch
      - tar -xvjf test.tar.bz2
      - docker login -u $CI_DEPLOY_USER -p $CI_DEPLOY_PASSWORD $CI_REGISTRY
      - docker build --no-cache -t $IMAGE_TAG .
      - docker push $IMAGE_TAG
dot
  • 14,928
  • 41
  • 110
  • 218
  • Looks like your are basically missing the docker runner/executor : the "docker" command is not found. Is there some clue (in the missing previous lines) that make you think it's stg else? – Marvin Aug 06 '20 at 15:04
  • @Marvin I think I assumed that since I was using a docker runner it would just work. Now that you've stated the obvious I'll rewrite my question. Thanks – dot Aug 06 '20 at 15:26

2 Answers2

3

To expand on @Alessandro Chitolina's answer:

Firstly, if you want to see the entire working example of Docker build using docker-in-docker method inside GitLab CI, one is available here.

Secondly, if I don't know how to do something in GitLab CI, I find it usefult to check Auto DevOps templates. The one for Docker build using docker-in-docker is available here. You can even import it to your pipeline directly as described here.

Finally, instead of docker-in-docker approach, you can also use kaniko, as described here.

As a side note - there's no need to manually download the artifact from make_patch in test_feature job - it will be downloaded automatically - see here for details.

Konrad Botor
  • 4,765
  • 1
  • 16
  • 26
1

You are not running a docker image as executor.

This is taken from a working job building a docker image:

build:
    image: docker:latest
    stage: build
    services:
        - docker:dind
    variables:
        DOCKER_HOST: tcp://docker:2375
        DOCKER_TLS_CERTDIR: ''
    script:
        - docker build -t xxx .