dot
dot

Reputation: 15660

gitlab-ci.yml: how to create a docker image using dind

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

Upvotes: 0

Views: 3106

Answers (2)

Konrad Botor
Konrad Botor

Reputation: 5033

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.

Upvotes: 3

Alessandro Chitolina
Alessandro Chitolina

Reputation: 906

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 .

Upvotes: 1

Related Questions